@vocab/phrase 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -506,6 +506,38 @@ Tags on keys in other languages will be ignored.
506
506
  [tags]: https://support.phrase.com/hc/en-us/articles/5822598372252-Tags-Strings-
507
507
  [configuration]: #Configuration
508
508
 
509
+ #### Global key
510
+
511
+ `vocab push` and `vocab pull` can support global keys mapping. When you want certain translations to use a specific/custom key in Phrase, add the `globalKey` to the structure.
512
+
513
+ ```jsonc
514
+ // translations.json
515
+ {
516
+ "Hello": {
517
+ "message": "Hello",
518
+ "globalKey": "hello"
519
+ },
520
+ "Goodbye": {
521
+ "message": "Goodbye",
522
+ "globalKey": "app.goodbye.label"
523
+ }
524
+ }
525
+ ```
526
+
527
+ In the above example,
528
+
529
+ - `vocab push` will push the `hello` and `app.goodbye.label` keys to Phrase.
530
+ - `vocab pull` will pull translations from Phrase and map them to the `hello` and `app.goodbye.label` keys.
531
+
532
+ ##### Error on no translation for global key
533
+
534
+ By default, `vocab pull` will not error if a translation is missing in Phrase for a translation with a global key.
535
+ If you want to throw an error in this situation, pass the `--error-on-no-global-key-translation` flag:
536
+
537
+ ```sh
538
+ vocab pull --error-on-no-global-key-translation
539
+ ```
540
+
509
541
  ## Troubleshooting
510
542
 
511
543
  ### Problem: Passed locale is being ignored or using en-US instead
@@ -2,6 +2,7 @@ import type { UserConfig } from '@vocab/core';
2
2
  interface PullOptions {
3
3
  branch?: string;
4
4
  deleteUnusedKeys?: boolean;
5
+ errorOnNoGlobalKeyTranslation?: boolean;
5
6
  }
6
- export declare function pull({ branch }: PullOptions, config: UserConfig): Promise<void>;
7
+ export declare function pull({ branch, errorOnNoGlobalKeyTranslation }: PullOptions, config: UserConfig): Promise<void>;
7
8
  export {};
@@ -224,7 +224,8 @@ async function ensureBranch(branch) {
224
224
  }
225
225
 
