@lingui/cli 4.0.0-next.1 → 4.0.0-next.2

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.
@@ -12,6 +12,7 @@ var _catalog = require("../catalog");
12
12
  var _utils = require("../utils");
13
13
  var _micromatch = _interopRequireDefault(require("micromatch"));
14
14
  var _formats = require("../formats");
15
+ var _getExperimentalCatalogs = require("../../extract-experimental/getExperimentalCatalogs");
15
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17
  const NAME_PH = "{name}";
17
18
  const LOCALE_PH = "{locale}";
@@ -20,6 +21,7 @@ const LOCALE_PH = "{locale}";
20
21
  * Parse `config.catalogs` and return a list of configured Catalog instances.
21
22
  */
22
23
  function getCatalogs(config) {
24
+ var _config$experimental, _config$experimental$;
23
25
  const catalogsConfig = config.catalogs;
24
26
  const catalogs = [];
25
27
  catalogsConfig.forEach(catalog => {
@@ -65,6 +67,9 @@ function getCatalogs(config) {
65
67
  }, config));
66
68
  });
67
69
  });
70
+ if ((_config$experimental = config.experimental) !== null && _config$experimental !== void 0 && (_config$experimental$ = _config$experimental.extractor) !== null && _config$experimental$ !== void 0 && _config$experimental$.entries.length) {
71
+ catalogs.push(...(0, _getExperimentalCatalogs.getExperimentalCatalogs)(config));
72
+ }
68
73
  return catalogs;
69
74
  }
70
75
 
@@ -90,7 +95,7 @@ function getCatalogForMerge(config) {
90
95
  }
