App-level Language Setting (Flutter)

Dart integration

import 'package:fw_flutter_sdk/fw_flutter_sdk.dart';

// You should pass language code to this API, such as "en", "ar" and "pt-BR".
FireworkSDK.getInstance().changeAppLanguage("ar");

Please refer to the Language support table to learn the languages we supported.

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.

Additional setup steps on Android

On Android, we have two behaviors for switching languages: RestartingActivity and NotRestartingActivity. Different behaviors have different setup steps. You could choose one of them.

Option 1: Setup steps for RestartingActivity

Calling sequence

Currently, FireworkSDK.getInstance().changeAppLanguage will recreate the activity by default on Android. Therefore, we need to follow the calling sequence.

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:

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.

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.

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

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.

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

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

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

Override the attachBaseContext method in your MainApplication.

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

Set NotRestartingActivity behavior for language-switching in your MainApplication

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

dependencies {
  implementation 'androidx.appcompat:appcompat:1.6.1'
  ...
}
...

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

Last updated