File tree 2 files changed +47
-0
lines changed
packages/use-intl/test/core
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,12 @@ Dynamic values can be inserted into messages by using curly braces:
167
167
t (' message' , {name: ' Jane' }); // "Hello Jane!"
168
168
```
169
169
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
+
170
176
### Cardinal pluralization
171
177
172
178
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."
249
255
250
256
** Note** : The ` other ` case is required and will be used when none of the specific values match.
251
257
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
+
252
275
### Escaping
253
276
254
277
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:
Original file line number Diff line number Diff line change @@ -49,6 +49,30 @@ it('handles formatting errors', () => {
49
49
expect ( result ) . toBe ( 'price' ) ;
50
50
} ) ;
51
51
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
+
52
76
describe ( 't.rich' , ( ) => {
53
77
it ( 'can translate a message to a ReactNode' , ( ) => {
54
78
const t = createTranslator ( {
You can’t perform that action at this time.
0 commit comments