Skip to content

Commit 57ed6c5

Browse files
committed
feat: add new intl formatting methods and update documentation
1 parent 9e9a9e0 commit 57ed6c5

2 files changed

Lines changed: 53 additions & 11 deletions

File tree

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,52 @@ You could make it as [peerDependency](https://github.com/alibaba/react-intl-univ
298298
* @returns {Object} options includes currentLocale and locales
299299
*/
300300
getInitOptions()
301+
302+
/**
303+
* Formats a list of React nodes for proper internationalized formatting.
304+
* @param {React.ReactNode[]} nodeList - Array of React nodes to format.
305+
* @param {Intl.ListFormatOptions} options - Intl.ListFormat options.
306+
* @returns {React.ReactNode[]} Array of React nodes formatted with locale-appropriate separators.
307+
*
308+
* @example
309+
* For en-US locale: formatList(["str1", "str2"]) => Returns: ["str1", ", ", "str2"] => Render as: "str1, str2" in React.js
310+
* For zh-CN locale: formatList(["str1", "str2"]) => Returns: ["str1", "", "str2"] => Render as: "str1、str2" in React.js
311+
*/
312+
formatList(nodeList, options)
313+
314+
/**
315+
* Returns locale-specific parentheses format for the current language.
316+
* @param {React.ReactNode} node - The content to be wrapped in parentheses.
317+
* @returns {ReactNode[]} An array containing left parenthesis, content, and right parenthesis.
318+
*
319+
* @example
320+
* For en-US locale: formatParentheses("str1") => Returns ["(", "str1", ")"] => Render as "(str1)" in React.js
321+
* For zh-CN locale: formatParentheses("str1") => Returns ["", "str1", ""] => Render as "(str1)" in React.js
322+
*/
323+
formatParentheses(node)
324+
325+
/**
326+
* Returns locale-specific colon character for the current language.
327+
* @returns {string} The locale-appropriate colon character.
328+
*
329+
* @example
330+
* For en-US locale: <>{intl.get("LABEL_NAME")}{intl.getColon()}{intl.get("VALUE")}</> => Returns "label: value"
331+
* For zh-CN locale: <>{intl.get("LABEL_NAME")}{intl.getColon()}{intl.get("VALUE")}</> => Returns "label:value"
332+
*/
333+
getColon()
334+
335+
/**
336+
* Formats a number according to the current locale.
337+
* @param {number} number - The number to format.
338+
* @returns {string} The formatted number.
339+
*
340+
* @example
341+
* For en-US locale: formatNumber(1234.56) => Returns "1,234.56"
342+
* For de-DE locale: formatNumber(1234.56) => Returns "1.234,567"
343+
* For fr-FR locale: formatNumber(1234.56) => Returns "1 234,567"
344+
*/
345+
formatNumber(number)
346+
301347
```
302348

303349

packages/react-intl-universal/typings/index.d.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,17 @@ declare module "react-intl-universal" {
100100
* @example
101101
* // For English locale (en-US):
102102
* formatList(["str1", "str2", "str3"])
103-
* // Returns: ['str1', ', ', 'str2', ', ', 'str3'] (with comma separators)
103+
* // Returns: ['str1', ', ', 'str2', ', ', 'str3'] (with comma separators) => Render as: ``str1, str2, str3``` in React.js
104104
*
105105
* @example
106106
* // For Chinese locale (zh-CN):
107107
* formatList(["str1", "str2", "str3"])
108-
* // Returns: ['str1', '、', 'str2', '、', 'str3'] (with ideographic comma separators)
108+
* // Returns: ['str1', '、', 'str2', '、', 'str3'] (with ideographic comma separators) => Render as: ``str1、str2、str3``` in React.js
109109
*
110110
* @example
111111
* // With custom options for disjunction (or) in English:
112112
* formatList(["str1", "str2", "str3"], { type: "disjunction" })
113-
* // Returns: ['str1', ', ', 'str2', ', or ', 'str3']
113+
* // Returns: ['str1', ', ', 'str2', ', or ', 'str3'] => Render as: ``str1, str2, or str3``` in React.js
114114
*/
115115
export function formatList(
116116
nodeList: React.ReactNode[],
@@ -186,23 +186,19 @@ declare module "react-intl-universal" {
186186
*
187187
* @example
188188
* // For English locale (en-US):
189-
* formatNumber(1234.56)
190-
* // => "1,234.56"
189+
* formatNumber(1234.56) // => "1,234.56"
191190
*
192191
* @example
193192
* // For German locale (de-DE):
194-
* formatNumber(1234.56)
195-
* // => "1.234,56"
193+
* formatNumber(1234.56) // => "1.234,56"
196194
*
197195
* @example
198196
* // For Chinese locale (zh-CN):
199-
* formatNumber(1234.56)
200-
* // => "1,234.56"
197+
* formatNumber(1234.56) // => "1,234.56"
201198
*
202199
* @example
203200
* // Invalid number input:
204-
* formatNumber("not-a-number")
205-
* // => "not-a-number"
201+
* formatNumber("not-a-number") // => "not-a-number"
206202
*/
207203
export function formatNumber(number: number): string;
208204

0 commit comments

Comments
 (0)