Skip to content

Commit

Permalink
chore: automatic translation registration
Browse files Browse the repository at this point in the history
Automatically load translations
  • Loading branch information
naltatis authored Dec 31, 2024
2 parents 785aeb0 + c6889bb commit 4020b84
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 21 deletions.
12 changes: 2 additions & 10 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import { useFonts } from "expo-font";
import { EvaIconsPack } from "@ui-kitten/eva-icons";
import * as SplashScreen from "expo-splash-screen";
import { decode, encode } from "base-64";
import translations from './i18n';

import translationEN from "./i18n/en.json";
import translationDE from "./i18n/de.json";
import translationLT from "./i18n/lt.json";
if (!global.btoa) {
global.btoa = encode;
}
Expand All @@ -37,14 +35,8 @@ SplashScreen.preventAutoHideAsync();

const Stack = createNativeStackNavigator();

const resources = {
en: { translation: translationEN },
de: { translation: translationDE },
lt: { translation: translationLT },
};

i18n.use(initReactI18next).init({
resources,
resources: translations,
lng: getLocales()[0].languageCode,
fallbackLng: "en",
});
Expand Down
23 changes: 23 additions & 0 deletions i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Resource } from "i18next";

import de from "./de.json";
import en from "./en.json";
import fi from "./fi.json";
import fr from "./fr.json";
import lb from "./lb.json";
import lt from "./lt.json";
import pl from "./pl.json";
import pt from "./pt.json";

const translations: Resource = {
de: { translation: de },
en: { translation: en },
fi: { translation: fi },
fr: { translation: fr },
lb: { translation: lb },
lt: { translation: lt },
pl: { translation: pl },
pt: { translation: pt }
};

export default translations;
190 changes: 182 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"ios": "expo run:ios",
"web": "expo start --web",
"lint": "tsc --noEmit && eslint .",
"format": "prettier --write ."
"format": "prettier --write .",
"translations": "ts-node ./scripts/load-translations.ts"
},
"dependencies": {
"@eva-design/eva": "^2.2.0",
Expand Down Expand Up @@ -53,12 +54,14 @@
"@babel/core": "^7.20.0",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.16.0",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"eslint": "^9.9.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-native": "^4.1.0",
"prettier": "^3.3.3"
"prettier": "^3.3.3",
"ts-node": "^10.9.2"
},
"private": true
}
23 changes: 23 additions & 0 deletions scripts/load-translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'fs';
import path from 'path';

const i18nFolder = "./i18n";
const files = fs.readdirSync(i18nFolder);
const jsonFiles = files.filter(file => file.endsWith('.json')).map(file => path.basename(file, '.json'));


const imports = `import { Resource } from "i18next";\n\n` + jsonFiles.map(lang => `import ${lang} from "./${lang}.json";`).join('\n');

const translationsObject = `const translations: Resource = {
${jsonFiles.map(lang => `${lang}: { translation: ${lang} }`).join(',\n ')}
};
export default translations;
`;


const outputPath = "./i18n/index.ts";
const content = `${imports}\n\n${translationsObject}`;

fs.writeFileSync(outputPath, content, 'utf-8');
console.log('Finished loading translations.');
Loading

0 comments on commit 4020b84

Please sign in to comment.