Translation files
Node utility tool to extract translation files from typescript source code and output them in a human readable format so they can be sent to translation services.
Install
You can find this package from npm
npm install translation-files
Usage
// src/todo-list/translations.tsconst translations = todo: key: "page.title.todo" default: "Todo" description: "Displayed as page title" completed: key: "todo-list.completed.message" default: "You have finished {0}/{1} items of your todo list." 0: "Number of finished todo items" 1: "Number of unfinished todo items"
// src/todo-list/translations.tsx; const TodoList = getTranslationByKey: // this part you need to provide yourself :) string; const todoText = ; let completedText = ; const totalTodos = 10; const completedTodos = 3; // dummy function to replace {0} and {1} with values completedText = ; return <> <header> <h1>todoText</h1> </header> <div className="todo-list"> ... <p>{} </div> </> ;);
You can use it as follows. In your package.json
, add a new script to run:
"scripts": {
"translations": "translation-files",
},
Then when you run npm run translations
it will output a translation file with default settings.
Example of the output for the translations.ts file listed above:
key: page.title.tododescription: Displayed as page titledefault: Todo key: todo-list.completed.messagedefault: You have finished {0}/{1} items of your todo list. 0: Number of finished todo items 1: Number of unfinished todo items
You can modify the npm script to your needs using the following cli options:
Usage: translation-files [options] Options: --pattern [globPattern] Pattern to used to find translation files. --outFile [file] File where translations should be saved to. --exportName [var] Name of exported root variable used in each source translation file. --keyName [prop] Name of property containing the key
Javascript API
This package can also be used by it's javascript API.
const extractTranslations = ; // with default settings:; // or with custom settings:;