|
1 | | -# babel-plugin-intlized-components |
2 | | -:earth_americas: Babel plugin for intlized-components |
| 1 | +# 🌎 babel-plugin-intlized-components |
| 2 | + |
| 3 | +Babel plugin for package [intlized-components](https://github.com/ProAI/intlized-components). The primary use case for this plugin is to extract message ids and default messages from all files of a project. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```shell |
| 8 | +npm install babel-plugin-intlized-components |
| 9 | +# or |
| 10 | +yarn add babel-plugin-intlized-components |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Add the plugin to your babel configuration: |
| 16 | + |
| 17 | +```json |
| 18 | +{ |
| 19 | + "plugins": ["babel-plugin-styled-components"] |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Message extraction |
| 24 | + |
| 25 | +```javascript |
| 26 | +import babel from '@babel/core'; |
| 27 | + |
| 28 | +// Define fileName and babelConfig variables here. |
| 29 | + |
| 30 | +const { metadata: result } = babel.transformFileSync(fileName, babelConfig); |
| 31 | +const { translations } = result['intlized-components']; |
| 32 | + |
| 33 | +// Do something with the translations, e.g. save them in file. |
| 34 | +``` |
| 35 | + |
| 36 | +## Docs |
| 37 | + |
| 38 | +### Options |
| 39 | + |
| 40 | +#### `customImportKey` |
| 41 | + |
| 42 | +If you don't import `createDict` from `intlized-components` directly, you can set a custom import key in the babel plugin, so that `createDict` is detected anyway. |
| 43 | + |
| 44 | +Example: |
| 45 | + |
| 46 | +In babel configuration: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "plugins": [ |
| 51 | + [ |
| 52 | + "babel-plugin-styled-components", |
| 53 | + { "customImportKey": "my-custom-import" } |
| 54 | + ] |
| 55 | + ] |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +In a file: |
| 60 | + |
| 61 | +```javascript |
| 62 | +import { createDict } from 'my-custom-import'; |
| 63 | + |
| 64 | +const dict = createDict(...); |
| 65 | +``` |
| 66 | + |
| 67 | +#### `autoResolveKey` |
| 68 | + |
| 69 | +Normally you have to set a key as the first parameter and a message object as the second parameter for `createDict`. If you set this option to the base path of your application, the babel plugin will derive the first parameter from the file name, so that you can call `createDict` with a message object only. |
| 70 | + |
| 71 | +Example: |
| 72 | + |
| 73 | +Without `autoResolveKey` option: |
| 74 | + |
| 75 | +```javascript |
| 76 | +const dict = createDict('welcome.Welcome', { |
| 77 | + hello: 'Hello', |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +With `autoResolveKey` option: |
| 82 | + |
| 83 | +In babel configuration: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "plugins": [ |
| 88 | + ["babel-plugin-styled-components", { "autoResolveKey": "path-to-my-app" }] |
| 89 | + ] |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +In file `welcome/Welcome.js`: |
| 94 | + |
| 95 | +```javascript |
| 96 | +// Babel will transform createDict and will add the key "welcome.Welcome" as first parameter to the function call. |
| 97 | +const dict = createDict({ |
| 98 | + hello: 'Hello', |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +_Hint: You can even set a key manually if this option is on, so you can mix up auto resolved and manually set keys._ |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +This package is released under the [MIT License](LICENSE). |
0 commit comments