Open wyzzy opened 6 years ago
This library is just a small layer over https://www.npmjs.com/package/@types/i18n-js
You might want to use them directly
@wyzzy I actually ended up not using the library and now I am using 'i18n-js' directly this is how my "i18n.ts" looks like:
import I18n from 'i18n-js';
import Expo from 'expo';
// Import all locales
import en from './en.json';
import de from './de.json';
export async function initI18n() {
// Should the app fallback to English if user locale doesn't exists
I18n.fallbacks = true;
// Define the supported translations
I18n.translations = {
en,
de
};
const deviceLocale = await Expo.Util.getCurrentLocaleAsync();
//since it is en-GB or de-De, take only first 2 letters
I18n.locale = deviceLocale.substring(0, 2);
const currentLocale = I18n.currentLocale();
// Is it a RTL language?
const isRTL = currentLocale.indexOf('he') === 0 || currentLocale.indexOf('ar') === 0;
// Allow RTL alignment in RTL languages
ReactNative.I18nManager.allowRTL(isRTL);
}
// The method we'll use instead of a regular string
export function getString(name: string, params = {}) {
return I18n.t(name, params);
};
export default I18n;
Thanks @sheva007 - your solution will only work in an Expo app though. The reason I'm interested in this library is I have to integrate RN functionality into an existing native app, and this module provides the RN bridge functionality to get the device's current locale. Actually that is all this library does, other than also wrapping i18n-js, so I can see that if you're using Expo, you don't actually need this module.
I'm just not bothering with typings for this small module, and it's working fine.
@wyc777 This is not the purpose of this library, this is the purpose of https://github.com/react-community/react-native-languages and of the PR I made: https://github.com/facebook/react-native/pull/14568 (unfortunatelly not merged)
Please consider using this instead if you want more granular control. You can install react-native-languages
+ i18n-js
+ @types/i18n-js
. See how here: https://github.com/AlexanderZaytsev/react-native-i18n/issues/128#issuecomment-356980889
You can install @types/i18n-js
and then create d.ts
file in your project with following declaration:
declare module 'react-native-i18n' {
import * as I18n from 'i18n-js';
export default I18n;
export const getLanguages: () => Promise<string[]>;
}
From here it's one step to PR with definitions.
This library seems to be the leading option for localising React Native apps. Would be great if it included TypeScript definitions as well! Any plans to add them?