@lingui/format-po-gettext 4.11.4 → 4.13.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
@@ -13,7 +13,7 @@
13
13
 
14
14
  > **Warning**
15
15
  > This formatter is made for compatibility with translation management systems, which do not support ICU expressions in PO files.
16
- >
16
+ >
17
17
  > It does not support all features of LinguiJS and should be carefully considered over other formats.
18
18
  >
19
19
  > Not supported features (native gettext doesn't support this):
@@ -72,10 +72,17 @@ export type PoGettextFormatterOptions = {
72
72
 
73
73
  /**
74
74
  * Disable warning about unsupported `Select` feature encountered in catalogs
75
- *
75
+ *
76
76
  * @default false
77
77
  */
78
78
  disableSelectWarning?: boolean
79
+
80
+ /**
81
+ * Overrides the default prefix for icu and plural comments in the final PO catalog.
82
+ *
83
+ * @default "js-lingui:"
84
+ */
85
+ customICUPrefix?: string
79
86
  }
80
87
  ```
81
88
 
@@ -32,9 +32,10 @@ function stringifyICUCase(icuCase) {
32
32
  const ICU_PLURAL_REGEX = /^{.*, plural, .*}$/;
33
33
  const ICU_SELECT_REGEX = /^{.*, select(Ordinal)?, .*}$/;
34
34
  const LINE_ENDINGS = /\r?\n/g;
35
- const CTX_PREFIX = "js-lingui:";
35
+ const DEFAULT_CTX_PREFIX = "js-lingui:";
36
36
  function serializePlurals(item, message, id, isGeneratedId, options) {
37
37
  const icuMessage = message.message;
38
+ const ctxPrefix = options.customICUPrefix || DEFAULT_CTX_PREFIX;
38
39
  if (!icuMessage) {
39
40
  return item;
40
41
  }
@@ -63,7 +64,7 @@ function serializePlurals(item, message, id, isGeneratedId, options) {
63
64
  item.msgid_plural = id + "_plural";
64
65
  }
65
66
  ctx.sort();
66
- item.extractedComments.push(CTX_PREFIX + ctx.toString());
67
+ item.extractedComments.push(ctxPrefix + ctx.toString());
67
68
  if (message.translation?.length > 0) {
68
69
  const ast = parser.parse(message.translation)[0];
69
70
  if (ast.cases == null) {
@@ -96,7 +97,7 @@ const getPluralCases = (lang) => {
96
97
  (pluralCase) => pluralsCldr__default(correctLang, pluralCase.sample)
97
98
  );
98
99
  };
99
- const convertPluralsToICU = (item, pluralForms, lang) => {
100
+ const convertPluralsToICU = (item, pluralForms, lang, ctxPrefix = DEFAULT_CTX_PREFIX) => {
100
101
  const translationCount = item.msgstr.length;
101
102
  const messageKey = item.msgid;
102
103
  if (translationCount <= 1 && !item.msgid_plural) {
@@ -109,11 +110,11 @@ const convertPluralsToICU = (item, pluralForms, lang) => {
109
110
  );
110
111
  return;
111
112
  }
112
- const contextComment = item.extractedComments.find((comment) => comment.startsWith(CTX_PREFIX))?.substr(CTX_PREFIX.length);
113
+ const contextComment = item.extractedComments.find((comment) => comment.startsWith(ctxPrefix))?.substring(ctxPrefix.length);
113
114
  const ctx = new URLSearchParams(contextComment);
114
115
  if (contextComment != null) {
115
116
  item.extractedComments = item.extractedComments.filter(
116
- (comment) => !comment.startsWith(CTX_PREFIX)
117
+ (comment) => !comment.startsWith(ctxPrefix)
117
118
  );
118
119
  }
119
120
  const storedICU = ctx.get("icu");
@@ -141,7 +142,7 @@ const convertPluralsToICU = (item, pluralForms, lang) => {
141
142
  let pluralizeOn = ctx.get("pluralize_on");
142
143
  if (!pluralizeOn) {
143
144
  console.warn(
144
- `Unable to determine plural placeholder name for item with key "%s" in language "${lang}" (should be stored in a comment starting with "#. ${CTX_PREFIX}"), assuming "count".`,
145
+ `Unable to determine plural placeholder name for item with key "%s" in language "${lang}" (should be stored in a comment starting with "#. ${ctxPrefix}"), assuming "count".`,
145
146
  messageKey
146
147
  );
147
148
  pluralizeOn = "count";
@@ -162,7 +163,12 @@ function formatter(options = {}) {
162
163
  const po = PO__default.parse(content);
163
164
  let pluralForms = getPluralCases(po.headers.Language);
164
165
  po.items.forEach((item) => {
165
- convertPluralsToICU(item, pluralForms, po.headers.Language);
166
+ convertPluralsToICU(
167
+ item,
168
+ pluralForms,
169
+ po.headers.Language,
170
+ options.customICUPrefix
171
+ );
166
172
  });
167
173
  return formatter2.parse(po.toString(), ctx);
168
174
  },
@@ -3,6 +3,7 @@ import { PoFormatterOptions } from '@lingui/format-po';
3
3
 
4
4
  type PoGettextFormatterOptions = PoFormatterOptions & {
5
5
  disableSelectWarning?: boolean;
6
+ customICUPrefix?: string;
6
7
  };
7
8
  declare function formatter(options?: PoGettextFormatterOptions): CatalogFormatter;
8
9
 
@@ -3,6 +3,7 @@ import { PoFormatterOptions } from '@lingui/format-po';
3
3
 
4
4
  type PoGettextFormatterOptions = PoFormatterOptions & {
5
5
  disableSelectWarning?: boolean;
6
+ customICUPrefix?: string;
6
7
  };
7
8
  declare function formatter(options?: PoGettextFormatterOptions): CatalogFormatter;
8
9
 
@@ -3,6 +3,7 @@ import { PoFormatterOptions } from '@lingui/format-po';
3
3
 
4
4
  type PoGettextFormatterOptions = PoFormatterOptions & {
5
5
  disableSelectWarning?: boolean;
6
+ customICUPrefix?: string;
6
7
  };
7
8
  declare function formatter(options?: PoGettextFormatterOptions): CatalogFormatter;
8
9
 
@@ -24,9 +24,10 @@ function stringifyICUCase(icuCase) {
24
24
  const ICU_PLURAL_REGEX = /^{.*, plural, .*}$/;
25
25
  const ICU_SELECT_REGEX = /^{.*, select(Ordinal)?, .*}$/;
26
26
  const LINE_ENDINGS = /\r?\n/g;
27
- const CTX_PREFIX = "js-lingui:";
27
+ const DEFAULT_CTX_PREFIX = "js-lingui:";
28
28
  function serializePlurals(item, message, id, isGeneratedId, options) {
29
29
  const icuMessage = message.message;
30
+ const ctxPrefix = options.customICUPrefix || DEFAULT_CTX_PREFIX;
30
31
  if (!icuMessage) {
31
32
  return item;
32
33
  }
@@ -55,7 +56,7 @@ function serializePlurals(item, message, id, isGeneratedId, options) {
55
56
  item.msgid_plural = id + "_plural";
56
57
  }
57
58
  ctx.sort();
58
- item.extractedComments.push(CTX_PREFIX + ctx.toString());
59
+ item.extractedComments.push(ctxPrefix + ctx.toString());
59
60
  if (message.translation?.length > 0) {
60
61
  const ast = parse(message.translation)[0];
61
62
  if (ast.cases == null) {
@@ -88,7 +89,7 @@ const getPluralCases = (lang) => {
88
89
  (pluralCase) => pluralsCldr(correctLang, pluralCase.sample)
89
90
  );
90
91
  };
91
- const convertPluralsToICU = (item, pluralForms, lang) => {
92
+ const convertPluralsToICU = (item, pluralForms, lang, ctxPrefix = DEFAULT_CTX_PREFIX) => {
92
93
  const translationCount = item.msgstr.length;
93
94
  const messageKey = item.msgid;
94
95
  if (translationCount <= 1 && !item.msgid_plural) {
@@ -101,11 +102,11 @@ const convertPluralsToICU = (item, pluralForms, lang) => {
101
102
  );
102
103
  return;
103
104
  }
104
- const contextComment = item.extractedComments.find((comment) => comment.startsWith(CTX_PREFIX))?.substr(CTX_PREFIX.length);
105
+ const contextComment = item.extractedComments.find((comment) => comment.startsWith(ctxPrefix))?.substring(ctxPrefix.length);
105
106
  const ctx = new URLSearchParams(contextComment);
106
107
  if (contextComment != null) {
107
108
  item.extractedComments = item.extractedComments.filter(
108
- (comment) => !comment.startsWith(CTX_PREFIX)
109
+ (comment) => !comment.startsWith(ctxPrefix)
109
110
  );
110
111
  }
111
112
  const storedICU = ctx.get("icu");
@@ -133,7 +134,7 @@ const convertPluralsToICU = (item, pluralForms, lang) => {
133
134
  let pluralizeOn = ctx.get("pluralize_on");
134
135
  if (!pluralizeOn) {
135
136
  console.warn(
136
- `Unable to determine plural placeholder name for item with key "%s" in language "${lang}" (should be stored in a comment starting with "#. ${CTX_PREFIX}"), assuming "count".`,
137
+ `Unable to determine plural placeholder name for item with key "%s" in language "${lang}" (should be stored in a comment starting with "#. ${ctxPrefix}"), assuming "count".`,
137
138
  messageKey
138
139
  );
139
140
  pluralizeOn = "count";
@@ -154,7 +155,12 @@ function formatter(options = {}) {
154
155
  const po = PO.parse(content);
155
156
  let pluralForms = getPluralCases(po.headers.Language);
156
157
  po.items.forEach((item) => {
157
- convertPluralsToICU(item, pluralForms, po.headers.Language);
158
+ convertPluralsToICU(
159
+ item,
160
+ pluralForms,
161
+ po.headers.Language,
162
+ options.customICUPrefix
163
+ );
158
164
  });
159
165
  return formatter2.parse(po.toString(), ctx);
160
166
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/format-po-gettext",
3
- "version": "4.11.4",
3
+ "version": "4.13.0",
4
4
  "description": "Gettext PO format with gettext-style plurals for Lingui Catalogs",
5
5
  "main": "./dist/po-gettext.cjs",
6
6
  "module": "./dist/po-gettext.mjs",
@@ -41,9 +41,9 @@
41
41
  "dist/"
42
42
  ],
43
43
  "dependencies": {
44
- "@lingui/conf": "4.11.4",
45
- "@lingui/format-po": "4.11.4",
46
- "@lingui/message-utils": "4.11.4",
44
+ "@lingui/conf": "4.13.0",
45
+ "@lingui/format-po": "4.13.0",
46
+ "@lingui/message-utils": "4.13.0",
47
47
  "@messageformat/parser": "^5.0.0",
48
48
  "node-gettext": "^3.0.0",
49
49
  "plurals-cldr": "^2.0.1",
@@ -55,5 +55,5 @@
55
55
  "tsd": "^0.28.0",
56
56
  "unbuild": "2.0.0"
57
57
  },
58
- "gitHead": "43d4e671360e8e5f1b820e4e061c1926d95c120b"
58
+ "gitHead": "263faa5b8cbe07ea1bbe4f5902794f29b773984d"
59
59
  }