Template
Initial commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"i18next": "latest",
|
||||
"react-i18next": "latest"
|
||||
},
|
||||
"expoDependencies": ["expo-localization"],
|
||||
"appJson": {
|
||||
"expo": {
|
||||
"plugins": ["expo-localization"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
import './lang/translation';
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Resource } from 'i18next';
|
||||
|
||||
export const fallbackChecker = (resources: Resource, fallbackLng: string) => {
|
||||
const languages = Object.keys(resources);
|
||||
const hasFallback = languages.find((key) => fallbackLng === key);
|
||||
|
||||
if (!hasFallback) {
|
||||
throw new Error(
|
||||
`fallbackLng "${fallbackLng}", is not present in your resources, please check your config, languages available: ${languages.join(', ')}`
|
||||
);
|
||||
}
|
||||
return fallbackLng;
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
/* eslint-disable import/no-named-as-default-member */
|
||||
import i18n, { Resource } from 'i18next';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
|
||||
import { fallbackChecker } from './fallbackChecker';
|
||||
import { languageDetector } from './languageDetector';
|
||||
|
||||
type Init18n = {
|
||||
resources: Resource;
|
||||
fallbackLng: string;
|
||||
};
|
||||
|
||||
export const init18n = ({ resources, fallbackLng }: Init18n) => {
|
||||
return i18n
|
||||
.use(languageDetector)
|
||||
.use(initReactI18next)
|
||||
.init({
|
||||
resources,
|
||||
fallbackLng: fallbackChecker(resources, fallbackLng),
|
||||
compatibilityJSON: 'v3', // By default React Native projects does not support Intl
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
import * as Localization from 'expo-localization';
|
||||
import { LanguageDetectorModule } from 'i18next';
|
||||
|
||||
export const languageDetector: LanguageDetectorModule = {
|
||||
type: 'languageDetector',
|
||||
detect: () => {
|
||||
const locales = Localization.getLocales();
|
||||
const firstLanguageCode = locales[0].languageCode ?? 'nl';
|
||||
return firstLanguageCode;
|
||||
},
|
||||
init: () => {},
|
||||
cacheUserLanguage: () => {},
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import { init18n } from 'lang/i18n/init';
|
||||
import nl from './nl';
|
||||
|
||||
export const resources = {
|
||||
nl: {
|
||||
translation: nl,
|
||||
},
|
||||
};
|
||||
|
||||
export const fallbackLng = 'nl';
|
||||
|
||||
export type LanguageCode = keyof typeof resources;
|
||||
|
||||
const i18n = init18n({ resources, fallbackLng });
|
||||
|
||||
export default i18n;
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1,5 @@
|
||||
import general from './general.json';
|
||||
|
||||
export default {
|
||||
general,
|
||||
};
|
||||
Reference in New Issue
Block a user