@@ -38,26 +38,30 @@ now you know what messages you still need to update.
3838yarn add --dev react-intl-translations-manager
3939```
4040
41- or
41+ or
4242
4343```
4444npm i --save-dev react-intl-translations-manager
4545```
4646
4747## Setup
48+
4849### Basic
4950
5051This is an example of the most basic usage of this plugin, in the API documentation below you can find more options.
5152
5253Create a script in your package.json
54+
5355``` json
5456{
5557 "scripts" : {
5658 "manage:translations" : " node ./translationRunner.js"
5759 }
5860}
5961```
62+
6063Create a file with your config you can run with the npm script
64+
6165``` js
6266// translationRunner.js
6367const manageTranslations = require (' react-intl-translations-manager' ).default ;
@@ -68,7 +72,7 @@ const manageTranslations = require('react-intl-translations-manager').default;
6872manageTranslations ({
6973 messagesDirectory: ' src/translations/extractedMessages' ,
7074 translationsDirectory: ' src/translations/locales/' ,
71- languages: [' nl' ], // any language you need
75+ languages: [' nl' ] // any language you need
7276});
7377```
7478
@@ -103,64 +107,74 @@ these messages.
103107
104108#### Config
105109
106- - ` messagesDirectory ` (required),
107- - Directory where the babel plugin puts the extracted messages. This path is
108- relative to your projects root.
109- - example: ` src/locales/extractedMessages `
110- - ` translationsDirectory ` (required),
111- - Directory of the translation files the translation manager needs to maintain.
112- - example: ` src/locales/lang `
113- - ` whitelistsDirectory ` (optional, default: ` translationsDirectory ` )
114- - Directory of the whitelist files the translation manager needs to maintain.
115- These files contain the key of translations that have the exact same text in
116- a specific language as the defaultMessage. Specifying this key will suppress
117- ` unmaintained translation ` warnings.
118- - example: ` Dashboard ` in english is also accepted as a valid translation for
119- dutch.
120- - ` languages ` (optional, default: ` [] ` )
121- - What languages the translation manager needs to maintain. Specifying no
122- languages actually doesn't make sense, but won't break the translationManager
123- either. (Please do not include the default language, react-intl will automatically include it.)
124- - example: for ` ['nl', 'fr'] ` the translation manager will maintain a
125- ` nl.json ` , ` fr.json ` , ` whitelist_nl.json ` and a ` whitelist_fr.json ` file
126- - ` singleMessagesFile ` (optional, default: ` false ` )
127- - Option to output a single JSON file containing the aggregate of all extracted messages,
128- grouped by the file they were extracted from.
129- - example:
110+ * ` messagesDirectory ` (required),
111+ * Directory where the babel plugin puts the extracted messages. This path is
112+ relative to your projects root.
113+ * example: ` src/locales/extractedMessages `
114+ * ` translationsDirectory ` (required),
115+ * Directory of the translation files the translation manager needs to maintain.
116+ * example: ` src/locales/lang `
117+ * ` whitelistsDirectory ` (optional, default: ` translationsDirectory ` )
118+ * Directory of the whitelist files the translation manager needs to maintain.
119+ These files contain the key of translations that have the exact same text in
120+ a specific language as the defaultMessage. Specifying this key will suppress
121+ ` unmaintained translation ` warnings.
122+ * example: ` Dashboard ` in english is also accepted as a valid translation for
123+ dutch.
124+ * ` languages ` (optional, default: ` [] ` )
125+ * What languages the translation manager needs to maintain. Specifying no
126+ languages actually doesn't make sense, but won't break the translationManager
127+ either. (Please do not include the default language, react-intl will automatically include it.)
128+ * example: for ` ['nl', 'fr'] ` the translation manager will maintain a
129+ ` nl.json ` , ` fr.json ` , ` whitelist_nl.json ` and a ` whitelist_fr.json ` file
130+ * ` singleMessagesFile ` (optional, default: ` false ` )
131+ * Option to output a single JSON file containing the aggregate of all extracted messages,
132+ grouped by the file they were extracted from.
133+ * example:
130134 ``` json
131- [
132- {
133- "path" : " src/components/foo.json" ,
134- "descriptors" : [
135- {
136- "id" : " bar" ,
137- "description" : " Text for bar" ,
138- "defaultMessage" : " Bar" ,
139- }
140- ]
141- }
142- ]
135+ [
136+ {
137+ "path" : " src/components/foo.json" ,
138+ "descriptors" : [
139+ {
140+ "id" : " bar" ,
141+ "description" : " Text for bar" ,
142+ "defaultMessage" : " Bar"
143+ }
144+ ]
145+ }
146+ ]
143147 ```
144- - ` detectDuplicateIds ` (optional, default: ` true ` )
145- - If you want the translationManager to log duplicate message ids or not
146- - ` sortKeys ` (optional, default: ` true ` )
147- - If you want the translationManager to sort it's output, both json and console output
148- - ` jsonOptions ` (optional, default: { space: 2, trailingNewline: false })
149- - ` overridePrinters ` (optional, default: {})
150- - Here you can specify custom logging methods. If not specified a default printer is used.
151- - Possible printers to configure:
148+ * ` detectDuplicateIds ` (optional, default: ` true ` )
149+ * If you want the translationManager to log duplicate message ids or not
150+ * ` sortKeys ` (optional, default: ` true ` )
151+ * If you want the translationManager to sort it's output, both json and console output
152+ * ` jsonOptions ` (optional, default: { space: 2, trailingNewline: false })
153+ * ` overridePrinters ` (optional, default: {})
154+ * Here you can specify custom logging methods. If not specified a default printer is used.
155+ * Possible printers to configure:
152156 ``` js
153- const printers = {
154- printDuplicateIds : ( duplicateIds ) => { console .log (` You have ${ duplicateIds .length } duplicate IDs` ) },
155- printLanguageReport : ( report ) => { console .log (' Log report for a language' ) },
156- printNoLanguageFile : ( lang ) => { console .log (` No existing ${ lang} translation file found. A new one is created.` ) },
157- printNoLanguageWhitelistFile : ( lang ) => { console .log (` No existing ${ lang} file found. A new one is created.` ) },
158- };
157+ const printers = {
158+ printDuplicateIds : duplicateIds => {
159+ console .log (` You have ${ duplicateIds .length } duplicate IDs` );
160+ },
161+ printLanguageReport : report => {
162+ console .log (' Log report for a language' );
163+ },
164+ printNoLanguageFile : lang => {
165+ console .log (
166+ ` No existing ${ lang} translation file found. A new one is created.`
167+ );
168+ },
169+ printNoLanguageWhitelistFile : lang => {
170+ console .log (` No existing ${ lang} file found. A new one is created.` );
171+ }
172+ };
159173 ```
160- - ` overrideCoreMethods ` (optional, default: {})
161- - Here you can specify overrides for the core hooks. If not specified, the
162- default methods will be used.
163- - Possible overrides to configure:
174+ * ` overrideCoreMethods ` (optional, default: {})
175+ * Here you can specify overrides for the core hooks. If not specified, the
176+ default methods will be used.
177+ * Possible overrides to configure:
164178 ``` js
165179 const overrideCoreMethods = {
166180 provideExtractedMessages : () => {},
@@ -171,8 +185,8 @@ these messages.
171185 provideTranslationsFile : () => {},
172186 provideWhitelistFile : () => {},
173187 reportLanguage : () => {},
174- afterReporting : () => {},
175- }
188+ afterReporting : () => {}
189+ };
176190 ```
177191
178192#### Fully configured
@@ -192,13 +206,23 @@ manageTranslations({
192206 sortKeys: false ,
193207 jsonOptions: {
194208 space: 4 ,
195- trailingNewline: true ,
209+ trailingNewline: true
196210 },
197211 overridePrinters: {
198- printDuplicateIds : ( duplicateIds ) => { console .log (` You have ${ duplicateIds .length } duplicate IDs` ) },
199- printLanguageReport : ( report ) => { console .log (' Log report for a language' ) },
200- printNoLanguageFile : ( lang ) => { console .log (` No existing ${ lang} translation file found. A new one is created.` ) },
201- printNoLanguageWhitelistFile : ( lang ) => { console .log (` No existing ${ lang} file found. A new one is created.` ) },
212+ printDuplicateIds : duplicateIds => {
213+ console .log (` You have ${ duplicateIds .length } duplicate IDs` );
214+ },
215+ printLanguageReport : report => {
216+ console .log (' Log report for a language' );
217+ },
218+ printNoLanguageFile : lang => {
219+ console .log (
220+ ` No existing ${ lang} translation file found. A new one is created.`
221+ );
222+ },
223+ printNoLanguageWhitelistFile : lang => {
224+ console .log (` No existing ${ lang} file found. A new one is created.` );
225+ }
202226 },
203227 overrideCoreMethods: {
204228 provideExtractedMessages : () => {},
@@ -209,12 +233,12 @@ manageTranslations({
209233 provideTranslationsFile : () => {},
210234 provideWhitelistFile : () => {},
211235 reportLanguage : () => {},
212- afterReporting : () => {},
213- },
236+ afterReporting : () => {}
237+ }
214238});
215239```
216240
217- * This config is only as illustration for all possible options, these arent
241+ \ * This config is only as illustration for all possible options, these arent
218242recommended configuration options.
219243
220244### CoreMethods
@@ -223,48 +247,54 @@ These are the core methods of the translationManager and what purpose they
223247serve.
224248
225249#### provideExtractedMessages
250+
226251``` js
227252const extractedMessages = provideExtractedMessages ();
228253```
229254
230255Here you should return all extracted messages. This should be an array, with an object per file. Each object should at least contain a ` descriptors ` key which in turn has an array of message objects. Each message object should at least contain the id and message.
231256Example:
257+
232258``` js
233259// Minimal expected return value
234260const extractedMessages = [
235261 {
236262 descriptors: [
237263 {
238264 id: ' foo_ok' ,
239- defaultMessage: ' OK' ,
240- },
241- ],
242- },
265+ defaultMessage: ' OK'
266+ }
267+ ]
268+ }
243269];
244270```
245271
246272#### outputSingleFile
273+
247274``` js
248275outputSingleFile (extractedMessages);
249276```
250277
251278This gives you the option to output the extractedMessages. This way you can for example shrink all extracted files into a single File containing all messages.
252279
253280#### outputDuplicateKeys
281+
254282``` js
255283outputDuplicateKeys (duplicateIds);
256284```
257285
258286This gives you the option to warn for duplicate ids.
259287
260288#### beforeReporting
289+
261290``` js
262291beforeReporting ();
263292```
264293
265294Here you can do the preparation of the reporting, like creating the necessary folders, or printing a start message
266295
267296#### provideLangTemplate
297+
268298``` js
269299const languageResults = provideLangTemplate (lang);
270300```
@@ -273,51 +303,57 @@ Here you should provide the template for the language results. This is just a ba
273303The following keys are restricted and will be overridden by the core: ` report ` , ` noTranslationFile ` and ` noWhitelistFile ` .
274304
275305#### provideTranslationsFile
306+
276307``` js
277308const translationsFile = provideTranslationsFile (languageResults);
278309```
279310
280311Here you should return the translations for the specified language. This must be an object with the message id and message in a key value format.
312+
281313``` js
282314const translationsFile = {
283- messageId: ' message' ,
315+ messageId: ' message'
284316};
285317```
286318
287319#### provideWhitelistFile
320+
288321``` js
289322const whitelistFile = provideWhitelistFile (languageResults);
290323```
291324
292325Here you should return the whitelisted messsage ids for the specified language. This must be an array of strings.
326+
293327``` js
294- const whitelistFile = [
295- ' messageId' ,
296- ];
328+ const whitelistFile = [' messageId' ];
297329```
298330
299331#### reportLanguage
332+
300333``` js
301- reportLanguage (languageResults)
334+ reportLanguage (languageResults);
302335```
303336
304337Here you can handle the reporting of the results for a language, like logging and creating files based on the results.
305338
306339#### afterReporting
340+
307341``` js
308- afterReporting ()
342+ afterReporting ();
309343```
310344
311345Here you can do actions after all reports are made, like cleanup or printing a finished message.
312346
313347### readMessageFiles
348+
314349``` js
315350const extractedMessages = readMessageFiles (messagesDirectory);
316351```
317352
318353This is a ` babel-plugin-react-intl ` specific helper method. It will read all extracted JSON file for the specified directory, filter out all files without any messages, and output an array with all messages.
319354
320355Example output:
356+
321357``` js
322358const extractedMessages = [
323359 {
@@ -326,26 +362,28 @@ const extractedMessages = [
326362 {
327363 id: ' foo_ok' ,
328364 description: ' Ok text' ,
329- defaultMessage: ' OK' ,
330- },
331- ],
332- },
365+ defaultMessage: ' OK'
366+ }
367+ ]
368+ }
333369];
334370```
335371
336372### createSingleMessagesFile
373+
337374``` js
338375createSingleMessagesFile ({ messages, directory });
339376```
340377
341378This helper method will output all messages (potentially read by ` readMessageFiles ` ) in a single jsonFile.
342379
343- - messages: (required)
344- - directory: (required, string) contains the path to the directory where the file should be written into.
345- - fileName: (optional, default: ` defaultMessages.json ` ) this filename should contain the ` .json ` extension
346- - jsonSpaceIndentation: (optional, default: ` 2 ` ) number of spaces used for indentation (0-10)
380+ * messages: (required)
381+ * directory: (required, string) contains the path to the directory where the file should be written into.
382+ * fileName: (optional, default: ` defaultMessages.json ` ) this filename should contain the ` .json ` extension
383+ * jsonSpaceIndentation: (optional, default: ` 2 ` ) number of spaces used for indentation (0-10)
347384
348385### getDefaultMessages
386+
349387``` js
350388const messages = getDefaultMessages (extractedMessages);
351389```
@@ -355,7 +393,7 @@ This helper method will flatten all files (as returned from `readMessageFiles`)
355393``` js
356394const messages = {
357395 messages: {
358- messageId: ' message' ,
396+ messageId: ' message'
359397 },
360398 duplicateIds: [
361399 // potentially double used message keys,
0 commit comments