# App-level Language Setting (Flutter)

## Dart integration

```dart
import 'package:fw_flutter_sdk/fw_flutter_sdk.dart';

// You should pass language code to this API, such as "en", "ar", "pt-BR" and "hu" etc.
// For the language codes we support, please refer to "Language support table" below.
FireworkSDK.getInstance().changeAppLanguage("ar");
```

Please refer to the [Language support table](#language-support-table) to learn the languages we supported.

{% hint style="info" %}
Generally, the `changeAppLanguage` API should be called in the following cases:

1. The App is launched(e.g. in the initState method of your App State.).
2. Users change the app language manually.
3. Other cases that change app language.
   {% endhint %}

## Additional setup steps on iOS

Firework SDK widgets are based on native views. Thus, as depicted in the screenshot below, you must add all the language localizations you wish to support to the iOS project, as required by Apple.

<figure><img src="https://688917408-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MLoGG8m6bokS9YTmS7m%2Fuploads%2Fgit-blob-edb709cb88245239067c953b6d30c3a6aef9125d%2Fimage%20(1)%20(1)%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

## Additional setup steps on Android

We also require some additional setup steps on Android. On Android, there are two behaviors for switching languages: `RestartingActivity` and `NotRestartingActivity`. Each behavior involves different setup steps, and you can choose either one.

### Option 1: Setup steps for `RestartingActivity`

**`RestartingActivity` option will restart the Android activity when changing the app language**.

#### Calling sequence

{% hint style="danger" %}
Currently, `FireworkSDK.getInstance().changeAppLanguage` will recreate the activity by default on Android. Therefore, we need to follow the calling sequence.
{% endhint %}

Generally, you also have codes for switching languages, such as the codes of caching the language. Please call `FireworkSDK.getInstance().changeAppLanguage` after all your codes for switching languages. For example:

```dart
import 'package:localstorage/localstorage.dart';

const language = "ar";

// cache language
await localStorage.setItem("app_language", language);

// We need to call SDK changeAppLanguage API after your codes for switching languages,
// such as the codes of caching the language
await FireworkSDK.getInstance().changeAppLanguage(language);
```

#### Add codes on Android project

To be able to use the in-app language feature, the following configuration needs to be added to the app project.

#### Override the `attachBaseContext` method in your `MainActivity`.

```kotlin
import com.fireworksdk.bridge.flutter.FWFlutterSDK

class MainActivity: FlutterFragmentActivity() {
  
  override fun attachBaseContext(newBase: Context) {
    super.attachBaseContext(FWFlutterSDK.updateBaseContextLocale(newBase))
  }
}
```

#### Optional: Override the `attachBaseContext` method in your `MainApplication`.

```kotlin
class MainApplication: FlutterApplication() {
  
  override fun attachBaseContext(base: Context) {
    // If you can know the app language here, you could also call FWFlutterSDK.changeLanguage here to sync the app language to SDK. Such as:
    // FWFlutterSDK.changeLanguage("en", base)
    super.attachBaseContext(FWFlutterSDK.updateBaseContextLocale(base))
  }
}
```

#### If you need to support RTL, then please add the following configuration to the `AndroidManifest.xml`

```
    <application
        ...
        android:supportsRtl="true"
    >
    ...
```

### Option 2: Setup steps for `NotRestartingActivity`

**`NotRestartingActivity` option won't restart the Android activity when changing the app language**.

#### Add codes on Android project

To be able to use the in-app language feature, the following configuration needs to be added to the app project.

#### Inherit the `FWFlutterFragmentActivity` class in your `MainActivity`.

```kotlin
import com.fireworksdk.bridge.flutter.base.FWFlutterFragmentActivity

class MainActivity: FWFlutterFragmentActivity()
```

**Configure the theme in your `MainActivity`**

add `android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"` to the file `app/src/Androidmanifest.xml`

{% hint style="info" %}
Besides the `Theme.AppCompat.DayNight.NoActionBar`, you could also use a theme that meets one of the following conditions.

1. Any theme starting with **Theme.AppCompat**
2. Any theme that extends the theme starting with **Theme.AppCompat**
   {% endhint %}

```xml
    <application
        android:name=".MainApplication"
        ...>
        <activity
            android:name=".MainActivity"
            ...
            android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
        </activity>
    </application>
```

**Override the `attachBaseContext` method in your `MainApplication`.**

```kotlin
class MainApplication: FlutterApplication() {
  
  override fun attachBaseContext(base: Context) {
    super.attachBaseContext(FWFlutterSDK.updateBaseContextLocale(base))
  }
}
```

**Set `NotRestartingActivity`** **behavior for language-switching in your `MainApplication`**

```kotlin
class MainApplication: FlutterApplication() {
  
  override fun onCreate() {
    super.onCreate()
    // We suggest setting behavior before you invoke FWFlutterSDK.init()
    FWFlutterSDK.setLanguageSwitchingBehavior(FWSwitchLanguageBehavior.NotRestartingActivity)
    // Invoke FWFlutterSDK.init() for initialize FWFlutterSDK
    ...
  }
}
```

**Add `androidx.appcompat:appcompat` library in `app/firework.gradle`**

<pre class="language-gradle"><code class="lang-gradle"><strong>dependencies {
</strong>  implementation 'androidx.appcompat:appcompat:1.6.1'
  ...
}
...
</code></pre>

#### If you need to support RTL, then please add the following configuration to the `AndroidManifest.xml`

```
    <application
        ...
        android:supportsRtl="true"
    >
    ...
```

## Language support table

| Language                      | Language code |
| ----------------------------- | ------------- |
| English                       | en            |
| Arabic                        | ar            |
| Arabic (Saudi Arabia)         | ar-SA         |
| Arabic (United Arab Emirates) | ar-AE         |
| German                        | de            |
| Italian                       | it            |
| Japanese                      | ja            |
| Polish                        | pl            |
| Portuguese (Brazil)           | pt-BR         |
| Russian                       | ru            |
| Spanish                       | es            |
| Spanish (Mexico)              | es-MX         |
| Spanish (Colombia)            | es-CO         |
| Vietnamese                    | vi            |
| Thailand                      | th            |
| Hungarian                     | hu            |
| Turkish                       | tr            |
| French                        | fr            |
| Portuguese                    | pt            |
| Indonesian                    | id            |
| Swedish                       | sv            |
| Romanian                      | ro            |
| Traditional Chinese (Taiwan)  | zh-TW         |
| Traditional Chinese           | zh-Hant       |


---

# 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/flutter-sdk/integration-guide-v2/app-level-language-setting.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.
