@lingui/core 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/dist/index.cjs CHANGED
@@ -297,10 +297,10 @@ class I18n extends EventEmitter {
297
297
  if (messageMissing) {
298
298
  this.emit("missing", { id, locale: this._locale });
299
299
  }
300
- let translation = messageForId || message || id;
301
- if (process.env.NODE_ENV !== "production") {
302
- translation = isString(translation) ? compileMessage.compileMessage(translation) : translation;
303
- }
300
+ const translation = messageForId || (() => {
301
+ const trans = message || id;
302
+ return process.env.NODE_ENV !== "production" ? compileMessage.compileMessage(trans) : trans;
303
+ })();
304
304
  if (isString(translation) && UNICODE_REGEX.test(translation))
305
305
  return JSON.parse(`"${translation}"`);
306
306
  if (isString(translation))
package/dist/index.mjs CHANGED
@@ -295,10 +295,10 @@ class I18n extends EventEmitter {
295
295
  if (messageMissing) {
296
296
  this.emit("missing", { id, locale: this._locale });
297
297
  }
298
- let translation = messageForId || message || id;
299
- if (process.env.NODE_ENV !== "production") {
300
- translation = isString(translation) ? compileMessage(translation) : translation;
301
- }
298
+ const translation = messageForId || (() => {
299
+ const trans = message || id;
300
+ return process.env.NODE_ENV !== "production" ? compileMessage(trans) : trans;
301
+ })();
302
302
  if (isString(translation) && UNICODE_REGEX.test(translation))
303
303
  return JSON.parse(`"${translation}"`);
304
304
  if (isString(translation))
@@ -0,0 +1,214 @@
1
+ import type { I18n, MessageDescriptor } from "@lingui/core"
2
+
3
+ export type ChoiceOptions = {
4
+ /** Offset of value when calculating plural forms */
5
+ offset?: number
6
+ zero?: string
7
+ one?: string
8
+ two?: string
9
+ few?: string
10
+ many?: string
11
+
12
+ /** Catch-all option */
13
+ other: string
14
+ /** Exact match form, corresponds to =N rule */
15
+ [digit: `${number}`]: string
16
+ }
17
+
18
+ type MacroMessageDescriptor = (
19
+ | {
20
+ id: string
21
+ message?: string
22
+ }
23
+ | {
24
+ id?: string
25
+ message: string
26
+ }
27
+ ) & {
28
+ comment?: string
29
+ context?: string
30
+ }
31
+
32
+ /**
33
+ * Translates a message descriptor
34
+ *
35
+ * @example
36
+ * ```
37
+ * import { t } from "@lingui/core/macro";
38
+ * const message = t({
39
+ * id: "msg.hello",
40
+ * comment: "Greetings at the homepage",
41
+ * message: `Hello ${name}`,
42
+ * });
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```
47
+ * import { t } from "@lingui/core/macro";
48
+ * const message = t({
49
+ * id: "msg.plural",
50
+ * message: plural(value, { one: "...", other: "..." }),
51
+ * });
52
+ * ```
53
+ *
54
+ * @param descriptor The message descriptor to translate
55
+ */
56
+ export function t(descriptor: MacroMessageDescriptor): string
57
+
58
+ /**
59
+ * Translates a template string using the global I18n instance
60
+ *
61
+ * @example
62
+ * ```
63
+ * import { t } from "@lingui/core/macro";
64
+ * const message = t`Hello ${name}`;
65
+ * ```
66
+ */
67
+ export function t(
68
+ literals: TemplateStringsArray,
69
+ ...placeholders: any[]
70
+ ): string
71
+
72
+ /**
73
+ * Translates a template string or message descriptor using a given I18n instance
74
+ *
75
+ * @example
76
+ * ```
77
+ * import { t } from "@lingui/core/macro";
78
+ * import { I18n } from "@lingui/core";
79
+ * const i18n = new I18n({
80
+ * locale: "nl",
81
+ * messages: { "Hello {0}": "Hallo {0}" },
82
+ * });
83
+ * const message = t(i18n)`Hello ${name}`;
84
+ * ```
85
+ *
86
+ * @example
87
+ * ```
88
+ * import { t } from "@lingui/core/macro";
89
+ * import { I18n } from "@lingui/core";
90
+ * const i18n = new I18n({
91
+ * locale: "nl",
92
+ * messages: { "Hello {0}": "Hallo {0}" },
93
+ * });
94
+ * const message = t(i18n)({ message: `Hello ${name}` });
95
+ * ```
96
+ */
97
+ export function t(i18n: I18n): {
98
+ (literals: TemplateStringsArray, ...placeholders: any[]): string
99
+ (descriptor: MacroMessageDescriptor): string
100
+ }
101
+
102
+ /**
103
+ * Pluralize a message
104
+ *
105
+ * @example
106
+ * ```
107
+ * import { plural } from "@lingui/core/macro";
108
+ * const message = plural(count, {
109
+ * one: "# Book",
110
+ * other: "# Books",
111
+ * });
112
+ * ```
113
+ *
114
+ * @param value Determines the plural form
115
+ * @param options Object with available plural forms
116
+ */
117
+ export function plural(value: number | string, options: ChoiceOptions): string
118
+
119
+ /**
120
+ * Pluralize a message using ordinal forms
121
+ *
122
+ * Similar to `plural` but instead of using cardinal plural forms,
123
+ * it uses ordinal forms.
124
+ *
125
+ * @example
126
+ * ```
127
+ * import { selectOrdinal } from "@lingui/core/macro";
128
+ * const message = selectOrdinal(count, {
129
+ * one: "#st",
130
+ * two: "#nd",
131
+ * few: "#rd",
132
+ * other: "#th",
133
+ * });
134
+ * ```
135
+ *
136
+ * @param value Determines the plural form
137
+ * @param options Object with available plural forms
138
+ */
139
+ export function selectOrdinal(
140
+ value: number | string,
141
+ options: ChoiceOptions
142
+ ): string
143
+
144
+ type SelectOptions = {
145
+ /** Catch-all option */
146
+ other: string
147
+ [matches: string]: string
148
+ }
149
+
150
+ /**
151
+ * Selects a translation based on a value
152
+ *
153
+ * Select works like a switch statement. It will
154
+ * select one of the forms in `options` object which
155
+ * key matches exactly `value`.
156
+ *
157
+ * @example
158
+ * ```
159
+ * import { select } from "@lingui/core/macro";
160
+ * const message = select(gender, {
161
+ * male: "he",
162
+ * female: "she",
163
+ * other: "they",
164
+ * });
165
+ * ```
166
+ *
167
+ * @param value The key of choices to use
168
+ * @param choices
169
+ */
170
+ export function select(value: string, choices: SelectOptions): string
171
+
172
+ /**
173
+ * Define a message for later use
174
+ *
175
+ * `defineMessage` can be used to add comments for translators,
176
+ * or to override the message ID.
177
+ *
178
+ * @example
179
+ * ```
180
+ * import { defineMessage } from "@lingui/core/macro";
181
+ * const message = defineMessage({
182
+ * comment: "Greetings on the welcome page",
183
+ * message: `Welcome, ${name}!`,
184
+ * });
185
+ * ```
186
+ *
187
+ * @param descriptor The message descriptor
188
+ */
189
+ export function defineMessage(
190
+ descriptor: MacroMessageDescriptor
191
+ ): MessageDescriptor
192
+
193
+ /**
194
+ * Define a message for later use
195
+ *
196
+ * @example
197
+ * ```
198
+ * import { defineMessage, msg } from "@lingui/core/macro";
199
+ * const message = defineMessage`Hello ${name}`;
200
+ *
201
+ * // or using shorter version
202
+ * const message = msg`Hello ${name}`;
203
+ * ```
204
+ */
205
+ export function defineMessage(
206
+ literals: TemplateStringsArray,
207
+ ...placeholders: any[]
208
+ ): MessageDescriptor
209
+
210
+ /**
211
+ * Define a message for later use
212
+ * Alias for {@see defineMessage}
213
+ */
214
+ export const msg: typeof defineMessage
package/macro/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("@lingui/babel-plugin-lingui-macro/macro")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/core",
3
- "version": "4.11.2",
3
+ "version": "5.0.0-next.0",
4
4
  "sideEffects": false,
5
5
  "description": "I18n tools for javascript",
6
6
  "main": "./dist/index.cjs",
@@ -45,21 +45,40 @@
45
45
  "default": "./dist/index.mjs"
46
46
  }
47
47
  },
