# In-app Language Switches

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.

{% file src="/files/8p5QOPQ5j5LJAPpNlqb7" %}
In-app langauge changes demo recording
{% endfile %}

```kotlin
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)
            }
        }
    }
}

```

```kotlin
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)
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.firework.com/firework-for-developers/android-sdk/integration-guide/in-app-language-switches.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
