Setting the app language and sending to firebase config always using the device system language

187 Views Asked by At

I have an android app and using remote config.

I have this usecase and I want to send the app language change in that request. My app supports both English and Vietnam and the user can select their preferred language. I am just testing with changing to vietnam language.

However, in the request its always using my system device language. And as that is always set to English it always sends en-US.

In my firebase remote config console I have a field that has an Language condition for English and Vietnam. So based on that condition I can do something in the app.

Just a side question how does firebase know where to extract these properties and send them in the request?

2

There are 2 best solutions below

7
Yakubu On

To send the languageCode based on user selection in your app to Firebase Remote Config:

  • Store the user's language choice in local storage, like SharedPreferences.
  • Retrieve this language setting and apply it to your Firebase Remote Config request before fetching configurations.

E.G

val userLanguage = sharedPreferences.getString("user_language", "en-US")
remoteConfig.setDefaultsAsync(mapOf("languageCode" to userLanguage))
remoteConfig.fetchAndActivate()

Firebase automatically gathers standard details like app version and device info via the integrated SDK. When you manually set additional properties, like languageCode, Firebase includes these in the server requests, helping determine the appropriate configuration to return based on your Remote Config settings.

You can also check their doc

Firebase remote config

Another approach

fetch all configurations, and select values based on the app's internal language setting since firebase won't allow dynamic fetch at run time

class FetchRemoteConfigUseCase @Inject constructor(
    private val remoteConfigProvider: RemoteConfigProvider,
    private val firebaseRemoteConfigSettings: 
    FirebaseRemoteConfigSettings,
    private val sharedPreferences: SharedPreferences, // update your DI
    private val schedulersFacade: SchedulersFacade
  ) {
   fun execute(): Completable {
       val userLanguageCode = sharedPreferences.getString("user_language", "en") // "en" for English, "vi" for Vietnamese

    return Completable.create { completable ->
        remoteConfigProvider
            .remoteConfig
            .setConfigSettingsAsync(firebaseRemoteConfigSettings)
            .continueWithTask {
                remoteConfigProvider.remoteConfig.fetchAndActivate()
            }
            .addOnCompleteListener {
                // After fetching, manually select the appropriate language configuration
                val configKey = "${userLanguageCode}_yourConfigKey" // Construct the key based on the user's selected language
                val configValue = remoteConfigProvider.remoteConfig.getString(configKey) // Use the constructed key to retrieve the value
                
                // Use configValue as needed in your app
                
                completable.onComplete()
              }
         }.observeOn(schedulersFacade.io)
     }
}

NB: configKey is a variable that represents the key for a specific configuration parameter you've set up in Firebase Remote Config

0
ryankuck On

The remote config allows key-value data to be sent to the frontend, but it needs to be actually used by the networking library.

The way the networking library picks up the language is similar to web browsers, by using Accept-Language Http Headers. Firebase Hosting also says it uses this method. Note that the "languageCode" of the device will automatically be sent to the backend, no additional work is needed to change it based on the devices settings. You would need to modify it if the user uses there phone in one language and wants to use the app in a different language.

Instruction to "Configure internationalization (i18n) rewrites" for Firebase Hosting are here. Here is an example in Flutter for localization with Firebase remote config.

In other Android contexts, this post explains how Accept-Language Http Headers may be used with OkHttp network requests. And this one for DefaultHttpClient

An example of how to change it in general on Android:

HttpClient client = new DefaultHttpClient();
  HttpPost request = new HttpPost(webServiceUrl);
  request.addHeader("Accept-Language", "fr");
  HttpResponse response = client.execute(request);