91
96
  function getCatalogForFile(file, catalogs) {
92
97
  for (const catalog of catalogs) {
93
- const catalogFile = `${catalog.path}${catalog.format.catalogExtension}`;
98
+ const catalogFile = `${catalog.path}${catalog.format.getCatalogExtension()}`;
94
99
  const catalogGlob = (0, _utils.replacePlaceholders)(catalogFile, {
95
100
  locale: "*"
96
101
  });
@@ -112,7 +117,7 @@ function validateCatalogPath(path, config) {
112
117
  if (!path.endsWith(_utils.PATHSEP)) {
113
118
  return;
114
119
  }
115
- const extension = (0, _formats.getFormat)(config.format).catalogExtension;
120
+ const extension = (0, _formats.getFormat)(config.format, config.formatOptions).getCatalogExtension();
116
121
  const correctPath = path.slice(0, -1);
117
122
  const examplePath = (0, _utils.replacePlaceholders)(correctPath, {
118
123
  locale: (config.locales || [])[0] || "en"
@@ -4,9 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getTranslationsForCatalog = getTranslationsForCatalog;
7
- function getTranslationsForCatalog(catalog, locale, options) {
8
- const catalogs = catalog.readAll();
9
- const template = catalog.readTemplate() || {};
7
+ async function getTranslationsForCatalog(catalog, locale, options) {
8
+ const catalogs = await catalog.readAll();
9
+ const template = (await catalog.readTemplate()) || {};
10
10
  const sourceLocaleCatalog = catalogs[options.sourceLocale] || {};
11
11
  const input = {
12
12
  ...template,
@@ -5,8 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.cleanObsolete = exports.Catalog = void 0;
7
7
  exports.order = order;
8
- exports.orderByMessageId = orderByMessageId;
9
- exports.orderByOrigin = orderByOrigin;
8
+ exports.orderByMessage = orderByMessage;
10
9
  var _fs = _interopRequireDefault(require("fs"));
11
10
  var _path = _interopRequireDefault(require("path"));
12
11
  var R = _interopRequireWildcard(require("ramda"));
@@ -35,15 +34,15 @@ class Catalog {
35
34
  this.path = (0, _utils.normalizeRelativePath)(path);
36
35
  this.include = include.map(_utils.normalizeRelativePath);
37
36
  this.exclude = [this.localeDir, ...exclude.map(_utils.normalizeRelativePath)];
38
- this.format = (0, _formats.getFormat)(config.format);
39
- this.templateFile = templatePath || getTemplatePath(this.format, this.path);
37
+ this.format = (0, _formats.getFormat)(config.format, config.formatOptions);
38
+ this.templateFile = templatePath || getTemplatePath(this.format.getTemplateExtension(), this.path);
40
39
  }
41
40
  async make(options) {
42
41
  const nextCatalog = await this.collect({
43
42
  files: options.files
44
43
  });
45
44
  if (!nextCatalog) return false;
46
- const prevCatalogs = this.readAll();
45
+ const prevCatalogs = await this.readAll();
47
46
  const catalogs = this.merge(prevCatalogs, nextCatalog, {
48
47
  overwrite: options.overwrite,
49
48
  files: options.files
@@ -56,11 +55,8 @@ class Catalog {
56
55
  // Sort messages
57
56
  order(options.orderBy)));
58
57
  const sortedCatalogs = cleanAndSort(catalogs);
59
- if (options.locale) {
60
- this.write(options.locale, sortedCatalogs[options.locale]);
61
- } else {
62
- this.writeAll(sortedCatalogs);
63
- }
58
+ const locales = options.locale ? [options.locale] : this.locales;
59
+ await Promise.all(locales.map(locale => this.write(locale, sortedCatalogs[locale])));
64
60
  return sortedCatalogs;
65
61
  }
66
62
  async makeTemplate(options) {
@@ -69,7 +65,7 @@ class Catalog {
69
65
  });
70
66
  if (!catalog) return false;
71
67
  const sorted = order(options.orderBy)(catalog);
72
- this.writeTemplate(sorted);
68
+ await this.writeTemplate(sorted);
73
69
  return sorted;
74
70
  }
75
71
 
@@ -111,33 +107,22 @@ class Catalog {
111
107
  return (0, _mergeCatalog.mergeCatalog)(prevCatalog, nextCatalog, this.config.sourceLocale === locale, options);
112
108
  }, prevCatalogs);
113
109
  }
114
- getTranslations(locale, options) {
115
- return (0, _getTranslationsForCatalog.getTranslationsForCatalog)(this, locale, options);
110
+ async getTranslations(locale, options) {
111
+ return await (0, _getTranslationsForCatalog.getTranslationsForCatalog)(this, locale, options);
116
112
  }
117
- write(locale, messages) {
113
+ async write(locale, messages) {
118
114
  const filename = (0, _utils.replacePlaceholders)(this.path, {
119
115
  locale
120
- }) + this.format.catalogExtension;
116
+ }) + this.format.getCatalogExtension();
121
117
  const created = !_fs.default.existsSync(filename);
122
- const options = {
123
- ...this.config.formatOptions,
124
- locale
125
- };
126
- this.format.write(filename, messages, options);
118
+ await this.format.write(filename, messages, locale);
127
119
  return [created, filename];
128
120
  }
129
- writeAll(catalogs) {
130
- this.locales.forEach(locale => this.write(locale, catalogs[locale]));
131
- }
132
- writeTemplate(messages) {
121
+ async writeTemplate(messages) {
133
122
  const filename = this.templateFile;
134
- const options = {
135
- ...this.config.formatOptions,
136
- locale: undefined
137
- };
138
- this.format.write(filename, messages, options);
123
+ await this.format.write(filename, messages, undefined);
139
124
  }
140
- writeCompiled(locale, compiledCatalog, namespace) {
125
+ async writeCompiled(locale, compiledCatalog, namespace) {
141
126
  let ext;
142
127
  if (namespace === "es") {
143
128
  ext = "mjs";
@@ -149,23 +134,29 @@ class Catalog {
149
134
  const filename = `${(0, _utils.replacePlaceholders)(this.path, {
150
135
  locale
151
136
  })}.${ext}`;
152
- (0, _utils.writeFile)(filename, compiledCatalog);
137
+ await (0, _utils.writeFile)(filename, compiledCatalog);
153
138
  return filename;
154
139
  }
155
- read(locale) {
140
+ async read(locale) {
156
141
  const filename = (0, _utils.replacePlaceholders)(this.path, {
157
142
  locale
158
- }) + this.format.catalogExtension;
159
- return this.format.read(filename);
143
+ }) + this.format.getCatalogExtension();
144
+ return await this.format.read(filename, locale);
160
145
  }
161
- readAll() {
162
- return R.mergeAll(this.locales.map(locale => ({
163
- [locale]: this.read(locale)
164
- })));
165
- }
166
- readTemplate() {
146
+ async readAll() {
147
+ const res = {};
148
+ await Promise.all(this.locales.map(async locale => res[locale] = await this.read(locale)));
149
+
150
+ // statement above will save locales in object in undetermined order
151
+ // resort here to have keys order the same as in locales definition
152
+ return this.locales.reduce((acc, locale) => {
153
+ acc[locale] = res[locale];
154
+ return acc;
155
+ }, {});
156
+ }
157
+ async readTemplate() {
167
158
  const filename = this.templateFile;
168
- return this.format.read(filename);
159
+ return await this.format.read(filename, undefined);
169
160
  }
170
161
  get sourcePaths() {
171
162
  const includeGlobs = this.include.map(includePath => {
@@ -194,8 +185,7 @@ class Catalog {
194
185
  }
195
186
  }
196
187
  exports.Catalog = Catalog;
197
- function getTemplatePath(format, path) {
198
- const ext = format.templateExtension || format.catalogExtension;
188
+ function getTemplatePath(ext, path) {
199
189
  return path.replace(LOCALE_SUFFIX_RE, "messages" + ext);
200
190
  }
201
191
  const cleanObsolete = R.filter(message => !message.obsolete);
@@ -203,6 +193,7 @@ exports.cleanObsolete = cleanObsolete;
203
193
  function order(by) {
204
194
  return {
205
195
  messageId: orderByMessageId,
196
+ message: orderByMessage,
206
197
  origin: orderByOrigin
207
198
  }[by];
208
199
  }
@@ -240,4 +231,15 @@ function orderByOrigin(messages) {
240
231
  acc[key] = messages[key];
241
232
  return acc;
242
233
  }, {});
234
+ }
235
+ function orderByMessage(messages) {
236
+ return Object.keys(messages).sort((a, b) => {
237
+ const aMsg = messages[a].message || "";
238
+ const bMsg = messages[b].message || "";
239
+ return aMsg.localeCompare(bMsg);
240
+ }).reduce((acc, key) => {
241
+ ;
242
+ acc[key] = messages[key];
243
+ return acc;
244
+ }, {});
243
245
  }
@@ -26,6 +26,11 @@ function createCompiledCatalog(locale, messages, options) {
26
26
  obj[key] = compile(translation, shouldPseudolocalize);
27
27
  return obj;
28
28
  }, {});
29
+ if (namespace === "json") {
30
+ return JSON.stringify({
31
+ messages: compiledMessages
32
+ });
33
+ }
29
34
  const ast = buildExportStatement(
30
35
  //build JSON.parse(<compiledMessages>) statement
31
36
  t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [t.stringLiteral(JSON.stringify(compiledMessages))]), namespace);
@@ -18,15 +18,21 @@ const extractor = {
18
18
  },
19
19
  async extract(filename, code, onMessageExtracted, ctx) {
20
20
  const parserOptions = ctx.linguiConfig.extractorParserOptions;
21
- const parserPlugins = [
21
+
22
22
  // https://babeljs.io/docs/en/babel-parser#latest-ecmascript-features
23
- ["decorators", {
24
- decoratorsBeforeExport: (parserOptions === null || parserOptions === void 0 ? void 0 : parserOptions.decoratorsBeforeExport) || true
25
- }]];
23
+ const parserPlugins = [];
26
24
  if ([/\.ts$/, /\.mts$/, /\.cts$/, /\.tsx$/].some(r => filename.match(r))) {
27
25
  parserPlugins.push("typescript");
28
- } else if (parserOptions !== null && parserOptions !== void 0 && parserOptions.flow) {
29
- parserPlugins.push("flow");
26
+ if (parserOptions.tsExperimentalDecorators) {
27
+ parserPlugins.push("decorators-legacy");
28
+ } else {
29
+ parserPlugins.push("decorators");
30
+ }
31
+ } else {
32
+ parserPlugins.push("decorators");
33
+ if (parserOptions !== null && parserOptions !== void 0 && parserOptions.flow) {
34
+ parserPlugins.push("flow");
35
+ }
30
36
  }
31
37
  if ([/\.jsx$/, /\.tsx$/].some(r => filename.match(r))) {
32
38
  parserPlugins.push("jsx");
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FormatterWrapper = void 0;
7
+ var _utils = require("../../utils");
8
+ var _rethrownError = require("../../rethrownError");
9
+ class FormatterWrapper {
10
+ constructor(f) {
11
+ this.f = f;
12
+ }
13
+ getCatalogExtension() {
14
+ return this.f.catalogExtension;
15
+ }
16
+ getTemplateExtension() {
17
+ return this.f.templateExtension || this.f.catalogExtension;
18
+ }
19
+ async write(filename, catalog, locale) {
20
+ const content = await this.f.serialize(catalog, {
21
+ locale,
22
+ existing: await (0, _utils.readFile)(filename),
23
+ filename
24
+ });
25
+ await (0, _utils.writeFileIfChanged)(filename, content);
26
+ }
27
+ async read(filename, locale) {
28
+ const content = await (0, _utils.readFile)(filename);
29
+ if (!content) {
30
+ return null;
31
+ }
32
+ try {
33
+ return this.f.parse(content, {
34
+ locale,
35
+ filename
36
+ });
37
+ } catch (e) {
38
+ throw new _rethrownError.RethrownError(`Cannot read ${filename}`, e);
39
+ }
40
+ }
41
+ }
42
+ exports.FormatterWrapper = FormatterWrapper;
@@ -3,9 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var _papaparse = _interopRequireDefault(require("papaparse"));
8
- var _utils = require("../utils");
9
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
9
  const serialize = catalog => {
11
10
  const rawArr = Object.keys(catalog).map(key => [key, catalog[key].translation]);
@@ -27,26 +26,14 @@ const deserialize = raw => {
27
26
  });
28
27
  return messages;
29
28
  };
30
- const csv = {
31
- catalogExtension: ".csv",
32
- write(filename, catalog) {
33
- const messages = serialize(catalog);
34
- (0, _utils.writeFileIfChanged)(filename, messages);
35
- },
36
- read(filename) {
37
- const raw = (0, _utils.readFile)(filename);
38
- if (!raw) {
39
- return null;
29
+ function _default() {
30
+ return {
31
+ catalogExtension: ".csv",
32
+ parse(content) {
33
+ return deserialize(content);
34
+ },
35
+ serialize(catalog) {
36
+ return serialize(catalog);
40
37
  }
41
- try {
42
- return deserialize(raw);
43
- } catch (e) {
44
- throw new Error(`Cannot read ${filename}: ${e.message}`);
45
- }
46
- },
47
- parse(content) {
48
- return deserialize(content);
49
- }
50
- };
51
- var _default = csv;
52
- exports.default = _default;
38
+ };
39
+ }
@@ -3,29 +3,28 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "FormatterWrapper", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _formatterWrapper.FormatterWrapper;
10
+ }
11
+ });
6
12
  exports.getFormat = getFormat;
7
- var _csv = _interopRequireDefault(require("./csv"));
8
- var _lingui = _interopRequireDefault(require("./lingui"));
9
- var _minimal = _interopRequireDefault(require("./minimal"));
10
- var _po = _interopRequireDefault(require("./po"));
11
- var _poGettext = _interopRequireDefault(require("./po-gettext"));
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ var _formatterWrapper = require("./api/formatterWrapper");
13
14
  const formats = {
14
- lingui: _lingui.default,
15
- minimal: _minimal.default,
16
- po: _po.default,
17
- csv: _csv.default,
18
- "po-gettext": _poGettext.default
15
+ lingui: () => require("./lingui").default,
16
+ minimal: () => require("./minimal").default,
17
+ po: () => require("./po").default,
18
+ csv: () => require("./csv").default,
19
+ "po-gettext": () => require("./po-gettext").default
19
20
  };
20
-
21
- /**
22
- * @internal
23
- */
24
-
25
- function getFormat(name) {
26
- const format = formats[name];
21
+ function getFormat(_format, options) {
22
+ if (typeof _format !== "string") {
23
+ return new _formatterWrapper.FormatterWrapper(_format);
24
+ }
25
+ const format = formats[_format];
27
26
  if (!format) {
28
- throw new Error(`Unknown format "${name}". Use one of following: ${Object.keys(formats).join(", ")}`);
27
+ throw new Error(`Unknown format "${_format}". Use one of following: ${Object.keys(formats).join(", ")}`);
29
28
  }
30
- return format;
29
+ return new _formatterWrapper.FormatterWrapper(format()(options));
31
30
  }
@@ -3,9 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var R = _interopRequireWildcard(require("ramda"));
8
- var _utils = require("../utils");
9
8
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
9
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
10
  const removeOrigins = R.map(({
@@ -14,39 +13,34 @@ const removeOrigins = R.map(({
14
13
  }) => message);
15
14
  const removeLineNumbers = R.map(message => {
16
15
  if (message.origin) {
17
- message.origin.map(originValue => {
18
- originValue.pop();
19
- return originValue;
20
- });
16
+ message.origin = message.origin.map(([file]) => [file]);
21
17
  }
22
18
  return message;
23
19
  });
24
- const lingui = {
25
- catalogExtension: ".json",
26
- write(filename, catalog, options) {
27
- let outputCatalog = catalog;
28
- if (options.origins === false) {
29
- outputCatalog = removeOrigins(catalog);
20
+ function _default(options = {}) {
21
+ options = {
22
+ origins: true,
23
+ lineNumbers: true,
24
+ ...options
25
+ };
26
+ return {
27
+ catalogExtension: ".json",
28
+ serialize(catalog, {
29
+ existing
30
+ }) {
31
+ let outputCatalog = catalog;
32
+ if (options.origins === false) {
33
+ outputCatalog = removeOrigins(outputCatalog);
34
+ }
35
+ if (options.origins !== false && options.lineNumbers === false) {
36
+ outputCatalog = removeLineNumbers(outputCatalog);
37
+ }
38
+ const shouldUseTrailingNewline = existing === null || (existing === null || existing === void 0 ? void 0 : existing.endsWith("\n"));
39
+ const trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
40
+ return JSON.stringify(outputCatalog, null, 2) + trailingNewLine;
41
+ },
42
+ parse(content) {
43
+ return JSON.parse(content);
30
44
  }
31
- if (options.origins !== false && options.lineNumbers === false) {
32
- outputCatalog = removeLineNumbers(outputCatalog);
33
- }
34
- (0, _utils.writeFileIfChanged)(filename, JSON.stringify(outputCatalog, null, 2));
35
- },
36
- read(filename) {
37
- const raw = (0, _utils.readFile)(filename);
38
- if (!raw) {
39
- return null;
40
- }
41
- try {
42
- return JSON.parse(raw);
43
- } catch (e) {
44
- throw new Error(`Cannot read ${filename}: ${e.message}`);
45
- }
46
- },
47
- parse(content) {
48
- return content;
49
- }
50
- };
51
- var _default = lingui;
52
- exports.default = _default;
45
+ };
46
+ }
@@ -3,9 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var R = _interopRequireWildcard(require("ramda"));
8
- var _utils = require("../utils");
9
8
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
9
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
10
  const serialize = R.map(message => message.translation || "");
@@ -15,30 +14,18 @@ const deserialize = R.map(translation => ({
15
14
  message: null,
16
15
  origin: []
17
16
  }));
18
- const minimal = {
19
- catalogExtension: ".json",
20
- write(filename, catalog) {
21
- const messages = serialize(catalog);
22
- let file = (0, _utils.readFile)(filename);
23
- const shouldUseTrailingNewline = file === null || (file === null || file === void 0 ? void 0 : file.endsWith("\n"));
24
- const trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
25
- (0, _utils.writeFile)(filename, `${JSON.stringify(messages, null, 2)}${trailingNewLine}`);
26
- },
27
- read(filename) {
28
- const raw = (0, _utils.readFile)(filename);
29
- if (!raw) {
30
- return null;
17
+ function _default() {
18
+ return {
19
+ catalogExtension: ".json",
20
+ serialize(catalog, {
21
+ existing
22
+ }) {
23
+ const shouldUseTrailingNewline = existing === null || (existing === null || existing === void 0 ? void 0 : existing.endsWith("\n"));
24
+ const trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
25
+ return JSON.stringify(serialize(catalog), null, 2) + trailingNewLine;
26
+ },
27
+ parse(content) {
28
+ return deserialize(JSON.parse(content));
31
29
  }
32
- try {
33
- const rawCatalog = JSON.parse(raw);
34
- return deserialize(rawCatalog);
35
- } catch (e) {
36
- throw new Error(`Cannot read ${filename}: ${e.message}`);
37
- }
38
- },
39
- parse(content) {
40
- return deserialize(content);
41
- }
42
- };
43
- var _default = minimal;
44
- exports.default = _default;
30
+ };
31
+ }
@@ -3,25 +3,28 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.serialize = exports.default = void 0;
6
+ exports.default = _default;
7
+ exports.parse = parse;
8
+ exports.serialize = void 0;
7
9
  var _dateFns = require("date-fns");
8
10
  var _parser = require("@messageformat/parser");
9
11
  var _pluralsCldr = _interopRequireDefault(require("plurals-cldr"));
10
12
  var _pofile = _interopRequireDefault(require("pofile"));
11
13
  var _plurals = _interopRequireDefault(require("node-gettext/lib/plurals"));
12
- var _utils = require("../utils");
13
14
  var _po = require("./po");
14
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16
  // @ts-ignore
16
17
 
17
- function getCreateHeaders(language = "no") {
18
+ function getCreateHeaders(language) {
18
19
  return {
19
20
  "POT-Creation-Date": (0, _dateFns.format)(new Date(), "yyyy-MM-dd HH:mmxxxx"),
20
21
  "MIME-Version": "1.0",
21
22
  "Content-Type": "text/plain; charset=utf-8",
22
23
  "Content-Transfer-Encoding": "8bit",
23
24
  "X-Generator": "@lingui/cli",
24
- Language: language
25
+ ...(language ? {
26
+ Language: language
27
+ } : {})
25
28
  };
26
29
  }
27
30
 
@@ -181,45 +184,42 @@ const convertPluralsToICU = (item, pluralForms, lang) => {
181
184
  }
182
185
  item.msgstr = ["{" + pluralizeOn + ", plural, " + pluralClauses + "}"];
183
186
  };
184
- const poGettext = {
185
- catalogExtension: ".po",
186
- templateExtension: ".pot",
187
- write(filename, catalog, options) {
188
- let po;
189
- const raw = (0, _utils.readFile)(filename);
190
- if (raw) {
191
- po = _pofile.default.parse(raw);
192
- } else {
193
- po = new _pofile.default();
194
- po.headers = getCreateHeaders(options.locale);
195
- if (options.locale === undefined) {
196
- delete po.headers.Language;
187
+ function parse(raw) {
188
+ const po = _pofile.default.parse(raw);
189
+
190
+ // .po plurals are numbered 0-N and need to be mapped to ICU plural classes ("one", "few", "many"...). Different
191
+ // languages can have different plural classes (some start with "zero", some with "one"), so read that data from CLDR.
192
+ // `pluralForms` may be `null` if lang is not found. As long as no plural is used, don't report an error.
193
+ let pluralForms = getPluralCases(po.headers.Language);
194
+ return (0, _po.deserialize)(po.items, item => {
195
+ convertPluralsToICU(item, pluralForms, po.headers.Language);
196
+ });
197
+ }
198
+ function _default(options = {}) {
199
+ options = {
200
+ origins: true,
201
+ lineNumbers: true,
202
+ ...options
203
+ };
204
+ return {
205
+ catalogExtension: ".po",
206
+ templateExtension: ".pot",
207
+ parse(content) {
208
+ return parse(content);
209
+ },
210
+ serialize(catalog, ctx) {
211
+ let po;
212
+ if (ctx.existing) {
213
+ po = _pofile.default.parse(ctx.existing);
214
+ } else {
215
+ po = new _pofile.default();
216
+ po.headers = getCreateHeaders(ctx.locale)
217
+ // accessing private property
218
+ ;
219
+ po.headerOrder = Object.keys(po.headers);
197
220
  }
198
- // accessing private property
199
- ;
200
- po.headerOrder = Object.keys(po.headers);
221
+ po.items = serialize(catalog, options);
222
+ return po.toString();
201
223
  }
202
- po.items = serialize(catalog, options);
203
- (0, _utils.writeFileIfChanged)(filename, po.toString());
204
- },
205
- read(filename) {
206
- const raw = (0, _utils.readFile)(filename);
207
- if (!raw) {
208
- return null;
209
- }
210
- return this.parse(raw);
211
- },
212
- parse(raw) {
213
- const po = _pofile.default.parse(raw);
214
-
215
- // .po plurals are numbered 0-N and need to be mapped to ICU plural classes ("one", "few", "many"...). Different
216
- // languages can have different plural classes (some start with "zero", some with "one"), so read that data from CLDR.
217
- // `pluralForms` may be `null` if lang is not found. As long as no plural is used, don't report an error.
218
- let pluralForms = getPluralCases(po.headers.Language);
219
- return (0, _po.deserialize)(po.items, item => {
220
- convertPluralsToICU(item, pluralForms, po.headers.Language);
221
- });
222
- }
223
- };
224
- var _default = poGettext;
225
- exports.default = _default;
224
+ };
225
+ }