> For the complete documentation index, see [llms.txt](https://docs.firework.com/firework-for-developers/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.firework.com/firework-for-developers/android-sdk/integration-guide/in-app-language-switches.md).

# In-app Language Switches

Support version: v6.3.1

## SDK Locale and App Resources

Keep the SDK locale and your app's Android resource locale aligned:

```kotlin
import com.firework.sdk.FireworkSdk
import java.util.Locale

// Call when applying the user's selected app language.
FireworkSdk.setLocale(Locale.forLanguageTag("ja-JP"))
```

`setLocale` selects the locale used by locale-aware SDK behavior and data requests. It does not change your Activity's resource configuration or automatically recreate existing views. Apply your app's language change and recreate affected UI as required by your host implementation; newly loaded SDK content uses the selected SDK locale.

The example below illustrates host resource configuration. Add the SDK call to the same language-selection flow; the SDK does not require the particular `LanguagePreference` helper shown here.

## Host Resource Configuration

Firework Android SDK provides support for the host app to dynamically change languages at runtime. The host app controls Android resource localization; use `FireworkSdk.setLocale` above for the SDK locale as well. 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)
    }
}
```
