@lingui/format-po 4.11.2 → 5.0.0-next.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
@@ -39,7 +39,7 @@ export default {
39
39
 
40
40
  Possible options:
41
41
 
42
- ```ts
42
+ ````ts
43
43
  export type PoFormatterOptions = {
44
44
  /**
45
45
  * Print places where message is used
@@ -54,14 +54,14 @@ export type PoFormatterOptions = {
54
54
  * @default true
55
55
  */
56
56
  lineNumbers?: boolean
57
-
57
+
58
58
  /**
59
59
  * Print `js-lingui-id: Xs4as` statement in extracted comments section
60
60
  *
61
61
  * @default false
62
62
  */
63
63
  printLinguiId?: boolean
64
-
64
+
65
65
  /**
66
66
  * By default, the po-formatter treats the pair `msgid` + `msgctx` as the source
67
67
  * for generating an ID by hashing its value.
@@ -76,15 +76,39 @@ export type PoFormatterOptions = {
76
76
  * @default false
77
77
  */
78
78
  explicitIdAsDefault?: boolean
79
-
79
+
80
80
  /**
81
81
  * Custom attributes to append to the PO file header
82
82
  *
83
83
  * @default {}
84
84
  */
85
85
  customHeaderAttributes?: { [key: string]: string }
86
+
87
+ /**
88
+ * Print values for unnamed placeholders as comments for each message.
89
+ *
90
+ * This can give more context to translators for better translations.
91
+ *
92
+ * By default first 3 placeholders are shown.
93
+ *
94
+ * Example:
95
+ *
96
+ * ```js
97
+ * t`Hello ${user.name} ${value}`
98
+ * ```
99
+ *
100
+ * This will be extracted as
101
+ *
102
+ * ```po
103
+ * #. placeholder {0}: user.name
104
+ * msgid "Hello {0} {value}"
105
+ * ```
106
+ *
107
+ * @default true
108
+ */
109
+ printPlaceholdersInComments?: boolean | { limit?: number }
86
110
  }
87
- ```
111
+ ````
88
112
 
89
113
  ## License
90
114
 
package/dist/po.cjs CHANGED
@@ -8,6 +8,10 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
8
8
 
9
9
  const PO__default = /*#__PURE__*/_interopDefaultCompat(PO);
10
10
 
11
+ function normalizePlaceholderValue(text) {
12
+ return text.replace(/\n/g, " ").replace(/\s{2,}/g, " ");
13
+ }
14
+
11
15
  const splitOrigin = (origin) => {
12
16
  const [file, line] = origin.split(":");
13
17
  return [file, line ? Number(line) : null];
@@ -59,6 +63,23 @@ const serialize = (catalog, options) => {
59
63
  }
60
64
  item.msgid = id;
61
65
  }
66
+ if (options.printPlaceholdersInComments !== false) {
67
+ item.extractedComments = item.extractedComments.filter(
68
+ (comment) => !comment.startsWith("placeholder ")
69
+ );
70
+ const limit = typeof options.printPlaceholdersInComments === "object" && options.printPlaceholdersInComments.limit ? options.printPlaceholdersInComments.limit : 3;
71
+ if (message.placeholders) {
72
+ Object.entries(message.placeholders).forEach(([name, value]) => {
73
+ if (/^\d+$/.test(name)) {
74
+ value.slice(0, limit).forEach((entry) => {
75
+ item.extractedComments.push(
76
+ `placeholder {${name}}: ${normalizePlaceholderValue(entry)}`
77
+ );
78
+ });
79
+ }
80
+ });
81
+ }
82
+ }
62
83
  if (message.context) {
63
84
  item.msgctxt = message.context;
64
85
  }
package/dist/po.d.cts CHANGED
@@ -41,6 +41,31 @@ type PoFormatterOptions = {
41
41
  customHeaderAttributes?: {
42
42
  [key: string]: string;
43
43
  };
44
+ /**
45
+ * Print values for unnamed placeholders as comments for each message.
46
+ *
47
+ * This can give more context to translators for better translations.
48
+ *
49
+ * By default first 3 placeholders are shown.
50
+ *
51
+ * Example:
52
+ *
53
+ * ```js
54
+ * t`Hello ${user.name} ${value}`
55
+ * ```
56
+ *
57
+ * This will be extracted as
58
+ *
59
+ * ```po
60
+ * #. placeholder {0}: user.name
61
+ * msgid "Hello {0} {value}"
62
+ * ```
63
+ *
64
+ * @default true
65
+ */
66
+ printPlaceholdersInComments?: boolean | {
67
+ limit?: number;
68
+ };
44
69
  };
45
70
  declare function formatter(options?: PoFormatterOptions): CatalogFormatter;
46
71
 
package/dist/po.d.mts CHANGED
@@ -41,6 +41,31 @@ type PoFormatterOptions = {
41
41
  customHeaderAttributes?: {
42
42
  [key: string]: string;
43
43
  };
44
+ /**
45
+ * Print values for unnamed placeholders as comments for each message.
46
+ *
47
+ * This can give more context to translators for better translations.
48
+ *
49
+ * By default first 3 placeholders are shown.
50
+ *
51
+ * Example:
52
+ *
53
+ * ```js
54
+ * t`Hello ${user.name} ${value}`
55
+ * ```
56
+ *
57
+ * This will be extracted as
58
+ *
59
+ * ```po
60
+ * #. placeholder {0}: user.name
61
+ * msgid "Hello {0} {value}"
62
+ * ```
63
+ *
64
+ * @default true
65
+ */
66
+ printPlaceholdersInComments?: boolean | {
67
+ limit?: number;
68
+ };
44
69
  };
45
70
  declare function formatter(options?: PoFormatterOptions): CatalogFormatter;
46
71
 
package/dist/po.d.ts CHANGED
@@ -41,6 +41,31 @@ type PoFormatterOptions = {
41
41
  customHeaderAttributes?: {
42
42
  [key: string]: string;
43
43
  };
44
+ /**
45
+ * Print values for unnamed placeholders as comments for each message.
46
+ *
47
+ * This can give more context to translators for better translations.
48
+ *
49
+ * By default first 3 placeholders are shown.
50
+ *
51
+ * Example:
52
+ *
53
+ * ```js
54
+ * t`Hello ${user.name} ${value}`
55
+ * ```
56
+ *
57
+ * This will be extracted as
58
+ *
59
+ * ```po
60
+ * #. placeholder {0}: user.name
61
+ * msgid "Hello {0} {value}"
62
+ * ```
63
+ *
64
+ * @default true
65
+ */
66
+ printPlaceholdersInComments?: boolean | {
67
+ limit?: number;
68
+ };
44
69
  };
45
70
  declare function formatter(options?: PoFormatterOptions): CatalogFormatter;
46
71
 
package/dist/po.mjs CHANGED
@@ -2,6 +2,10 @@ import { format } from 'date-fns';
2
2
  import PO from 'pofile';
3
3
  import { generateMessageId } from '@lingui/message-utils/generateMessageId';
4
4
 
5
+ function normalizePlaceholderValue(text) {
6
+ return text.replace(/\n/g, " ").replace(/\s{2,}/g, " ");
7
+ }
8
+
5
9
  const splitOrigin = (origin) => {
6
10
  const [file, line] = origin.split(":");
7
11
  return [file, line ? Number(line) : null];
@@ -53,6 +57,23 @@ const serialize = (catalog, options) => {
53
57
  }
54
58
  item.msgid = id;
55
59
  }
60
+ if (options.printPlaceholdersInComments !== false) {
61
+ item.extractedComments = item.extractedComments.filter(
62
+ (comment) => !comment.startsWith("placeholder ")
63
+ );
64
+ const limit = typeof options.printPlaceholdersInComments === "object" && options.printPlaceholdersInComments.limit ? options.printPlaceholdersInComments.limit : 3;
65
+ if (message.placeholders) {
66
+ Object.entries(message.placeholders).forEach(([name, value]) => {
67
+ if (/^\d+$/.test(name)) {
68
+ value.slice(0, limit).forEach((entry) => {
69
+ item.extractedComments.push(
70
+ `placeholder {${name}}: ${normalizePlaceholderValue(entry)}`
71
+ );
72
+ });
73
+ }
74
+ });
75
+ }
76
+ }
56
77
  if (message.context) {
57
78
  item.msgctxt = message.context;
58
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/format-po",
3
- "version": "4.11.2",
3
+ "version": "5.0.0-next.0",
4
4
  "description": "Gettext PO format for Lingui Catalogs",
5
5
  "main": "./dist/po.cjs",
6
6
  "module": "./dist/po.mjs",
@@ -41,8 +41,8 @@
41
41
  "dist/"
42
42
  ],
43
43
  "dependencies": {
44
- "@lingui/conf": "4.11.2",
45
- "@lingui/message-utils": "4.11.2",
44
+ "@lingui/conf": "^5.0.0-next.0",
45
+ "@lingui/message-utils": "^5.0.0-next.0",
46
46
  "date-fns": "^3.6.0",
47
47
  "pofile": "^1.1.4"
48
48
  },
@@ -52,5 +52,5 @@
52
52
  "tsd": "^0.28.0",
53
53
  "unbuild": "2.0.0"
54
54
  },
55
- "gitHead": "17e0af54b0b9d9577db2cef1be680a6d871611e7"
55
+ "gitHead": "2192f8d54699a3846ff8fe6f3992697be2da68a8"
56
56
  }