# App-level Language Setting (React Native)

## Use API on JS/TS side

### Call API on JS/TS side

```typescript
import FireworkSDK from 'react-native-firework-sdk';

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

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

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

### Calling sequence

{% hint style="danger" %}
Currently, FireworkSDK.getInstance().changeAppLanguage will recreate the activity 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.

1. Please call `FireworkSDK.getInstance().changeAppLanguage` before `RNRestart.Restart`
2. Please call `FireworkSDK.getInstance().changeAppLanguage` after all your other codes for switching languages, such as the codes of caching the language.

For example:

```typescript
const language = 'ar';

// Caching language should be called before changeAppLanguage API
await AsyncStorage.setItem('app-language', language);

await FireworkSDK.getInstance().changeAppLanguage(language);

// If you use RNRestart.Restart,
// RNRestart.Restart should be called after changeAppLanguage API
RNRestart.Restart();
```

### Additional setup steps on iOS

Firework SDK widgets are based on native views. Hence, as shown in the screenshot below, you need to add the localizations you want to support to the iOS project, which is required by iOS framework.

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

### Additional setup steps on Android

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 `MainActivity`.

```java
import com.fireworksdk.bridge.reactnative.FWReactNativeSDK;

public class MainActivity extends ReactActivity {  
  protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(FWReactNativeSDK.INSTANCE.updateBaseContextLocale(newBase));
  }
}
```

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

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

## Use API on native sides

Generally, you should use changeAppLanguage API on JS/TS side. But we also support using this API on native sides directly.

### Change app language on iOS native side

```swift
import FireworkVideoUI
AppLanguageManager.shared.changeAppLanguage("ar") // such as ar, ar-JO, en, etc.
```

Generally, the `changeAppLanguage` API should be called in the following cases:

1. The App is launched(e.g. in the `application(:, didFinishLaunchingWithOptions:) -> Bool` method).
2. Users change the app language manually.
3. Other cases that change app language.

After calling `changeAppLanguage` API, we need to recreate FireworkVideo SDK components to update the UI. Such as:

* Recreate video feed and story block
* Stop floating player

#### Additional setup steps on iOS

Firework SDK widgets are based on native views. Hence, as shown in the screenshot below, you need to add the localizations you want to support to the iOS project, which is required by iOS framework.

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

### Change app language on Android native side

Step 1: The following methods need to be prepared

```kotlin
  // Some code
  // change language
  fun changeLanguage(inputLocaleString: String, activity: Activity) {
     // save to sp, or save to application or your app cache system
     sharedPreferences.edit().apply {
        putString(LOCALE_KEY, inputLocaleString)
      }.commit()
     restartActivity(activity)
  }
  
  // update the base context locale
  fun updateBaseContextLocale(baseContext: Context): Context {
    // Read from sp or application. If the language/locale exists in your app, use it directly
    val currentLocaleString = sharedPreferences.getString(LOCALE_KEY, null)
    if (currentLocaleStrinotg.isNullOrBlank()) {
      return baseContext
    }
    
    val localeStrings = currentLocaleString.split("-")
    val locale = if (localeStrings.size > 1) {
      Locale(localeStrings[0], localeStrings[1])
    } else {
      Locale(currentLocaleString)
    }
    
    updateDefaultLocale(locale)
    return updateResourcesLocale(baseContext, locale)
  }
  
  // update the default locale
  private fun updateDefaultLocale(locale: Locale) {
    Locale.setDefault(locale)
  }

  // update the context locale
  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)
  }
  
  // restart activity
  private fun restartActivity(activity: Activity) {
    activity.recreate()
  }
```

Step 2: Add the following code to all activities

```kotlin
// Some code
class YourActivity: Activity() {

  ...
  override fun attachBaseContext(newBase: Context) {
    super.attachBaseContext(updateBaseContextLocale(newBase))
  }
}
```

Step 3: Call the change language method where you need

```kotlin
// Some code
// You must call this for all currently live activities
changeLanguage("ar", activity) // "ar-jo", "en-US", "en"
```

## Language support table

<table data-full-width="false"><thead><tr><th width="214">Language</th><th width="84">Language code</th><th width="112">iOS</th><th width="115">iOS translation</th><th>Android</th><th>Android translation</th></tr></thead><tbody><tr><td>English</td><td>en</td><td>Supported</td><td>100%</td><td>Supported</td><td>100%</td></tr><tr><td>Spanish</td><td>es</td><td>Supported</td><td>76%</td><td>Supported</td><td>41.1%</td></tr><tr><td>Portuguese(Brazil)</td><td>pt-BR</td><td>Supported</td><td>58%</td><td>Supported</td><td>41.1%</td></tr><tr><td>Russian</td><td>ru</td><td>Supported</td><td>52%</td><td>Supported</td><td>26.3%</td></tr><tr><td>Polish</td><td>pl</td><td>Supported</td><td>64%</td><td>Supported</td><td>41.1%</td></tr><tr><td>Japanese</td><td>ja</td><td>Supported</td><td>76%</td><td>Supported</td><td>43.2%</td></tr><tr><td>Arabic</td><td>ar</td><td>Supported</td><td>80%</td><td>Supported</td><td>85.3%</td></tr><tr><td>Persian</td><td>fa</td><td>Not supported</td><td>NA</td><td>Supported</td><td>86.3%</td></tr></tbody></table>