48
+ "./macro": {
49
+ "types": "./macro/index.d.ts",
50
+ "default": "./macro/index.js"
51
+ },
48
52
  "./package.json": "./package.json"
49
53
  },
50
54
  "files": [
51
55
  "LICENSE",
52
56
  "README.md",
53
- "dist/"
57
+ "dist/",
58
+ "macro/index.d.ts",
59
+ "macro/index.js"
54
60
  ],
55
61
  "dependencies": {
56
62
  "@babel/runtime": "^7.20.13",
57
- "@lingui/message-utils": "4.11.2",
63
+ "@lingui/message-utils": "^5.0.0-next.0",
58
64
  "unraw": "^3.0.0"
59
65
  },
60
66
  "devDependencies": {
61
- "@lingui/jest-mocks": "*",
67
+ "@lingui/jest-mocks": "^3.0.3",
68
+ "tsd": "^0.26.1",
62
69
  "unbuild": "2.0.0"
63
70
  },
64
- "gitHead": "17e0af54b0b9d9577db2cef1be680a6d871611e7"
71
+ "peerDependencies": {
72
+ "@lingui/babel-plugin-lingui-macro": "5.0.0-next.0",
73
+ "babel-plugin-macros": "2 || 3"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "@lingui/babel-plugin-lingui-macro": {
77
+ "optional": true
78
+ },
79
+ "babel-plugin-macros": {
80
+ "optional": true
81
+ }
82
+ },
83
+ "gitHead": "2192f8d54699a3846ff8fe6f3992697be2da68a8"
65
84
  }