@lingui/cli 4.0.0-next.5 → 4.0.0-next.7

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.
Files changed (66) hide show
  1. package/dist/api/catalog/extractFromFiles.d.ts +5 -0
  2. package/dist/api/catalog/extractFromFiles.js +27 -44
  3. package/dist/api/catalog/getCatalogs.d.ts +14 -0
  4. package/dist/api/catalog/getCatalogs.js +70 -78
  5. package/dist/api/catalog/getTranslationsForCatalog.d.ts +14 -0
  6. package/dist/api/catalog/getTranslationsForCatalog.js +36 -32
  7. package/dist/api/catalog/mergeCatalog.d.ts +3 -0
  8. package/dist/api/catalog/mergeCatalog.js +43 -74
  9. package/dist/api/catalog.d.ts +66 -0
  10. package/dist/api/catalog.js +144 -169
  11. package/dist/api/compile.d.ts +19 -0
  12. package/dist/api/compile.js +60 -87
  13. package/dist/api/extractors/babel.d.ts +3 -0
  14. package/dist/api/extractors/babel.js +43 -100
  15. package/dist/api/extractors/index.d.ts +6 -0
  16. package/dist/api/extractors/index.js +17 -23
  17. package/dist/api/extractors/typescript.d.ts +3 -0
  18. package/dist/api/extractors/typescript.js +7 -11
  19. package/dist/api/formats/formatterWrapper.d.ts +10 -0
  20. package/dist/api/formats/formatterWrapper.js +14 -19
  21. package/dist/api/formats/index.d.ts +5 -0
  22. package/dist/api/formats/index.js +15 -63
  23. package/dist/api/help.d.ts +1 -0
  24. package/dist/api/help.js +12 -13
  25. package/dist/api/index.d.ts +4 -0
  26. package/dist/api/index.js +24 -30
  27. package/dist/api/pseudoLocalize.d.ts +1 -0
  28. package/dist/api/pseudoLocalize.js +23 -27
  29. package/dist/api/rethrownError.d.ts +4 -0
  30. package/dist/api/rethrownError.js +4 -9
  31. package/dist/api/stats.d.ts +6 -0
  32. package/dist/api/stats.js +23 -55
  33. package/dist/api/types.d.ts +5 -0
  34. package/dist/api/types.js +1 -3
  35. package/dist/api/utils.d.ts +22 -0
  36. package/dist/api/utils.js +61 -59
  37. package/dist/extract-experimental/buildExternalizeFilter.d.ts +13 -0
  38. package/dist/extract-experimental/buildExternalizeFilter.js +19 -64
  39. package/dist/extract-experimental/bundleSource.d.ts +2 -0
  40. package/dist/extract-experimental/bundleSource.js +25 -74
  41. package/dist/extract-experimental/constants.d.ts +2 -0
  42. package/dist/extract-experimental/constants.js +4 -15
  43. package/dist/extract-experimental/getEntryPoints.d.ts +1 -0
  44. package/dist/extract-experimental/getEntryPoints.js +8 -16
  45. package/dist/extract-experimental/getExperimentalCatalogs.d.ts +3 -0
  46. package/dist/extract-experimental/getExperimentalCatalogs.js +15 -19
  47. package/dist/extract-experimental/resolveCatalogPath.d.ts +2 -0
  48. package/dist/extract-experimental/resolveCatalogPath.js +13 -24
  49. package/dist/extract-experimental/resolveTemplatePath.d.ts +1 -0
  50. package/dist/extract-experimental/resolveTemplatePath.js +11 -14
  51. package/dist/extract-experimental/writeCatalogs.d.ts +21 -0
  52. package/dist/extract-experimental/writeCatalogs.js +23 -36
  53. package/dist/lingui-compile.d.ts +9 -0
  54. package/dist/lingui-compile.js +94 -82
  55. package/dist/lingui-extract-experimental.d.ts +10 -0
  56. package/dist/lingui-extract-experimental.js +58 -67
  57. package/dist/lingui-extract-template.d.ts +6 -0
  58. package/dist/lingui-extract-template.js +29 -35
  59. package/dist/lingui-extract.d.ts +11 -0
  60. package/dist/lingui-extract.js +81 -117
  61. package/dist/lingui.d.ts +2 -0
  62. package/dist/lingui.js +15 -6
  63. package/dist/services/translationIO.d.ts +3 -0
  64. package/dist/services/translationIO.js +92 -89
  65. package/package.json +23 -14
  66. package/dist/tests.js +0 -137
