Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from 'react';

import React from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import { ReactNativeLanguageDetector } from 'react-native-localization-settings';
import i18next from 'i18next';
import { initReactI18next, useTranslation } from 'react-i18next';
import {
getLanguage,
getLanguageAsync,
ReactNativeLanguageDetector,
} from 'react-native-localization-settings';

i18next
.use(ReactNativeLanguageDetector)
Expand Down Expand Up @@ -35,21 +38,26 @@ i18next

export default function App() {
const { t } = useTranslation();
const [lang, setLang] = React.useState(getLanguage());

const changeLanguage = (language: string) => () => {
i18next.changeLanguage(language);
setLang(getLanguage());
};
return (
<View style={styles.container}>
<Text>{t('key')}</Text>
<Text>{lang}</Text>
<Button title={'change to pl'} onPress={changeLanguage('pl-PL')} />
<Button title={'change to en'} onPress={changeLanguage('en-US')} />
<Button title={'change to fr'} onPress={changeLanguage('fr-FR')} />
<Button
title={'change to pl'}
onPress={() => i18next.changeLanguage('pl-PL')}
/>
<Button
title={'change to en'}
onPress={() => i18next.changeLanguage('en-US')}
title={'get language sync'}
onPress={() => console.log(getLanguage())}
/>
<Button
title={'change to fr'}
onPress={() => i18next.changeLanguage('fr-FR')}
title={'get language async'}
onPress={() => getLanguageAsync().then(console.log)}
/>
</View>
);
Expand Down
8 changes: 6 additions & 2 deletions example/ios/Localizable.xcstrings
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"sourceLanguage" : "en",
"strings" : {},
"strings" : {
"Key" : {
"extractionState" : "manual"
}
},
"version" : "1.0"
}
}
24 changes: 22 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,36 @@ const LocalizationSettings = LocalizationSettingsModule
);

/**
* Get language
* Get language (sync)
* @returns Language in IETF BCP 47 format (like 'en-US')
* @example
* console.log(getLanguage())
*/
export function getLanguage(): string {
LocalizationSettings.getLanguage();
return LocalizationSettings.getConstants().language.split('_')[0];
}

/**
* Get language (async)
* @param fallback - fallback language
* @returns Promise with Language in IETF BCP 47 format (like 'en-US')
* @example
* getLanguageAsync().then(console.log)
*/
export function getLanguageAsync(fallback?: string): Promise<string> {
return LocalizationSettings.getLanguage()
.then((res: string) => res.split('_'))
.then((res: string[]) => {
if (res[0]) {
return res[0];
}
if (fallback) {
return fallback;
}
throw new Error('Invalid language format');
});
}

/**
* Set language
* @param language - locale string
Expand Down
Loading