Skip to content

Commit 345ab66

Browse files
authored
docs: Clarify which characters can be used in ICU variables and select values (#967)
1 parent a92b13d commit 345ab66

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Diff for: docs/pages/docs/usage/messages.mdx

+23
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ Dynamic values can be inserted into messages by using curly braces:
167167
t('message', {name: 'Jane'}); // "Hello Jane!"
168168
```
169169

170+
<details>
171+
<summary>Which characters are supported for value names?</summary>
172+
173+
Value names are required to be alphanumeric and can contain underscores. All other characters, including dashes, are not supported.
174+
</details>
175+
170176
### Cardinal pluralization
171177

172178
To express the pluralization of a given number of items, the `plural` argument can be used:
@@ -249,6 +255,23 @@ t('message', {gender: 'female'}); // "She is online."
249255

250256
**Note**: The `other` case is required and will be used when none of the specific values match.
251257

258+
<details>
259+
<summary>Which characters are supported for select values?</summary>
260+
261+
Values are required to be alphanumeric and can contain underscores. All other characters, including dashes, are not supported.
262+
263+
Therefore, e.g. when you're mapping a locale to a human readable string, you should map the dash to an underscore first:
264+
265+
```tsx filename="en.json"
266+
"label": "{locale, select, en_BR {British English} en_US {American English} other {Unknown}"
267+
```
268+
269+
```tsx
270+
const locale = 'en-BR';
271+
t('message', {locale: locale.replaceAll('-', '_')};
272+
```
273+
</details>
274+
252275
### Escaping
253276
254277
Since curly braces are used for [interpolating dynamic values](/docs/usage/messages#interpolation-of-dynamic-values), you can escape them with the `'` marker to use the actual symbol in messages:

Diff for: packages/use-intl/test/core/createTranslator.test.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ it('handles formatting errors', () => {
4949
expect(result).toBe('price');
5050
});
5151

52+
it('supports alphanumeric value names', () => {
53+
const t = createTranslator({
54+
locale: 'en',
55+
messages: {label: '{val_u3}'}
56+
});
57+
58+
const result = t('label', {val_u3: 'Hello'});
59+
expect(result).toBe('Hello');
60+
});
61+
62+
it('throws an error for non-alphanumeric value names', () => {
63+
const onError = vi.fn();
64+
65+
const t = createTranslator({
66+
locale: 'en',
67+
messages: {label: '{val-u3}'},
68+
onError
69+
});
70+
71+
t('label', {'val-u3': 'Hello'});
72+
const error: IntlError = onError.mock.calls[0][0];
73+
expect(error.code).toBe('INVALID_MESSAGE');
74+
});
75+
5276
describe('t.rich', () => {
5377
it('can translate a message to a ReactNode', () => {
5478
const t = createTranslator({

0 commit comments

Comments
 (0)