@@ -0,0 +1,5 @@
1
+ import type { LinguiConfigNormalized } from "@lingui/conf";
2
+ import { ExtractedCatalogType } from "../types";
3
+ export declare function extractFromFiles(paths: string[], config: LinguiConfigNormalized): Promise<ExtractedCatalogType<{
4
+ [x: string]: unknown;
5
+ }>>;
@@ -1,64 +1,47 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "extractFromFiles", {
6
- enumerable: true,
7
- get: ()=>extractFromFiles
8
- });
9
- const _extractors = /*#__PURE__*/ _interopRequireDefault(require("../extractors"));
10
- const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
11
- const _chalk = /*#__PURE__*/ _interopRequireDefault(require("chalk"));
12
- const _utils = require("../utils");
13
- function _interopRequireDefault(obj) {
14
- return obj && obj.__esModule ? obj : {
15
- default: obj
16
- };
17
- }
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.extractFromFiles = void 0;
7
+ const extractors_1 = __importDefault(require("../extractors"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const chalk_1 = __importDefault(require("chalk"));
10
+ const utils_1 = require("../utils");
18
11
  async function extractFromFiles(paths, config) {
19
12
  const messages = {};
20
13
  let catalogSuccess = true;
21
- for (let filename of paths){
22
- const fileSuccess = await (0, _extractors.default)(filename, (next)=>{
14
+ for (let filename of paths) {
15
+ const fileSuccess = await (0, extractors_1.default)(filename, (next) => {
23
16
  if (!messages[next.id]) {
24
17
  messages[next.id] = {
25
18
  message: next.message,
26
19
  context: next.context,
27
20
  comments: [],
28
- origin: []
21
+ origin: [],
29
22
  };
30
23
  }
31
24
  const prev = messages[next.id];
32
25
  // there might be a case when filename was not mapped from sourcemaps
33
- const filename = next.origin[0] ? _path.default.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/") : "";
34
- const origin = [
35
- filename,
36
- next.origin[1]
37
- ];
26
+ const filename = next.origin[0]
27
+ ? path_1.default.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/")
28
+ : "";
29
+ const origin = [filename, next.origin[1]];
38
30
  if (prev.message && next.message && prev.message !== next.message) {
39
- throw new Error(`Encountered different default translations for message ${_chalk.default.yellow(next.id)}` + `\n${_chalk.default.yellow((0, _utils.prettyOrigin)(prev.origin))} ${prev.message}` + `\n${_chalk.default.yellow((0, _utils.prettyOrigin)([
40
- origin
41
- ]))} ${next.message}`);
31
+ throw new Error(`Encountered different default translations for message ${chalk_1.default.yellow(next.id)}` +
32
+ `\n${chalk_1.default.yellow((0, utils_1.prettyOrigin)(prev.origin))} ${prev.message}` +
33
+ `\n${chalk_1.default.yellow((0, utils_1.prettyOrigin)([origin]))} ${next.message}`);
42
34
  }
43
- messages[next.id] = {
44
- ...prev,
45
- comments: next.comment ? [
46
- ...prev.comments,
47
- next.comment
48
- ] : prev.comments,
49
- origin: [
50
- ...prev.origin,
51
- [
52
- filename,
53
- next.origin[1]
54
- ]
55
- ]
56
- };
35
+ messages[next.id] = Object.assign(Object.assign({}, prev), { comments: next.comment
36
+ ? [...prev.comments, next.comment]
37
+ : prev.comments, origin: [...prev.origin, [filename, next.origin[1]]] });
57
38
  }, config, {
58
- extractors: config.extractors
39
+ extractors: config.extractors,
59
40
  });
60
- catalogSuccess &&= fileSuccess;
41
+ catalogSuccess && (catalogSuccess = fileSuccess);
61
42
  }
62
- if (!catalogSuccess) return undefined;
43
+ if (!catalogSuccess)
44
+ return undefined;
63
45
  return messages;
64
46
  }
47
+ exports.extractFromFiles = extractFromFiles;
@@ -0,0 +1,14 @@
1
+ import { LinguiConfigNormalized } from "@lingui/conf";
2
+ import { Catalog } from "../catalog";
3
+ /**
4
+ * Parse `config.catalogs` and return a list of configured Catalog instances.
5
+ */
6
+ export declare function getCatalogs(config: LinguiConfigNormalized): Promise<Catalog[]>;
7
+ /**
8
+ * Create catalog for merged messages.
9
+ */
10
+ export declare function getCatalogForMerge(config: LinguiConfigNormalized): Promise<Catalog>;
11
+ export declare function getCatalogForFile(file: string, catalogs: Catalog[]): {
12
+ locale: string;
13
+ catalog: Catalog;
14
+ };
@@ -1,137 +1,129 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- getCatalogs: ()=>getCatalogs,
13
- getCatalogForMerge: ()=>getCatalogForMerge,
14
- getCatalogForFile: ()=>getCatalogForFile
15
- });
16
- const _glob = /*#__PURE__*/ _interopRequireDefault(require("glob"));
17
- const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
18
- const _catalog = require("../catalog");
19
- const _utils = require("../utils");
20
- const _micromatch = /*#__PURE__*/ _interopRequireDefault(require("micromatch"));
21
- const _formats = require("../formats");
22
- const _getExperimentalCatalogs = require("../../extract-experimental/getExperimentalCatalogs");
23
- function _interopRequireDefault(obj) {
24
- return obj && obj.__esModule ? obj : {
25
- default: obj
26
- };
27
- }
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getCatalogForFile = exports.getCatalogForMerge = exports.getCatalogs = void 0;
7
+ const glob_1 = __importDefault(require("glob"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const catalog_1 = require("../catalog");
10
+ const utils_1 = require("../utils");
11
+ const micromatch_1 = __importDefault(require("micromatch"));
12
+ const formats_1 = require("../formats");
13
+ const getExperimentalCatalogs_1 = require("../../extract-experimental/getExperimentalCatalogs");
28
14
  const NAME_PH = "{name}";
29
15
  const LOCALE_PH = "{locale}";
16
+ /**
17
+ * Parse `config.catalogs` and return a list of configured Catalog instances.
18
+ */
30
19
  async function getCatalogs(config) {
31
- var _config_experimental, _config_experimental_extractor;
20
+ var _a, _b;
32
21
  const catalogsConfig = config.catalogs;
33
22
  const catalogs = [];
34
- const format = await (0, _formats.getFormat)(config.format, config.formatOptions, config.sourceLocale);
35
- catalogsConfig.forEach((catalog)=>{
23
+ const format = await (0, formats_1.getFormat)(config.format, config.formatOptions, config.sourceLocale);
24
+ catalogsConfig.forEach((catalog) => {
36
25
  validateCatalogPath(catalog.path, format.getCatalogExtension());
37
- const include = ensureArray(catalog.include).map(_utils.normalizeRelativePath);
38
- const exclude = ensureArray(catalog.exclude).map(_utils.normalizeRelativePath);
26
+ const include = ensureArray(catalog.include).map(utils_1.normalizeRelativePath);
27
+ const exclude = ensureArray(catalog.exclude).map(utils_1.normalizeRelativePath);
39
28
  // catalog.path without {name} pattern -> always refers to a single catalog
40
29
  if (!catalog.path.includes(NAME_PH)) {
41
30
  // Validate that sourcePaths doesn't use {name} pattern either
42
- const invalidSource = include.find((path)=>path.includes(NAME_PH));
31
+ const invalidSource = include.find((path) => path.includes(NAME_PH));
43
32
  if (invalidSource !== undefined) {
44
- throw new Error(`Catalog with path "${catalog.path}" doesn't have a {name} pattern` + ` in it, but one of source directories uses it: "${invalidSource}".` + ` Either add {name} pattern to "${catalog.path}" or remove it` + ` from all source directories.`);
33
+ throw new Error(`Catalog with path "${catalog.path}" doesn't have a {name} pattern` +
34
+ ` in it, but one of source directories uses it: "${invalidSource}".` +
35
+ ` Either add {name} pattern to "${catalog.path}" or remove it` +
36
+ ` from all source directories.`);
45
37
  }
46
- catalogs.push(new _catalog.Catalog({
38
+ catalogs.push(new catalog_1.Catalog({
47
39
  name: getCatalogName(catalog.path),
48
- path: (0, _utils.normalizeRelativePath)(catalog.path),
40
+ path: (0, utils_1.normalizeRelativePath)(catalog.path),
49
41
  include,
50
42
  exclude,
51
- format
43
+ format,
52
44
  }, config));
53
45
  return;
54
46
  }
55
- const patterns = include.map((path)=>(0, _utils.replacePlaceholders)(path, {
56
- name: "*"
57
- }));
58
- const candidates = _glob.default.sync(patterns.length > 1 ? `{${patterns.join(",")}}` : patterns[0], {
47
+ const patterns = include.map((path) => (0, utils_1.replacePlaceholders)(path, { name: "*" }));
48
+ const candidates = glob_1.default.sync(patterns.length > 1 ? `{${patterns.join(",")}}` : patterns[0], {
59
49
  ignore: exclude,
60
- mark: true
50
+ mark: true,
61
51
  });
62
- candidates.forEach((catalogDir)=>{
63
- const name = _path.default.basename(catalogDir);
64
- catalogs.push(new _catalog.Catalog({
52
+ candidates.forEach((catalogDir) => {
53
+ const name = path_1.default.basename(catalogDir);
54
+ catalogs.push(new catalog_1.Catalog({
65
55
  name,
66
- path: (0, _utils.normalizeRelativePath)((0, _utils.replacePlaceholders)(catalog.path, {
67
- name
68
- })),
69
- include: include.map((path)=>(0, _utils.replacePlaceholders)(path, {
70
- name
71
- })),
72
- exclude: exclude.map((path)=>(0, _utils.replacePlaceholders)(path, {
73
- name
74
- })),
75
- format
56
+ path: (0, utils_1.normalizeRelativePath)((0, utils_1.replacePlaceholders)(catalog.path, { name })),
57
+ include: include.map((path) => (0, utils_1.replacePlaceholders)(path, { name })),
58
+ exclude: exclude.map((path) => (0, utils_1.replacePlaceholders)(path, { name })),
59
+ format,
76
60
  }, config));
77
61
  });
78
62
  });
79
- if ((_config_experimental = config.experimental) === null || _config_experimental === void 0 ? void 0 : (_config_experimental_extractor = _config_experimental.extractor) === null || _config_experimental_extractor === void 0 ? void 0 : _config_experimental_extractor.entries.length) {
80
- catalogs.push(...await (0, _getExperimentalCatalogs.getExperimentalCatalogs)(config));
63
+ if ((_b = (_a = config.experimental) === null || _a === void 0 ? void 0 : _a.extractor) === null || _b === void 0 ? void 0 : _b.entries.length) {
64
+ catalogs.push(...(await (0, getExperimentalCatalogs_1.getExperimentalCatalogs)(config)));
81
65
  }
82
66
  return catalogs;
83
67
  }
68
+ exports.getCatalogs = getCatalogs;
84
69
  /**
85
70
  * Ensure that value is always array. If not, turn it into an array of one element.
86
- */ const ensureArray = (value)=>{
87
- if (value == null) return [];
88
- return Array.isArray(value) ? value : [
89
- value
90
- ];
71
+ */
72
+ const ensureArray = (value) => {
73
+ if (value == null)
74
+ return [];
75
+ return Array.isArray(value) ? value : [value];
91
76
  };
77
+ /**
78
+ * Create catalog for merged messages.
79
+ */
92
80
  async function getCatalogForMerge(config) {
93
- const format = await (0, _formats.getFormat)(config.format, config.formatOptions, config.sourceLocale);
81
+ const format = await (0, formats_1.getFormat)(config.format, config.formatOptions, config.sourceLocale);
94
82
  validateCatalogPath(config.catalogsMergePath, format.getCatalogExtension());
95
- return new _catalog.Catalog({
83
+ return new catalog_1.Catalog({
96
84
  name: getCatalogName(config.catalogsMergePath),
97
- path: (0, _utils.normalizeRelativePath)(config.catalogsMergePath),
85
+ path: (0, utils_1.normalizeRelativePath)(config.catalogsMergePath),
98
86
  include: [],
99
87
  exclude: [],
100
- format
88
+ format,
101
89
  }, config);
102
90
  }
91
+ exports.getCatalogForMerge = getCatalogForMerge;
103
92
  function getCatalogForFile(file, catalogs) {
104
- for (const catalog of catalogs){
93
+ for (const catalog of catalogs) {
105
94
  const catalogFile = `${catalog.path}${catalog.format.getCatalogExtension()}`;
106
- const catalogGlob = (0, _utils.replacePlaceholders)(catalogFile, {
107
- locale: "*"
108
- });
109
- const match = _micromatch.default.capture((0, _utils.normalizeRelativePath)(_path.default.relative(catalog.config.rootDir, catalogGlob)), (0, _utils.normalizeRelativePath)(file));
95
+ const catalogGlob = (0, utils_1.replacePlaceholders)(catalogFile, { locale: "*" });
96
+ const match = micromatch_1.default.capture((0, utils_1.normalizeRelativePath)(path_1.default.relative(catalog.config.rootDir, catalogGlob)), (0, utils_1.normalizeRelativePath)(file));
110
97
  if (match) {
111
98
  return {
112
99
  locale: match[0],
113
- catalog
100
+ catalog,
114
101
  };
115
102
  }
116
103
  }
117
104
  return null;
118
105
  }
106
+ exports.getCatalogForFile = getCatalogForFile;
119
107
  /**
120
108
  * Validate that `catalogPath` doesn't end with trailing slash
121
- */ function validateCatalogPath(path, extension) {
122
- if (!path.endsWith(_utils.PATHSEP)) {
109
+ */
110
+ function validateCatalogPath(path, extension) {
111
+ if (!path.endsWith(utils_1.PATHSEP)) {
123
112
  return;
124
113
  }
125
114
  const correctPath = path.slice(0, -1);
126
- const examplePath = (0, _utils.replacePlaceholders)(correctPath, {
127
- locale: "en"
115
+ const examplePath = (0, utils_1.replacePlaceholders)(correctPath, {
116
+ locale: "en",
128
117
  }) + extension;
129
- throw new Error(// prettier-ignore
130
- `Remove trailing slash from "${path}". Catalog path isn't a directory,` + ` but translation file without extension. For example, catalog path "${correctPath}"` + ` results in translation file "${examplePath}".`);
118
+ throw new Error(
119
+ // prettier-ignore
120
+ `Remove trailing slash from "${path}". Catalog path isn't a directory,` +
121
+ ` but translation file without extension. For example, catalog path "${correctPath}"` +
122
+ ` results in translation file "${examplePath}".`);
131
123
  }
132
124
  function getCatalogName(filePath) {
133
125
  // catalog name is the last directory of catalogPath.
134
126
  // If the last part is {locale}, then catalog doesn't have an explicit name
135
- const _name = _path.default.basename((0, _utils.normalizeRelativePath)(filePath));
127
+ const _name = path_1.default.basename((0, utils_1.normalizeRelativePath)(filePath));
136
128
  return _name !== LOCALE_PH ? _name : null;
137
129
  }
@@ -0,0 +1,14 @@
1
+ import { Catalog } from "../catalog";
2
+ import { FallbackLocales } from "@lingui/conf";
3
+ export type TranslationMissingEvent = {
4
+ source: string;
5
+ id: string;
6
+ };
7
+ export type GetTranslationsOptions = {
8
+ sourceLocale: string;
9
+ fallbackLocales: FallbackLocales;
10
+ onMissing?: (message: TranslationMissingEvent) => void;
11
+ };
12
+ export declare function getTranslationsForCatalog(catalog: Catalog, locale: string, options: GetTranslationsOptions): Promise<{
13
+ [id: string]: string;
14
+ }>;
@@ -1,25 +1,17 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "getTranslationsForCatalog", {
6
- enumerable: true,
7
- get: ()=>getTranslationsForCatalog
8
- });
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTranslationsForCatalog = void 0;
9
4
  async function getTranslationsForCatalog(catalog, locale, options) {
10
5
  const catalogs = await catalog.readAll();
11
- const template = await catalog.readTemplate() || {};
6
+ const template = (await catalog.readTemplate()) || {};
12
7
  const sourceLocaleCatalog = catalogs[options.sourceLocale] || {};
13
- const input = {
14
- ...template,
15
- ...sourceLocaleCatalog,
16
- ...catalogs[locale]
17
- };
18
- return Object.keys(input).reduce((acc, key)=>{
8
+ const input = Object.assign(Object.assign(Object.assign({}, template), sourceLocaleCatalog), catalogs[locale]);
9
+ return Object.keys(input).reduce((acc, key) => {
19
10
  acc[key] = getTranslation(catalogs, input[key], locale, key, options);
20
11
  return acc;
21
12
  }, {});
22
13
  }
14
+ exports.getTranslationsForCatalog = getTranslationsForCatalog;
23
15
  function sourceLocaleFallback(catalog, key) {
24
16
  if (!(catalog === null || catalog === void 0 ? void 0 : catalog[key])) {
25
17
  return null;
@@ -27,23 +19,25 @@ function sourceLocaleFallback(catalog, key) {
27
19
  return catalog[key].translation || catalog[key].message;
28
20
  }
29
21
  function getTranslation(catalogs, msg, locale, key, options) {
30
- const { fallbackLocales , sourceLocale , onMissing } = options;
31
- const getTranslation = (_locale)=>{
32
- var _localeCatalog_key;
22
+ const { fallbackLocales, sourceLocale, onMissing } = options;
23
+ const getTranslation = (_locale) => {
24
+ var _a;
33
25
  const localeCatalog = catalogs[_locale];
34
- return localeCatalog === null || localeCatalog === void 0 ? void 0 : (_localeCatalog_key = localeCatalog[key]) === null || _localeCatalog_key === void 0 ? void 0 : _localeCatalog_key.translation;
26
+ return (_a = localeCatalog === null || localeCatalog === void 0 ? void 0 : localeCatalog[key]) === null || _a === void 0 ? void 0 : _a.translation;
35
27
  };
36
- const getMultipleFallbacks = (_locale)=>{
28
+ const getMultipleFallbacks = (_locale) => {
37
29
  const fL = fallbackLocales && (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales[_locale]);
38
30
  // some probably the fallback will be undefined, so just search by locale
39
- if (!fL) return null;
31
+ if (!fL)
32
+ return null;
40
33
  if (Array.isArray(fL)) {
41
- for (const fallbackLocale of fL){
34
+ for (const fallbackLocale of fL) {
42
35
  if (catalogs[fallbackLocale] && getTranslation(fallbackLocale)) {
43
36
  return getTranslation(fallbackLocale);
44
37
  }
45
38
  }
46
- } else {
39
+ }
40
+ else {
47
41
  return getTranslation(fL);
48
42
  }
49
43
  };
@@ -53,16 +47,26 @@ function getTranslation(catalogs, msg, locale, key, options) {
53
47
  // -> template message
54
48
  // ** last resort **
55
49
  // -> id
56
- let translation = // Get translation in target locale
57
- getTranslation(locale) || // We search in fallbackLocales as dependent of each locale
58
- getMultipleFallbacks(locale) || // Get translation in fallbackLocales.default (if any)
59
- (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && getTranslation(fallbackLocales.default) || sourceLocale && sourceLocale === locale && sourceLocaleFallback(catalogs[sourceLocale], key);
50
+ let translation =
51
+ // Get translation in target locale
52
+ getTranslation(locale) ||
53
+ // We search in fallbackLocales as dependent of each locale
54
+ getMultipleFallbacks(locale) ||
55
+ // Get translation in fallbackLocales.default (if any)
56
+ ((fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && getTranslation(fallbackLocales.default)) ||
57
+ (sourceLocale &&
58
+ sourceLocale === locale &&
59
+ sourceLocaleFallback(catalogs[sourceLocale], key));
60
60
  if (!translation) {
61
- onMissing && onMissing({
62
- id: key,
63
- source: msg.message || sourceLocaleFallback(catalogs[sourceLocale], key)
64
- });
61
+ onMissing &&
62
+ onMissing({
63
+ id: key,
64
+ source: msg.message || sourceLocaleFallback(catalogs[sourceLocale], key),
65
+ });
65
66
  }
66
- return translation || sourceLocale && sourceLocaleFallback(catalogs[sourceLocale], key) || // take from template
67
- msg.message || key;
67
+ return (translation ||
68
+ (sourceLocale && sourceLocaleFallback(catalogs[sourceLocale], key)) ||
69
+ // take from template
70
+ msg.message ||
71
+ key);
68
72
  }
@@ -0,0 +1,3 @@
1
+ import type { MergeOptions } from "../catalog";
2
+ import { CatalogType, ExtractedCatalogType } from "../types";
3
+ export declare function mergeCatalog(prevCatalog: CatalogType, nextCatalog: ExtractedCatalogType, forSourceLocale: boolean, options: MergeOptions): CatalogType;
@@ -1,86 +1,55 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "mergeCatalog", {
6
- enumerable: true,
7
- get: ()=>mergeCatalog
8
- });
9
- const _ramda = /*#__PURE__*/ _interopRequireWildcard(require("ramda"));
10
- function _getRequireWildcardCache(nodeInterop) {
11
- if (typeof WeakMap !== "function") return null;
12
- var cacheBabelInterop = new WeakMap();
13
- var cacheNodeInterop = new WeakMap();
14
- return (_getRequireWildcardCache = function(nodeInterop) {
15
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
16
- })(nodeInterop);
17
- }
18
- function _interopRequireWildcard(obj, nodeInterop) {
19
- if (!nodeInterop && obj && obj.__esModule) {
20
- return obj;
21
- }
22
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
23
- return {
24
- default: obj
25
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
26
7
  }
27
- var cache = _getRequireWildcardCache(nodeInterop);
28
- if (cache && cache.has(obj)) {
29
- return cache.get(obj);
30
- }
31
- var newObj = {};
32
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
33
- for(var key in obj){
34
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
35
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
36
- if (desc && (desc.get || desc.set)) {
37
- Object.defineProperty(newObj, key, desc);
38
- } else {
39
- newObj[key] = obj[key];
40
- }
41
- }
42
- }
43
- newObj.default = obj;
44
- if (cache) {
45
- cache.set(obj, newObj);
46
- }
47
- return newObj;
48
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.mergeCatalog = void 0;
27
+ const R = __importStar(require("ramda"));
49
28
  function mergeCatalog(prevCatalog, nextCatalog, forSourceLocale, options) {
50
29
  const nextKeys = Object.keys(nextCatalog);
51
- const prevKeys = _ramda.keys(prevCatalog).map(String);
52
- const newKeys = _ramda.difference(nextKeys, prevKeys);
53
- const mergeKeys = _ramda.intersection(nextKeys, prevKeys);
54
- const obsoleteKeys = _ramda.difference(prevKeys, nextKeys);
30
+ const prevKeys = R.keys(prevCatalog).map(String);
31
+ const newKeys = R.difference(nextKeys, prevKeys);
32
+ const mergeKeys = R.intersection(nextKeys, prevKeys);
33
+ const obsoleteKeys = R.difference(prevKeys, nextKeys);
55
34
  // Initialize new catalog with new keys
56
- const newMessages = _ramda.mapObjIndexed((message, key)=>({
57
- translation: forSourceLocale ? message.message || key : "",
58
- ...message
59
- }), _ramda.pick(newKeys, nextCatalog));
35
+ const newMessages = R.mapObjIndexed((message, key) => (Object.assign({ translation: forSourceLocale ? message.message || key : "" }, message)), R.pick(newKeys, nextCatalog));
60
36
  // Merge translations from previous catalog
61
- const mergedMessages = mergeKeys.map((key)=>{
62
- const updateFromDefaults = forSourceLocale && (prevCatalog[key].translation === prevCatalog[key].message || options.overwrite);
63
- const translation = updateFromDefaults ? nextCatalog[key].message || key : prevCatalog[key].translation;
37
+ const mergedMessages = mergeKeys.map((key) => {
38
+ const updateFromDefaults = forSourceLocale &&
39
+ (prevCatalog[key].translation === prevCatalog[key].message ||
40
+ options.overwrite);
41
+ const translation = updateFromDefaults
42
+ ? nextCatalog[key].message || key
43
+ : prevCatalog[key].translation;
64
44
  return {
65
- [key]: {
66
- translation,
67
- ..._ramda.omit([
68
- "obsolete, translation"
69
- ], nextCatalog[key])
70
- }
45
+ [key]: Object.assign({ translation }, R.omit(["obsolete, translation"], nextCatalog[key])),
71
46
  };
72
47
  });
73
48
  // Mark all remaining translations as obsolete
74
49
  // Only if *options.files* is not provided
75
- const obsoleteMessages = obsoleteKeys.map((key)=>({
76
- [key]: {
77
- ...prevCatalog[key],
78
- obsolete: !options.files
79
- }
80
- }));
81
- return _ramda.mergeAll([
82
- newMessages,
83
- ...mergedMessages,
84
- ...obsoleteMessages
85
- ]);
50
+ const obsoleteMessages = obsoleteKeys.map((key) => ({
51
+ [key]: Object.assign(Object.assign({}, prevCatalog[key]), { obsolete: !options.files }),
52
+ }));
53
+ return R.mergeAll([newMessages, ...mergedMessages, ...obsoleteMessages]);
86
54
  }
55
+ exports.mergeCatalog = mergeCatalog;
@@ -0,0 +1,66 @@
1
+ import * as R from "ramda";
2
+ import { LinguiConfigNormalized, OrderBy } from "@lingui/conf";
3
+ import { FormatterWrapper } from "./formats";
4
+ import { CliExtractOptions } from "../lingui-extract";
5
+ import { CliExtractTemplateOptions } from "../lingui-extract-template";
6
+ import { CompiledCatalogNamespace } from "./compile";
7
+ import { GetTranslationsOptions } from "./catalog/getTranslationsForCatalog";
8
+ import { AllCatalogsType, CatalogType, ExtractedCatalogType, ExtractedMessageType } from "./types";
9
+ export type MakeOptions = CliExtractOptions & {
10
+ orderBy?: OrderBy;
11
+ };
12
+ export type MakeTemplateOptions = CliExtractTemplateOptions & {
13
+ orderBy?: OrderBy;
14
+ };
15
+ export type MergeOptions = {
16
+ overwrite?: boolean;
17
+ files?: string[];
18
+ };
19
+ export type CatalogProps = {
20
+ name?: string;
21
+ path: string;
22
+ include: Array<string>;
23
+ exclude?: Array<string>;
24
+ templatePath?: string;
25
+ format: FormatterWrapper;
26
+ };
27
+ export declare class Catalog {
28
+ config: LinguiConfigNormalized;
29
+ name?: string;
30
+ path: string;
31
+ include: Array<string>;
32
+ exclude: Array<string>;
33
+ format: FormatterWrapper;
34
+ templateFile?: string;
35
+ constructor({ name, path, include, templatePath, format, exclude }: CatalogProps, config: LinguiConfigNormalized);
36
+ make(options: MakeOptions): Promise<AllCatalogsType | false>;
37
+ makeTemplate(options: MakeTemplateOptions): Promise<CatalogType | false>;
38
+ /**
39
+ * Collect messages from source paths. Return a raw message catalog as JSON.
40
+ */
41
+ collect(options?: {
42
+ files?: string[];
43
+ }): Promise<ExtractedCatalogType | undefined>;
44
+ merge(prevCatalogs: AllCatalogsType, nextCatalog: ExtractedCatalogType, options: MergeOptions): Record<string, CatalogType<{
45
+ [x: string]: unknown;
46
+ }>>;
47
+ getTranslations(locale: string, options: GetTranslationsOptions): Promise<{
48
+ [id: string]: string;
49
+ }>;
50
+ write(locale: string, messages: CatalogType): Promise<[created: boolean, filename: string]>;
51
+ writeTemplate(messages: CatalogType): Promise<void>;
52
+ writeCompiled(locale: string, compiledCatalog: string, namespace?: CompiledCatalogNamespace): Promise<string>;
53
+ read(locale: string): Promise<CatalogType>;
54
+ readAll(): Promise<AllCatalogsType>;
55
+ readTemplate(): Promise<CatalogType>;
56
+ get sourcePaths(): string[];
57
+ get localeDir(): string;
58
+ get locales(): string[];
59
+ }
60
+ export declare const cleanObsolete: <K extends ExtractedMessageType<{
61
+ [x: string]: unknown;
62
+ }>[] | R.Dictionary<ExtractedMessageType<{
63
+ [x: string]: unknown;
64
+ }>>>(source: K) => K extends (infer U)[] ? U[] : K extends R.Dictionary<infer U_1> ? R.Dictionary<U_1> : never;
65
+ export declare function order<T extends ExtractedCatalogType>(by: OrderBy): (catalog: T) => T;
66
+ export declare function orderByMessage<T extends ExtractedCatalogType>(messages: T): T;