skip to Main Content

I’m working on an Android application that requires support for multiple languages. I am considering using Firebase Remote Config to manage localization by storing the translations in JSON format, which would then dynamically update the app’s strings.xml files at runtime. Is it possible to directly update strings.xml or a similar resource file at runtime using data fetched from Firebase? If so, how can this be implemented?

Additionally, I would appreciate any advice on whether this approach is advisable, or if there are better practices for handling dynamic localization in Android apps using Firebase.

What I Tried:

Data Class Setup: I created a data class in Kotlin to hold the localized strings after parsing them from the JSON retrieved from Firebase Remote Config.
Programmatic Localization: Attempted to apply these parsed strings programmatically throughout the app instead of relying on the traditional strings.xml mechanism.
Expectations vs. Reality:

Expected: I expected that once the JSON data was parsed into the data class, I could use these objects to dynamically update the UI with localized strings based on the user’s language settings.
Actual Result: Despite the JSON being parsed correctly and the data class being populated with translation strings, I found no effective way to integrate these dynamic translations seamlessly across all parts of the app. The main issue is that without modifying the strings.xml at runtime (which is not possible), I cannot use Android’s built-in resource system (R.string) to manage localization.
Problems Faced:

The lack of integration with Android’s native resource system means I can’t use familiar mechanisms like getString(R.string.some_string) to fetch and display the translated strings. This limits the scalability and maintainability of the solution, as it diverges from standard Android development practices.

2

Answers


  1. For sure you won’t be able to modify the resource files at runtime because it is generated at compile time so the best approach for localization and support of multiple languages is to create string.xml files for each language.

    res/values/strings.xml -> for the default resource and fallbacks
    res/values-ar/strings.xml -> for the alternative resource like Arabic.
    

    Also, you can manage your translation via the Android studio editor like:

    • First, open your strings.xml and you can find Open Editor option

    enter image description here

    • Second, modify your translation

    enter image description here

    You can check the documentation for this approach here: https://developer.android.com/guide/topics/resources/localization#creating-alternatives

    Login or Signup to reply.
  2. For handling dynamic localization in Android using Firebase, you can bypass modifying strings.xml by storing translations in Firebase Remote Config and fetching them at runtime. Here’s a simplified way to manage this:

    Step-by-Step Approach

    1. Store Translations in Firebase Remote Config: Keep your translations
      in JSON format in Firebase.
    2. Create a Localization Manager: Develop a singleton class that fetches and stores these translations. Replace calls to getString(R.string.some_string) with your manager’s method that retrieves the correct translation based on the user’s locale.
    3. Fetch and Use Translations: At app launch or when needed, load translations from Firebase and update your app’s UI accordingly.

    Sample Code for Localization Manager

    object LocalizationManager {
    private var translations: Map<String, String> = mapOf()

    fun loadTranslations(jsonString: String) {
        translations = Gson().fromJson(jsonString, object : TypeToken<Map<String, String>>() {}.type)
    }
    
    fun getString(key: String, default: String = ""): String {
        return translations[key] ?: default
    }
    

    }

    // Usage
    LocalizationManager.loadTranslations(firebaseRemoteConfig.getString("translations"))
    val localizedString = LocalizationManager.getString("hello_key", "Hello")

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search