translation-static-analyzer
A library for scanning javscript files to build translation mappings in json automatically.
There are three different concepts for this library.
- The source files which the translations are analyzed and extracted from
src/**/*.js
- The intermediary translations that are meant to be edited by humanes in json5 format
locales/ja.json
- The final translations which strip out untranslated strings and json5 comments and are optionally split between targets.
src/.locales/ja.json
Usage for just translating everything:
const TranslationStaticAnalyzer = ;const analyzer = //Analyzes all javscript files in the src directory files: 'src/**/*.js' //Enables verbose output debug: true //generate a locales/fr.json as well as a locales/en.json locales: 'fr' 'en' //Each page in the folder will get it's own subset of translations target: 'src';analyzer;
Usage for splitting transaltions between dynamically imported pages of a web app:
const TranslationStaticAnalyzer = ;const analyzer = //Analyzes all javscript files in the src directory files: 'src/**/*.js' //Enables verbose output debug: true //generate a locales/fr.json as well as a locales/en.json locales: 'fr' 'en' //Each page in the folder will get it's own subset of translations target: 'src/pages/*';analyzer;
You can incrementally update the written translation (such as for updates triggered by watch) by passing the files that changed: analyzer.update(['/src/index.js']);
Example of translation templates:
// NEW // src/Application/pages/AboutPage/index.js:14 "About": "" // UNUSED "This isn't used anymore": "So the text here doesn't really do anything" // src/Application/pages/AboutPage/index.js:38 "Welcome to the about page!": "ようこそ"
Use the translations with a library like y18n by doing:
const y18n = updateFiles: false;const japanese = ; y18ncache'ja' = japanese;y18n; //This library will add any translations to the json using __() or __n() as the translation function.const translated = y18n;