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
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.Also, you can manage your translation via the Android studio editor like:
strings.xml
and you can find Open Editor optionYou can check the documentation for this approach here: https://developer.android.com/guide/topics/resources/localization#creating-alternatives
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
in JSON format in Firebase.
Sample Code for Localization Manager
object LocalizationManager {
private var translations: Map<String, String> = mapOf()
}
// Usage
LocalizationManager.loadTranslations(firebaseRemoteConfig.getString("translations"))
val localizedString = LocalizationManager.getString("hello_key", "Hello")