226
226
  async function pull({
227
- branch = 'local-development'
227
+ branch = 'local-development',
228
+ errorOnNoGlobalKeyTranslation
228
229
  }, config) {
229
230
  trace(`Pulling translations from branch ${branch}`);
230
231
  await ensureBranch(branch);
@@ -251,9 +252,10 @@ async function pull({
251
252
  };
252
253
  const localKeys = Object.keys(defaultValues);
253
254
  for (const key of localKeys) {
255
+ var _defaultValues$key$gl;
254
256
  defaultValues[key] = {
255
257
  ...defaultValues[key],
256
- ...allPhraseTranslations[config.devLanguage][core.getUniqueKey(key, loadedTranslation.namespace)]
258
+ ...allPhraseTranslations[config.devLanguage][(_defaultValues$key$gl = defaultValues[key].globalKey) !== null && _defaultValues$key$gl !== void 0 ? _defaultValues$key$gl : core.getUniqueKey(key, loadedTranslation.namespace)]
257
259
  };
258
260
  }
259
261
 
@@ -269,11 +271,14 @@ async function pull({
269
271
  };
270
272
  const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
271
273
  for (const key of localKeys) {
272
- var _phraseAltTranslation;
273
- const phraseKey = core.getUniqueKey(key, loadedTranslation.namespace);
274
+ var _defaultValues$key$gl2, _phraseAltTranslation;
275
+ const phraseKey = (_defaultValues$key$gl2 = defaultValues[key].globalKey) !== null && _defaultValues$key$gl2 !== void 0 ? _defaultValues$key$gl2 : core.getUniqueKey(key, loadedTranslation.namespace);
274
276
  const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
275
277
  if (!phraseTranslationMessage) {
276
278
  trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
279
+ if (errorOnNoGlobalKeyTranslation && defaultValues[key].globalKey) {
280
+ throw new Error(`Missing translation for global key ${key} in language ${alternativeLanguage}`);
281
+ }
277
282
  continue;
278
283
  }
279
284
  altTranslations[key] = {
@@ -324,7 +329,6 @@ async function push({
324
329
  }
325
330
  } = loadedTranslation;
326
331
  for (const localKey of Object.keys(localTranslations)) {
327
- const phraseKey = core.getUniqueKey(localKey, loadedTranslation.namespace);
328
332
  const {
329
333
  tags = [],
330
334
  ...localTranslation
@@ -332,6 +336,8 @@ async function push({
332
336
  if (language === config.devLanguage) {
333
337
  localTranslation.tags = [...tags, ...sharedTags];
334
338
  }
339
+ const globalKey = loadedTranslation.languages[config.devLanguage][localKey].globalKey;
340
+ const phraseKey = globalKey !== null && globalKey !== void 0 ? globalKey : core.getUniqueKey(localKey, loadedTranslation.namespace);
335
341
  phraseTranslations[language][phraseKey] = localTranslation;
336
342
  }
337
343
  }
@@ -224,7 +224,8 @@ async function ensureBranch(branch) {
224
224
  }
225
225
 
226
226
  async function pull({
227
- branch = 'local-development'
227
+ branch = 'local-development',
228
+ errorOnNoGlobalKeyTranslation
228
229
  }, config) {
229
230
  trace(`Pulling translations from branch ${branch}`);
230
231
  await ensureBranch(branch);
@@ -251,9 +252,10 @@ async function pull({
251
252
  };
252
253
  const localKeys = Object.keys(defaultValues);
253
254
  for (const key of localKeys) {
255
+ var _defaultValues$key$gl;
254
256
  defaultValues[key] = {
255
257
  ...defaultValues[key],
256
- ...allPhraseTranslations[config.devLanguage][core.getUniqueKey(key, loadedTranslation.namespace)]
258
+ ...allPhraseTranslations[config.devLanguage][(_defaultValues$key$gl = defaultValues[key].globalKey) !== null && _defaultValues$key$gl !== void 0 ? _defaultValues$key$gl : core.getUniqueKey(key, loadedTranslation.namespace)]
257
259
  };
258
260
  }
259
261
 
@@ -269,11 +271,14 @@ async function pull({
269
271
  };
270
272
  const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
271
273
  for (const key of localKeys) {
272
- var _phraseAltTranslation;
273
- const phraseKey = core.getUniqueKey(key, loadedTranslation.namespace);
274
+ var _defaultValues$key$gl2, _phraseAltTranslation;
275
+ const phraseKey = (_defaultValues$key$gl2 = defaultValues[key].globalKey) !== null && _defaultValues$key$gl2 !== void 0 ? _defaultValues$key$gl2 : core.getUniqueKey(key, loadedTranslation.namespace);
274
276
  const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
275
277
  if (!phraseTranslationMessage) {
276
278
  trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
279
+ if (errorOnNoGlobalKeyTranslation && defaultValues[key].globalKey) {
280
+ throw new Error(`Missing translation for global key ${key} in language ${alternativeLanguage}`);
281
+ }
277
282
  continue;
278
283
  }
279
284
  altTranslations[key] = {
@@ -324,7 +329,6 @@ async function push({
324
329
  }
325
330
  } = loadedTranslation;
326
331
  for (const localKey of Object.keys(localTranslations)) {
327
- const phraseKey = core.getUniqueKey(localKey, loadedTranslation.namespace);
328
332
  const {
329
333
  tags = [],
330
334
  ...localTranslation
@@ -332,6 +336,8 @@ async function push({
332
336
  if (language === config.devLanguage) {
333
337
  localTranslation.tags = [...tags, ...sharedTags];
334
338
  }
339
+ const globalKey = loadedTranslation.languages[config.devLanguage][localKey].globalKey;
340
+ const phraseKey = globalKey !== null && globalKey !== void 0 ? globalKey : core.getUniqueKey(localKey, loadedTranslation.namespace);
335
341
  phraseTranslations[language][phraseKey] = localTranslation;
336
342
  }
337
343
  }
@@ -212,7 +212,8 @@ async function ensureBranch(branch) {
212
212
  }
213
213
 
214
214
  async function pull({
215
- branch = 'local-development'
215
+ branch = 'local-development',
216
+ errorOnNoGlobalKeyTranslation
216
217
  }, config) {
217
218
  trace(`Pulling translations from branch ${branch}`);
218
219
  await ensureBranch(branch);
@@ -239,9 +240,10 @@ async function pull({
239
240
  };
240
241
  const localKeys = Object.keys(defaultValues);
241
242
  for (const key of localKeys) {
243
+ var _defaultValues$key$gl;
242
244
  defaultValues[key] = {
243
245
  ...defaultValues[key],
244
- ...allPhraseTranslations[config.devLanguage][getUniqueKey(key, loadedTranslation.namespace)]
246
+ ...allPhraseTranslations[config.devLanguage][(_defaultValues$key$gl = defaultValues[key].globalKey) !== null && _defaultValues$key$gl !== void 0 ? _defaultValues$key$gl : getUniqueKey(key, loadedTranslation.namespace)]
245
247
  };
246
248
  }
247
249
 
@@ -257,11 +259,14 @@ async function pull({
257
259
  };
258
260
  const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
259
261
  for (const key of localKeys) {
260
- var _phraseAltTranslation;
261
- const phraseKey = getUniqueKey(key, loadedTranslation.namespace);
262
+ var _defaultValues$key$gl2, _phraseAltTranslation;
263
+ const phraseKey = (_defaultValues$key$gl2 = defaultValues[key].globalKey) !== null && _defaultValues$key$gl2 !== void 0 ? _defaultValues$key$gl2 : getUniqueKey(key, loadedTranslation.namespace);
262
264
  const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
263
265
  if (!phraseTranslationMessage) {
264
266
  trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
267
+ if (errorOnNoGlobalKeyTranslation && defaultValues[key].globalKey) {
268
+ throw new Error(`Missing translation for global key ${key} in language ${alternativeLanguage}`);
269
+ }
265
270
  continue;
266
271
  }
267
272
  altTranslations[key] = {
@@ -312,7 +317,6 @@ async function push({
312
317
  }
313
318
  } = loadedTranslation;
314
319
  for (const localKey of Object.keys(localTranslations)) {
315
- const phraseKey = getUniqueKey(localKey, loadedTranslation.namespace);
316
320
  const {
317
321
  tags = [],
318
322
  ...localTranslation
@@ -320,6 +324,8 @@ async function push({
320
324
  if (language === config.devLanguage) {
321
325
  localTranslation.tags = [...tags, ...sharedTags];
322
326
  }
327
+ const globalKey = loadedTranslation.languages[config.devLanguage][localKey].globalKey;
328
+ const phraseKey = globalKey !== null && globalKey !== void 0 ? globalKey : getUniqueKey(localKey, loadedTranslation.namespace);
323
329
  phraseTranslations[language][phraseKey] = localTranslation;
324
330
  }
325
331
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@vocab/phrase",
3
- "version": "1.2.7",
3
+ "version": "1.3.0",
4
4
  "main": "dist/vocab-phrase.cjs.js",
5
5
  "module": "dist/vocab-phrase.esm.js",
6
6
  "author": "SEEK",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@vocab/core": "^1.3.1",
9
+ "@vocab/core": "^1.5.0",
10
10
  "chalk": "^4.1.0",
11
11
  "csv-stringify": "^6.2.3",
12
12
  "debug": "^4.3.1",