AWS Amplify I18n - how to get current set language?

Viewed 730

I'm trying to output the currently set language from the I18n object but there doesn't seem to be a way to do this.

It would be great if there were something like I18n.getCurrentLang() but there is nothing like this and i can't find anything in the source code class.

Thanks in advance!

1 Answers

It is either a bug or a feature, library only checks for window havigator to automatically set, in the mobile app it will de undefined

//class I18n 

    if (
        !this._lang &&
        typeof window !== 'undefined' &&
        window &&
        window.navigator
    ) {
        this._lang = window.navigator.language;
    } else {

    }

I use a custom function to determine that. It will return Amplify formated locale code.

import { Platform, NativeModules } from 'react-native'

export default function getLocale() {
    let locale= 'en';
    try{
        locale = Platform.OS === 'ios'
            ? NativeModules.SettingsManager.settings.AppleLocale ||
            NativeModules.SettingsManager.settings.AppleLanguages[0] //iOS 13
            : NativeModules.I18nManager.localeIdentifier;
        //return format en_US
        locale = locale.split("_")[0];
    } catch (e) {
        console.error("Could not split locale. Falling back to en");
        locale = 'en';
    }
    return locale;
}
Related