@lingui/cli 3.14.0 → 3.15.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/api/catalog.js CHANGED
@@ -326,46 +326,49 @@ var Catalog = /*#__PURE__*/function () {
326
326
  var _this2 = this;
327
327
 
328
328
  var catalogs = this.readAll();
329
+ var template = this.readTemplate() || {};
329
330
  return R.mapObjIndexed(function (_value, key) {
330
331
  return _this2.getTranslation(catalogs, locale, key, options);
331
- }, catalogs[locale]);
332
+ }, _objectSpread(_objectSpread({}, template), catalogs[locale]));
332
333
  }
333
334
  }, {
334
335
  key: "getTranslation",
335
336
  value: function getTranslation(catalogs, locale, key, _ref4) {
336
- var _this3 = this;
337
+ var _this3 = this,
338
+ _catalog$key;
337
339
 
338
340
  var fallbackLocales = _ref4.fallbackLocales,
339
341
  sourceLocale = _ref4.sourceLocale;
342
+ var catalog = catalogs[locale] || {};
340
343
 
341
- if (!catalogs[locale].hasOwnProperty(key)) {
344
+ if (!catalog.hasOwnProperty(key)) {
342
345
  console.error("Message with key ".concat(key, " is missing in locale ").concat(locale));
343
346
  }
344
347
 
345
- var getTranslation = function getTranslation(locale) {
348
+ var getTranslation = function getTranslation(_locale) {
346
349
  var configLocales = _this3.config.locales.join('", "');
347
350
 
348
- var localeCatalog = catalogs[locale];
351
+ var localeCatalog = catalogs[_locale] || {};
349
352
 
350
353
  if (!localeCatalog) {
351
- console.warn("\n Catalog \"".concat(locale, "\" isn't present in locales config parameter\n Add \"").concat(locale, "\" to your lingui.config.js:\n {\n locales: [\"").concat(configLocales, "\", \"").concat(locale, "\"]\n }\n "));
354
+ console.warn("\n Catalog \"".concat(_locale, "\" isn't present in locales config parameter\n Add \"").concat(_locale, "\" to your lingui.config.js:\n {\n locales: [\"").concat(configLocales, "\", \"").concat(_locale, "\"]\n }\n "));
352
355
  return null;
353
356
  }
354
357
 
355
358
  if (!localeCatalog.hasOwnProperty(key)) {
356
- console.error("Message with key ".concat(key, " is missing in locale ").concat(locale));
359
+ console.error("Message with key ".concat(key, " is missing in locale ").concat(_locale));
357
360
  return null;
358
361
  }
359
362
 
360
- if (catalogs[locale]) {
361
- return catalogs[locale][key].translation;
363
+ if (catalogs[_locale]) {
364
+ return catalogs[_locale][key].translation;
362
365
  }
363
366
 
364
367
  return null;
365
368
  };
366
369
 
367
- var getMultipleFallbacks = function getMultipleFallbacks(locale) {
368
- var fL = fallbackLocales && fallbackLocales[locale]; // some probably the fallback will be undefined, so just search by locale
370
+ var getMultipleFallbacks = function getMultipleFallbacks(_locale) {
371
+ var fL = fallbackLocales && fallbackLocales[_locale]; // some probably the fallback will be undefined, so just search by locale
369
372
 
370
373
  if (!fL) return null;
371
374
 
@@ -394,9 +397,9 @@ var Catalog = /*#__PURE__*/function () {
394
397
  return (// Get translation in target locale
395
398
  getTranslation(locale) || // We search in fallbackLocales as dependent of each locale
396
399
  getMultipleFallbacks(locale) || // Get translation in fallbackLocales.default (if any)
397
- fallbackLocales && fallbackLocales.default && getTranslation(fallbackLocales.default) || // Get message default
398
- catalogs[locale][key].defaults || // If sourceLocale is either target locale of fallback one, use key
399
- sourceLocale && sourceLocale === locale && key || sourceLocale && fallbackLocales && fallbackLocales.default && sourceLocale === fallbackLocales.default && key || // Otherwise no translation is available
400
+ (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && getTranslation(fallbackLocales.default) || ( // Get message default
401
+ (_catalog$key = catalog[key]) === null || _catalog$key === void 0 ? void 0 : _catalog$key.defaults) || // If sourceLocale is either target locale of fallback one, use key
402
+ sourceLocale && sourceLocale === locale && key || sourceLocale && (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && sourceLocale === fallbackLocales.default && key || // Otherwise no translation is available
400
403
  undefined
401
404
  );
402
405
  }
package/api/compile.js CHANGED
@@ -35,11 +35,7 @@ var INVALID_OBJECT_KEY_REGEX = /^(\d+[a-zA-Z]|[a-zA-Z]+\d)(\d|[a-zA-Z])*/;
35
35
  * applying pseudolocalization where necessary.
36
36
  */
37
37
  function compileSingleKey(key, translation, shouldPseudolocalize) {
38
- if (shouldPseudolocalize) {
39
- translation = (0, _pseudoLocalize.default)(key);
40
- }
41
-
42
- return t.objectProperty(t.stringLiteral(key), compile(translation));
38
+ return t.objectProperty(t.stringLiteral(key), compile(translation, shouldPseudolocalize));
43
39
  }
44
40
 
45
41
  function createCompiledCatalog(locale, messages, options) {
@@ -107,6 +103,7 @@ function buildExportStatement(expression, namespace) {
107
103
 
108
104
 
109
105
  function compile(message) {
106
+ var shouldPseudolocalize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
110
107
  var tokens;
111
108
 
112
109
  try {
@@ -115,23 +112,29 @@ function compile(message) {
115
112
  throw new Error("Can't parse message. Please check correct syntax: \"".concat(message, "\" \n \n Messageformat-parser trace: ").concat(e.message));
116
113
  }
117
114
 
118
- var ast = processTokens(tokens);
115
+ var ast = processTokens(tokens, shouldPseudolocalize);
119
116
  if (isString(ast)) return t.stringLiteral(ast);
120
117
  return ast;
121
118
  }
122
119
 
123
- function processTokens(tokens) {
120
+ function processTokens(tokens, shouldPseudolocalize) {
124
121
  // Shortcut - if the message doesn't include any formatting,
125
122
  // simply join all string chunks into one message
126
123
  if (!tokens.filter(function (token) {
127
124
  return !isString(token);
128
125
  }).length) {
129
- return tokens.join("");
126
+ if (shouldPseudolocalize) {
127
+ return tokens.map(function (token) {
128
+ return (0, _pseudoLocalize.default)(token);
129
+ }).join("");
130
+ } else {
131
+ return tokens.join("");
132
+ }
130
133
  }
131
134
 
132
135
  return t.arrayExpression(tokens.map(function (token) {
133
136
  if (isString(token)) {
134
- return t.stringLiteral(token); // # in plural case
137
+ return t.stringLiteral(shouldPseudolocalize ? (0, _pseudoLocalize.default)(token) : token); // # in plural case
135
138
  } else if (token.type === "octothorpe") {
136
139
  return t.stringLiteral("#"); // simple argument
137
140
  } else if (token.type === "argument") {
@@ -155,7 +158,7 @@ function processTokens(tokens) {
155
158
  }
156
159
 
157
160
  token.cases.forEach(function (item) {
158
- var inlineTokens = processTokens(item.tokens);
161
+ var inlineTokens = processTokens(item.tokens, shouldPseudolocalize);
159
162
  formatProps.push(t.objectProperty( // if starts with number must be wrapped with quotes
160
163
  INVALID_OBJECT_KEY_REGEX.test(item.key) ? t.stringLiteral(item.key) : t.identifier(item.key), isString(inlineTokens) ? t.stringLiteral(inlineTokens) : inlineTokens));
161
164
  });
@@ -13,8 +13,6 @@ var _fs = _interopRequireDefault(require("fs"));
13
13
 
14
14
  var R = _interopRequireWildcard(require("ramda"));
15
15
 
16
- var _utils = require("../utils");
17
-
18
16
  var serialize = R.map(function (message) {
19
17
  return message.translation || "";
20
18
  });
@@ -29,8 +27,23 @@ var deserialize = R.map(function (translation) {
29
27
  var minimal = {
30
28
  catalogExtension: ".json",
31
29
  write: function write(filename, catalog) {
30
+ var _file;
31
+
32
32
  var messages = serialize(catalog);
33
- (0, _utils.writeFileIfChanged)(filename, JSON.stringify(messages, null, 2));
33
+ var file = null;
34
+
35
+ try {
36
+ file = _fs.default.readFileSync(filename, 'utf8');
37
+ } catch (error) {
38
+ if (error.code !== "ENOENT") {
39
+ throw error;
40
+ }
41
+ }
42
+
43
+ var shouldUseTrailingNewline = file === null || ((_file = file) === null || _file === void 0 ? void 0 : _file.endsWith("\n"));
44
+ var trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
45
+
46
+ _fs.default.writeFileSync(filename, "".concat(JSON.stringify(messages, null, 2)).concat(trailingNewLine));
34
47
  },
35
48
  read: function read(filename) {
36
49
  var raw = _fs.default.readFileSync(filename).toString();
package/lingui-compile.js CHANGED
@@ -57,7 +57,6 @@ function command(config, options) {
57
57
  if (locale !== config.pseudoLocale && !plurals[language]) {
58
58
  console.log(_chalk.default.red("Error: Invalid locale ".concat(_chalk.default.bold(locale), " (missing plural rules)!")));
59
59
  console.error();
60
- process.exit(1);
61
60
  }
62
61
 
63
62
  catalogs.forEach(function (catalog) {
@@ -9,8 +9,12 @@ exports.default = command;
9
9
 
10
10
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
11
 
12
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
13
+
12
14
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
15
 
16
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
17
+
14
18
  var _chalk = _interopRequireDefault(require("chalk"));
15
19
 
16
20
  var _commander = _interopRequireDefault(require("commander"));
@@ -25,36 +29,83 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
25
29
 
26
30
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
27
31
 
28
- function command(config, options) {
29
- // `react-app` babel plugin used by CRA requires either BABEL_ENV or NODE_ENV to be
30
- // set. We're setting it here, because lingui macros are going to use them as well.
31
- if (!process.env.BABEL_ENV && !process.env.NODE_ENV) {
32
- process.env.BABEL_ENV = "development";
33
- } // We need macros to keep imports, so extract-messages plugin know what componets
34
- // to collect. Users usually use both BABEN_ENV and NODE_ENV, so it's probably
35
- // safer to introduce a new env variable. LINGUI_EXTRACT=1 during `lingui extract`
36
-
37
-
38
- process.env.LINGUI_EXTRACT = "1";
39
- options.verbose && console.error("Extracting messages from source files…");
40
- var catalogs = (0, _catalog.getCatalogs)(config);
41
- var catalogStats = {};
42
- catalogs.forEach(function (catalog) {
43
- catalog.makeTemplate(_objectSpread(_objectSpread({}, options), {}, {
44
- orderBy: config.orderBy,
45
- projectType: (0, _detect.detect)()
46
- }));
47
- catalogStats[catalog.templateFile] = Object.keys(catalog.readTemplate()).length;
48
- });
49
- Object.entries(catalogStats).forEach(function (_ref) {
50
- var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
51
- key = _ref2[0],
52
- value = _ref2[1];
32
+ function command(_x, _x2) {
33
+ return _command.apply(this, arguments);
34
+ }
53
35
 
54
- console.log("Catalog statistics for ".concat(_chalk.default.bold(key), ": ").concat(_chalk.default.green(value), " messages"));
55
- console.log();
56
- });
57
- return true;
36
+ function _command() {
37
+ _command = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(config, options) {
38
+ var catalogs, catalogStats;
39
+ return _regenerator.default.wrap(function _callee2$(_context2) {
40
+ while (1) {
41
+ switch (_context2.prev = _context2.next) {
42
+ case 0:
43
+ // `react-app` babel plugin used by CRA requires either BABEL_ENV or NODE_ENV to be
44
+ // set. We're setting it here, because lingui macros are going to use them as well.
45
+ if (!process.env.BABEL_ENV && !process.env.NODE_ENV) {
46
+ process.env.BABEL_ENV = "development";
47
+ } // We need macros to keep imports, so extract-messages plugin know what componets
48
+ // to collect. Users usually use both BABEN_ENV and NODE_ENV, so it's probably
49
+ // safer to introduce a new env variable. LINGUI_EXTRACT=1 during `lingui extract`
50
+
51
+
52
+ process.env.LINGUI_EXTRACT = "1";
53
+ options.verbose && console.error("Extracting messages from source files…");
54
+ catalogs = (0, _catalog.getCatalogs)(config);
55
+ catalogStats = {};
56
+ _context2.next = 7;
57
+ return Promise.all(catalogs.map( /*#__PURE__*/function () {
58
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(catalog) {
59
+ var catalogTemplate;
60
+ return _regenerator.default.wrap(function _callee$(_context) {
61
+ while (1) {
62
+ switch (_context.prev = _context.next) {
63
+ case 0:
64
+ _context.next = 2;
65
+ return catalog.makeTemplate(_objectSpread(_objectSpread({}, options), {}, {
66
+ orderBy: config.orderBy,
67
+ projectType: (0, _detect.detect)()
68
+ }));
69
+
70
+ case 2:
71
+ catalogTemplate = catalog.readTemplate();
72
+
73
+ if (catalogTemplate !== null && catalogTemplate !== undefined) {
74
+ catalogStats[catalog.templateFile] = Object.keys(catalogTemplate).length;
75
+ }
76
+
77
+ case 4:
78
+ case "end":
79
+ return _context.stop();
80
+ }
81
+ }
82
+ }, _callee);
83
+ }));
84
+
85
+ return function (_x3) {
86
+ return _ref.apply(this, arguments);
87
+ };
88
+ }()));
89
+
90
+ case 7:
91
+ Object.entries(catalogStats).forEach(function (_ref2) {
92
+ var _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
93
+ key = _ref3[0],
94
+ value = _ref3[1];
95
+
96
+ console.log("Catalog statistics for ".concat(_chalk.default.bold(key), ": ").concat(_chalk.default.green(value), " messages"));
97
+ console.log();
98
+ });
99
+ return _context2.abrupt("return", true);
100
+
101
+ case 9:
102
+ case "end":
103
+ return _context2.stop();
104
+ }
105
+ }
106
+ }, _callee2);
107
+ }));
108
+ return _command.apply(this, arguments);
58
109
  }
59
110
 
60
111
  if (require.main === module) {
@@ -66,6 +117,7 @@ if (require.main === module) {
66
117
  var result = command(config, {
67
118
  verbose: _commander.default.verbose || false,
68
119
  configPath: _commander.default.config || process.env.LINGUI_CONFIG
120
+ }).then(function () {
121
+ if (!result) process.exit(1);
69
122
  });
70
- if (!result) process.exit(1);
71
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/cli",
3
- "version": "3.14.0",
3
+ "version": "3.15.0",
4
4
  "description": "CLI for working wit message catalogs",
5
5
  "keywords": [
6
6
  "cli",
@@ -39,8 +39,8 @@
39
39
  "@babel/plugin-syntax-jsx": "^7.10.4",
40
40
  "@babel/runtime": "^7.11.2",
41
41
  "@babel/types": "^7.11.5",
42
- "@lingui/babel-plugin-extract-messages": "^3.14.0",
43
- "@lingui/conf": "^3.14.0",
42
+ "@lingui/babel-plugin-extract-messages": "^3.15.0",
43
+ "@lingui/conf": "^3.15.0",
44
44
  "babel-plugin-macros": "^3.0.1",
45
45
  "bcp-47": "^1.0.7",
46
46
  "chalk": "^4.1.0",
@@ -76,7 +76,7 @@
76
76
  },
77
77
  "peerDependencies": {
78
78
  "@babel/core": "^7.0.0",
79
- "babel-plugin-macros": "2 || 3",
79
+ "babel-plugin-macros": "2 || 3",
80
80
  "typescript": "2 || 3 || 4"
81
81
  }
82
82
  }