https://github.com/thdk/translation-files
Extract all translation files from typescript source code. Clean output format that can be used by translators.
https://github.com/thdk/translation-files
Last synced: 9 months ago
JSON representation
Extract all translation files from typescript source code. Clean output format that can be used by translators.
- Host: GitHub
- URL: https://github.com/thdk/translation-files
- Owner: thdk
- Created: 2020-11-04T13:36:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-06T18:45:50.000Z (over 5 years ago)
- Last Synced: 2025-08-28T22:02:59.211Z (9 months ago)
- Language: TypeScript
- Size: 98.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://www.npmjs.com/package/translation-files)
`npm install translation-files`
## Usage
```javascript
// src/todo-list/translations.ts
export const 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",
}
}
```
```javascript
// src/todo-list/translations.tsx
import { translations } from "./translations";
export const TodoList = ({
getTranslationByKey,
}: {
// this part you need to provide yourself :)
getTranslationByKey: (key: string) => string;
}) => {
const todoText = getTranslationByKey(
translations.todo,
);
let completedText = getTranslationByKey(
translations.completed,
);
const totalTodos = 10;
const completedTodos = 3;
// dummy function to replace {0} and {1} with values
completedText = format(
completedText,
completedTodos,
totalTodos,
);
return (
<>
{todoText}
...
{}
>
);
});
```
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:
```sh
key: page.title.todo
description: Displayed as page title
default: Todo
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
```
You can modify the npm script to your needs using the following cli options:
```shell
Usage: translation-files [options]
Options:
--pattern [globPattern] Pattern (glob) to used to find translation files. (default: "**/**/translations.ts")
--outFile [file] File where translations should be saved to.
--exportName [var] Name of exported root variable used in each source translation file. (default: "translations")
--keyName [prop] Name of property containing the key for translation (default: "key")
--cwd [cwd] Current working directory
-h, --help display help for command
```
**Javascript API**
This package can also be used by it's javascript API.
```javascript
const {
extractTranslations,
} = require('translation-files');
// with default settings:
extractTranslations();
// or with custom settings:
extractTranslations({
globOptions: {
cwd: __dirname, // default process.cwd()
},
globPattern, // default "**/**/translations.ts"
processOptions: {
translationKeyProperty: "label", // default "key"
translationExport: "labels", // default "translations"
},
outFile: "dist/labels.txt", // default undefined (output in console)
});
```