In-app Language Switches

Support version: v6.3.1

Firework Android SDK provides support for the host app to dynamically change languages at runtime. There is no specific API requirement for the host app to use this feature. The host app has the freedom to implement its own mechanism for overriding the context, and the Firework Android SDK will automatically update the PlayerActivity's BaseContext to the desired locale, enabling the update of resources and layout alignment (LTR/RTL) as per the current locale.

Below is an example showcasing how the host app can implement language changes within the app. Please note that this example can be customized based on your app's design and requirements.

Example

In the following example, the host app utilizes the attachBaseContext override method to update locale configurations. Additionally, an object called LanguageHelper is introduced to facilitate the update of resources' locale and persist the selected language. When users select a language from the options menu, the Activity is restarted to apply the locale updates and reflect the chosen language.

class InAppLanguageActivity : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_in_app_language)
    }

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LanguageHelper.changeLanguage(newBase))
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.in_app_toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.menu_en -> {
                LanguagePreference.setLocalePreference(this, getString(R.string.in_app_language_en))
                this.recreate()
                true
            }

            R.id.menu_ja -> {
                LanguagePreference.setLocalePreference(this, getString(R.string.in_app_language_ja))
                this.recreate()
                true
            }

            R.id.menu_ar -> {
                LanguagePreference.setLocalePreference(this, getString(R.string.in_app_language_ar))
                this.recreate()
                true
            }

            else -> {
                super.onOptionsItemSelected(item)
            }
        }
    }
}
object LanguageHelper {

    fun changeLanguage(context: Context): Context {
        val prefsManager = PreferenceManager.getDefaultSharedPreferences(context)
        val language = prefsManager.getString(
            context.getString(R.string.pref_key_app_language),
            context.getString(R.string.app_language_default),
        ) ?: context.getString(R.string.app_language_default)

        return updateBaseContextLocale(context, language)
    }

    fun setLocalePreference(context: Context, language: String) {
        val key = context.getString(R.string.pref_key_app_language)
        val prefsManager = PreferenceManager.getDefaultSharedPreferences(context)
        prefsManager.edit().putString(key, language).apply()
    }

    private fun updateBaseContextLocale(context: Context, language: String?): Context {
        language?.let {
            val localeStrings = language.split("-")
            val locale = if (localeStrings.size > 1) {
                Locale(localeStrings[0], localeStrings[1])
            } else {
                Locale(language)
            }
            return updateResourcesLocale(context, locale)
        }
        return updateResourcesLocale(context, Locale.getDefault())
    }

    private fun updateResourcesLocale(context: Context, locale: Locale): Context {
        val resources = context.resources
        val configuration = resources.configuration
        configuration.setLocale(locale)
        configuration.setLayoutDirection(locale)
        return context.createConfigurationContext(configuration)
    }
}

Last updated