|
| 1 | +# React-intl-translations-manager |
| 2 | + |
| 3 | +## Installing |
| 4 | + |
| 5 | +``` |
| 6 | +npm install --save-dev react-intl-translations-manager |
| 7 | +``` |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Since you need the `babel-plugin-react-intl` to extract the messages, I'll assume you're using babel in your project. |
| 12 | + |
| 13 | +This is an example of the most basic usage of this plugin, in the API documentation below you can find more options. |
| 14 | + |
| 15 | +Create a script in your package.json |
| 16 | +```json |
| 17 | +{ |
| 18 | + "scripts": { |
| 19 | + "manage:translations": "babel-node ./translationRunner.js" |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | +Create a file with your config you can run with the npm script |
| 24 | +```js |
| 25 | +// translationRunner.js |
| 26 | +import { createTranslationManager } from 'react-intl-translations-manager'; |
| 27 | + |
| 28 | +const translationManager = createTranslationManager({ |
| 29 | + messagesDirectory: 'src/translations/extractedMessages', |
| 30 | + translationsDirectory: 'src/translations/locales/', |
| 31 | + languages: ['nl'], // any language you need |
| 32 | +}); |
| 33 | + |
| 34 | +translationManager.run(); |
| 35 | +``` |
| 36 | + |
| 37 | +Run the translation manager with your new npm script |
| 38 | + |
| 39 | +``` |
| 40 | +npm run manage:translations |
| 41 | +``` |
| 42 | + |
| 43 | +## API |
| 44 | + |
| 45 | +### createTranslationManager |
| 46 | + |
| 47 | +This will create a new translationManager based on the config you gave in. On |
| 48 | +itself this won't do anything unless you run any of the commands below. |
| 49 | + |
| 50 | +#### Config |
| 51 | + |
| 52 | +- `messagesDirectory` (required), |
| 53 | + - Directory where the babel plugin puts the extracted messages. This path is |
| 54 | + relative to your projects root. |
| 55 | + - example: `src/locales/extractedMessages` |
| 56 | +- `translationsDirectory` (required), |
| 57 | + - Directory of the translation files the translation manager needs to maintain. |
| 58 | + - example: `src/locales/lang` |
| 59 | +- `singleMessagesFile` (optional, default: `false`) |
| 60 | + - Option to output a single file containing the aggregate of all extracted messages, |
| 61 | + grouped by the file they were extracted from. |
| 62 | + - example: |
| 63 | + ```json |
| 64 | + [ |
| 65 | + { |
| 66 | + "path": "src/components/foo.json", |
| 67 | + "descriptors": [ |
| 68 | + { |
| 69 | + "id": "bar", |
| 70 | + "description": "Text for bar", |
| 71 | + "defaultMessage": "Bar", |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + ] |
| 76 | + ``` |
| 77 | +- `whitelistsDirectory` (optional, default: `translationsDirectory`) |
| 78 | + - Directory of the whitelist files the translation manager needs to maintain. |
| 79 | + These files contain the key of translations that have the exact same text in a specific language as the defaultMessage. Specifying this key will suppress |
| 80 | + `unmaintained translation` warnings. |
| 81 | + - example: `Dashboard` in english is also accepted as a valid translation for |
| 82 | + dutch. |
| 83 | +- `languages` (optional, default: `[]`) |
| 84 | + - What languages the translation manager needs to maintain. Specifying no |
| 85 | + languages actually doesn't make sense, but won't break the translationManager |
| 86 | + either. |
| 87 | + - example: for `['nl', 'fr']` the translation manager will maintain a `nl.json`, `fr.json`, `whitelist_nl.json` and a `whitelist_fr.json` file |
| 88 | +- `detectDuplicateIds` (optional, default: `true`) |
| 89 | + - If you want the translationManager to detect duplicate message ids or not |
| 90 | + |
| 91 | +### run |
| 92 | + |
| 93 | +This will maintain all translation files. Based on your config you will get output for duplicate ids, and per specified language you will get the deleted translations, added messages (new messages that need to be translated), and not translated messages. |
0 commit comments