@vocab/core 1.4.0 → 1.6.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
@@ -29,7 +29,7 @@ $ npm i --save @vocab/core @vocab/react
29
29
 
30
30
  ### Step 2: Configure Vocab
31
31
 
32
- You can configure Vocab directly when calling the API or via a `vocab.config.js` file.
32
+ You can configure Vocab directly when calling the API or via a `vocab.config.js` or `vocab.config.cjs` file.
33
33
 
34
34
  In this example we've configured two languages, English and French, where our initial `translation.json` files will use English.
35
35
 
@@ -184,7 +184,7 @@ t('my key with component', {
184
184
 
185
185
  ## Configuration
186
186
 
187
- Configuration can either be passed into the Node API directly or be gathered from the nearest _vocab.config.js_ file.
187
+ Configuration can either be passed into the Node API directly or be gathered from the nearest _vocab.config.js_ or _vocab.config.cjs_ file.
188
188
 
189
189
  **vocab.config.js**
190
190
 
@@ -341,7 +341,7 @@ functionality.
341
341
 
342
342
  ### Generating a pseudo-localized language using Vocab
343
343
 
344
- Vocab can generate a pseudo-localized language via the [`generatedLanguages` config][generated languages config], either via the webpack plugin or your `vocab.config.js` file.
344
+ Vocab can generate a pseudo-localized language via the [`generatedLanguages` config][generated languages config], either via the webpack plugin or your `vocab.config.js` or `vocab.config.cjs` file.
345
345
  `@vocab/pseudo-localize` exports a `generator` that can be used directly in your config.
346
346
 
347
347
  **vocab.config.js**
@@ -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
@@ -1,4 +1,4 @@
1
- import type { LoadedTranslation, UserConfig } from './types';
1
+ import type { LoadedTranslation, UserConfig } from "./types.js";
2
2
  export declare function generateRuntime(loadedTranslation: LoadedTranslation): Promise<void>;
3
3
  export declare function watch(config: UserConfig): () => Promise<void>;
4
4
  export declare function compile({ watch: shouldWatch }: {
@@ -1,4 +1,4 @@
1
- import type { UserConfig } from './types';
1
+ import type { UserConfig } from "./types.js";
2
2
  export declare function validateConfig(c: UserConfig): boolean;
3
3
  export declare function resolveConfig(customConfigFilePath?: string): Promise<UserConfig | null>;
4
4
  export declare function resolveConfigSync(customConfigFilePath?: string): UserConfig | null;
@@ -1,2 +1,2 @@
1
- import type { ParsedICUMessages, TranslationMessagesByKey } from './types';
1
+ import type { ParsedICUMessages, TranslationMessagesByKey } from "./types.js";
2
2
  export declare const getParsedICUMessages: (m: TranslationMessagesByKey, locale: string) => ParsedICUMessages<any>;
@@ -1,6 +1,6 @@
1
- export { compile, watch } from './compile';
2
- export { validate } from './validate';
3
- export { resolveConfig, resolveConfigSync, validateConfig } from './config';
4
- export { getAltLanguages, getAltLanguageFilePath, getDevLanguageFileFromTsFile, } from './utils';
5
- export { getUniqueKey, loadAllTranslations, loadTranslation, } from './load-translations';
6
- export * from './types';
1
+ export { compile, watch } from "./compile.js";
2
+ export { validate } from "./validate/index.js";
3
+ export { resolveConfig, resolveConfigSync, validateConfig } from "./config.js";
4
+ export { getAltLanguages, getAltLanguageFilePath, getDevLanguageFileFromTsFile, } from "./utils.js";
5
+ export { getUniqueKey, loadAllTranslations, loadTranslation, } from "./load-translations.js";
6
+ export * from "./types.js";
@@ -1,5 +1,5 @@
1
- import type { TranslationsByKey, UserConfig, LoadedTranslation, LanguageTarget } from './types';
2
- import { type Fallback } from './utils';
1
+ import type { TranslationsByKey, UserConfig, LoadedTranslation, LanguageTarget } from "./types.js";
2
+ import { type Fallback } from "./utils.js";
3
3
  export declare function getUniqueKey(key: string, namespace: string): string;
4
4
  export declare function mergeWithDevLanguageTranslation({ translation, devTranslation, }: {
5
5
  translation: TranslationsByKey;
@@ -1,3 +1,3 @@
1
- import type { TranslationModule, TranslationMessagesByKey } from './types';
2
- export { createTranslationFile } from './translation-file';
1
+ import type { TranslationModule, TranslationMessagesByKey } from "./types.js";
2
+ export { createTranslationFile } from "./translation-file.js";
3
3
  export declare const createLanguage: (module: TranslationMessagesByKey) => TranslationModule<any>;
@@ -1,2 +1,2 @@
1
- import type { TranslationModuleByLanguage, LanguageName, ParsedFormatFnByKey, TranslationFile } from './types';
1
+ import type { TranslationModuleByLanguage, LanguageName, ParsedFormatFnByKey, TranslationFile } from "./types.js";
2
2
  export declare function createTranslationFile<Language extends LanguageName, FormatFnByKey extends ParsedFormatFnByKey>(translationsByLanguage: TranslationModuleByLanguage<Language, FormatFnByKey>): TranslationFile<Language, FormatFnByKey>;
@@ -99,6 +99,7 @@ export interface TranslationData {
99
99
  message: TranslationMessage;
100
100
  description?: string;
101
101
  tags?: Tags;
102
+ globalKey?: string;
102
103
  }
103
104
  export type TranslationsByKey<Key extends TranslationKey = string> = Record<Key, TranslationData>;
104
105
  export type TranslationFileContents = TranslationsByKey & {
@@ -1,4 +1,4 @@
1
- import type { LanguageName, LanguageTarget, TranslationsByKey, TranslationMessagesByKey, UserConfig } from './types';
1
+ import type { LanguageName, LanguageTarget, TranslationsByKey, TranslationMessagesByKey, UserConfig } from "./types.js";
2
2
  export declare const defaultTranslationDirSuffix = ".vocab";
3
3
  export declare const devTranslationFileName = "translations.json";
4
4
  export type Fallback = 'none' | 'valid' | 'all';
@@ -1,3 +1,3 @@
1
- import type { UserConfig, LoadedTranslation, LanguageName } from '../types';
1
+ import type { UserConfig, LoadedTranslation, LanguageName } from "../types.js";
2
2
  export declare function findMissingKeys(loadedTranslation: LoadedTranslation, devLanguageName: LanguageName, altLanguages: Array<LanguageName>): readonly [boolean, Record<string, string[]>];
3
3
  export declare function validate(config: UserConfig): Promise<boolean>;
@@ -1 +1,2 @@
1
1
  export * from "./declarations/src/index";
2
+ //# sourceMappingURL=vocab-core.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab-core.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -433,6 +433,13 @@ async function loadAllTranslations({
433
433
  throw new Error(`Duplicate keys found. Key with namespace ${loadedTranslation.namespace} and key ${key} was found multiple times.`);
434
434
  }
435
435
  keys.add(uniqueKey);
436
+ const globalKey = loadedTranslation.languages[config.devLanguage][key].globalKey;
437
+ if (globalKey) {
438
+ if (keys.has(globalKey)) {
439
+ throw new Error(`Duplicate keys found. Key with global key ${globalKey} and key ${key} was found multiple times`);
440
+ }
441
+ keys.add(globalKey);
442
+ }
436
443
  }
437
444
  }
438
445
  return result;
@@ -848,7 +855,7 @@ function createConfig(configFilePath) {
848
855
  };
849
856
  }
850
857
  async function resolveConfig(customConfigFilePath) {
851
- const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : await findUp__default["default"]('vocab.config.js');
858
+ const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : await findUp__default["default"](['vocab.config.js', 'vocab.config.cjs']);
852
859
  if (configFilePath) {
853
860
  trace(`Resolved configuration file to ${configFilePath}`);
854
861
  return createConfig(configFilePath);
@@ -857,7 +864,7 @@ async function resolveConfig(customConfigFilePath) {
857
864
  return null;
858
865
  }
859
866
  function resolveConfigSync(customConfigFilePath) {
860
- const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : findUp__default["default"].sync('vocab.config.js');
867
+ const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : findUp__default["default"].sync(['vocab.config.js', 'vocab.config.cjs']);
861
868
  if (configFilePath) {
862
869
  trace(`Resolved configuration file to ${configFilePath}`);
863
870
  return createConfig(configFilePath);
@@ -433,6 +433,13 @@ async function loadAllTranslations({
433
433
  throw new Error(`Duplicate keys found. Key with namespace ${loadedTranslation.namespace} and key ${key} was found multiple times.`);
434
434
  }
435
435
  keys.add(uniqueKey);
436
+ const globalKey = loadedTranslation.languages[config.devLanguage][key].globalKey;
437
+ if (globalKey) {
438
+ if (keys.has(globalKey)) {
439
+ throw new Error(`Duplicate keys found. Key with global key ${globalKey} and key ${key} was found multiple times`);
440
+ }
441
+ keys.add(globalKey);
442
+ }
436
443
  }
437
444
  }
438
445
  return result;
@@ -848,7 +855,7 @@ function createConfig(configFilePath) {
848
855
  };
849
856
  }
850
857
  async function resolveConfig(customConfigFilePath) {
851
- const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : await findUp__default["default"]('vocab.config.js');
858
+ const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : await findUp__default["default"](['vocab.config.js', 'vocab.config.cjs']);
852
859
  if (configFilePath) {
853
860
  trace(`Resolved configuration file to ${configFilePath}`);
854
861
  return createConfig(configFilePath);
@@ -857,7 +864,7 @@ async function resolveConfig(customConfigFilePath) {
857
864
  return null;
858
865
  }
859
866
  function resolveConfigSync(customConfigFilePath) {
860
- const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : findUp__default["default"].sync('vocab.config.js');
867
+ const configFilePath = customConfigFilePath ? path__default["default"].resolve(customConfigFilePath) : findUp__default["default"].sync(['vocab.config.js', 'vocab.config.cjs']);
861
868
  if (configFilePath) {
862
869
  trace(`Resolved configuration file to ${configFilePath}`);
863
870
  return createConfig(configFilePath);
@@ -417,6 +417,13 @@ async function loadAllTranslations({
417
417
  throw new Error(`Duplicate keys found. Key with namespace ${loadedTranslation.namespace} and key ${key} was found multiple times.`);
418
418
  }
419
419
  keys.add(uniqueKey);
420
+ const globalKey = loadedTranslation.languages[config.devLanguage][key].globalKey;
421
+ if (globalKey) {
422
+ if (keys.has(globalKey)) {
423
+ throw new Error(`Duplicate keys found. Key with global key ${globalKey} and key ${key} was found multiple times`);
424
+ }
425
+ keys.add(globalKey);
426
+ }
420
427
  }
421
428
  }
422
429
  return result;
@@ -832,7 +839,7 @@ function createConfig(configFilePath) {
832
839
  };
833
840
  }
834
841
  async function resolveConfig(customConfigFilePath) {
835
- const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : await findUp('vocab.config.js');
842
+ const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : await findUp(['vocab.config.js', 'vocab.config.cjs']);
836
843
  if (configFilePath) {
837
844
  trace(`Resolved configuration file to ${configFilePath}`);
838
845
  return createConfig(configFilePath);
@@ -841,7 +848,7 @@ async function resolveConfig(customConfigFilePath) {
841
848
  return null;
842
849
  }
843
850
  function resolveConfigSync(customConfigFilePath) {
844
- const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : findUp.sync('vocab.config.js');
851
+ const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : findUp.sync(['vocab.config.js', 'vocab.config.cjs']);
845
852
  if (configFilePath) {
846
853
  trace(`Resolved configuration file to ${configFilePath}`);
847
854
  return createConfig(configFilePath);
@@ -1 +1,2 @@
1
1
  export * from "../../dist/declarations/src/icu-handler";
2
+ //# sourceMappingURL=vocab-core-icu-handler.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab-core-icu-handler.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/icu-handler.d.ts"],"names":[],"mappings":"AAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vocab/core",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "main": "dist/vocab-core.cjs.js",
5
5
  "module": "dist/vocab-core.esm.js",
6
6
  "exports": {
@@ -1 +1,2 @@
1
1
  export * from "../../dist/declarations/src/runtime";
2
+ //# sourceMappingURL=vocab-core-runtime.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab-core-runtime.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/runtime.d.ts"],"names":[],"mappings":"AAAA"}
@@ -1 +1,2 @@
1
1
  export * from "../../dist/declarations/src/translation-file";
2
+ //# sourceMappingURL=vocab-core-translation-file.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab-core-translation-file.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/translation-file.d.ts"],"names":[],"mappings":"AAAA"}
@@ -1,5 +0,0 @@
1
- export declare class ValidationError extends Error {
2
- code: string;
3
- rawMessage: string;
4
- constructor(code: string, message: string);
5
- }
@@ -1,5 +0,0 @@
1
- import type { MessageGenerator, TranslationsByKey } from './types';
2
- export declare function generateLanguageFromTranslations({ baseTranslations, generator, }: {
3
- baseTranslations: TranslationsByKey<string>;
4
- generator: MessageGenerator;
5
- }): TranslationsByKey<string>;
@@ -1,3 +0,0 @@
1
- import debug from 'debug';
2
- export declare const trace: debug.Debugger;
3
- export declare const log: (...params: unknown[]) => void;