This commit is contained in:
David Štaleker
2025-07-18 05:33:16 +02:00
parent 401a367e5d
commit db0cc8d3de
14776 changed files with 9251484 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
## .loadMessages( json )
Load messages data.
The first level of keys must be locales. For example:
```
{
en: {
hello: "Hello"
},
pt: {
hello: "Olá"
}
}
```
ICU MessageFormat pattern is supported: variable replacement, gender and plural inflections. For more information see [`.messageFormatter( path ) ➡ function([ variables ])`](./message-formatter.md).
The provided messages are stored along side other cldr data, under the "globalize-messages" key. This allows Globalize to reuse the traversal methods provided by cldrjs. You can inspect this data using `cldrjs.get("globalize-messages")`.
### Parameters
#### json
JSON object of messages data. Keys can use any character, except `/`, `{` and `}`. Values (i.e., the message content itself) can contain any character.
### Example
```javascript
Globalize.loadMessages({
pt: {
greetings: {
hello: "Olá",
bye: "Tchau"
}
}
});
Globalize( "pt" ).formatMessage( "greetings/hello" );
// > Olá
```
#### Multiline strings
Use Arrays as a convenience for multiline strings. The lines will be joined by a space.
```javascript
Globalize.loadMessages({
en: {
longText: [
"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi non",
"quis exercitationem culpa nesciunt nihil aut nostrum explicabo",
"reprehenderit optio amet ab temporibus asperiores quasi cupiditate.",
"Voluptatum ducimus voluptates voluptas?"
]
}
});
Globalize( "en" ).formatMessage( "longText" );
// > "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi non quis exercitationem culpa nesciunt nihil aut nostrum explicabo reprehenderit optio amet ab temporibus asperiores quasi cupiditate. Voluptatum ducimus voluptates voluptas?"
```
#### Messages inheritance
It's possible to inherit messages, for example:
```javascript
Globalize.loadMessages({
root: {
amen: "Amen"
},
de: {},
en: {},
"en-GB": {},
fr: {},
pt: {
amen: "Amém"
},
"pt-PT": {}
});
Globalize( "de" ).formatMessage( "amen" );
// > "Amen"
Globalize( "en" ).formatMessage( "amen" );
// > "Amen"
Globalize( "en-GB" ).formatMessage( "amen" );
// > "Amen"
Globalize( "fr" ).formatMessage( "amen" );
// > "Amen"
Globalize( "pt-PT" ).formatMessage( "amen" );
// > "Amém"
```
Note that `de`, `en`, `en-GB`, `fr`, and `pt-PT` are empty. `.formatMessage()` inherits `pt-PT` messages from `pt` (`pt-PT``pt`), and it inherits the other messages from root, eg. `en-GB``en-001``en``root`. Yes, `root` is the last bundle of the parent lookup.
Attention: On browsers, message inheritance only works if the optional dependency `cldr/unresolved` is loaded.
```html
<script src="cldr/unresolved.js"></script>
```

View File

@@ -0,0 +1,208 @@
## .messageFormatter( path ) ➡ function([ variables ])
Return a function that formats a message (using ICU message format pattern) given its path and a set of variables into a user-readable string. It supports pluralization and gender inflections.
Use [`Globalize.loadMessages( json )`](./load-messages.md) to load
messages data.
### Parameters
#### path
String or Array containing the path of the message content, eg., `"greetings/bye"`, or `[ "greetings", "bye" ]`.
#### variables
Optional. Variables can be Objects, where each property can be referenced by name inside a message; or Arrays, where each entry of the Array can be used inside a message, using numeric indices. When passing one or more arguments of other types, they're converted to an Array and used as such.
### Example
You can use the static method `Globalize.messageFormatter()`, which uses the default locale.
```javascript
var formatter;
Globalize.loadMessages({
pt: {
greetings: {
bye: "Tchau"
}
}
});
Globalize.locale( "pt" );
formatter = Globalize.messageFormatter( "greetings/bye" );
formatter();
// > "Tchau"
```
You can use the instance method `.messageFormatter()`, which uses the instance locale.
```javascript
var pt = new Globalize( "pt" ),
formatter = pt.messageFormatter( "greetings/bye" );
formatter();
// > "Tchau"
```
#### Simple Variable Replacement
```javascript
var formatter;
Globalize.loadMessages({
en: {
hello: "Hello, {0} {1} {2}",
hey: "Hey, {first} {middle} {last}"
}
});
formatter = Globalize( "en" ).messageFormatter( "hello" );
// Numbered variables using Array.
formatter([ "Wolfgang", "Amadeus", "Mozart" ]);
// > "Hello, Wolfgang Amadeus Mozart"
// Numbered variables using function arguments.
formatter( "Wolfgang", "Amadeus", "Mozart" );
// > "Hello, Wolfgang Amadeus Mozart"
// Named variables using Object key-value pairs.
formatter = Globalize( "en" ).messageFormatter( "hey" );
formatter({
first: "Wolfgang",
middle: "Amadeus",
last: "Mozart"
});
// > "Hey, Wolfgang Amadeus Mozart"
```
#### Gender inflections
`select` can be used to format any message variations that works like a switch.
```javascript
var formatter;
// Note you can define multiple lines message using an Array of Strings.
Globalize.loadMessages({
en: {
party: [
"{hostGender, select,",
" female {{host} invites {guest} to her party}",
" male {{host} invites {guest} to his party}",
" other {{host} invites {guest} to their party}",
"}"
]
}
});
formatter = Globalize( "en" ).messageFormatter( "party" );
formatter({
guest: "Mozart",
host: "Beethoven",
hostGender: "male"
});
// > "Beethoven invites Mozart to his party"
```
#### Plural inflections
It uses the plural forms `zero`, `one`, `two`, `few`, `many`, or `other` (required). Note English only uses `one` and `other`. So, including `zero` will never get called, even when the number is 0. For more information see [`.pluralGenerator()`](../plural/plural-generator.md).
```javascript
var numberFormatter, taskFormatter,
en = new Globalize( "en" );
// Note you can define multiple lines message using an Array of Strings.
Globalize.loadMessages({
en: {
task: [
"You have {count, plural,",
" one {one task}",
" other {{formattedCount} tasks}",
"} remaining"
]
}
});
numberFormatter = en.numberFormatter();
taskFormatter = en.messageFormatter( "task" );
taskFormatter({
count: 1000,
formattedCount: numberFormatter( 1000 )
});
// > "You have 1,000 tasks remaining"
```
Literal numeric keys can be used in `plural` to match single, specific numbers.
```javascript
var taskFormatter,
en = new Globalize( "en" );
// Note you can define multiple lines message using an Array of Strings.
Globalize.loadMessages({
en: {
task: [
"You have {count, plural,",
" =0 {no tasks}",
" one {one task}",
" other {{formattedCount} tasks}",
"} remaining"
]
}
});
taskFormatter = Globalize( "en" ).messageFormatter( "task" );
taskFormatter({
count: 0,
formattedCount: en.numberFormatter( 0 )
});
// > "You have no tasks remaining"
```
You may find useful having the plural forms calculated with an offset applied.
Use `#` to output the resulting number. Note literal numeric keys do NOT use the
offset value.
```javascript
var likeFormatter,
en = new Globalize( "en" );
Globalize.loadMessages({
en: {
likeIncludingMe: [
"{0, plural, offset:1",
" =0 {Be the first to like this}",
" =1 {You liked this}",
" one {You and someone else liked this}",
" other {You and # others liked this}",
"}"
]
}
});
likeFormatter = Globalize( "en" ).messageFormatter( "likeIncludingMe" );
likeFormatter( 0 );
// > "Be the first to like this"
likeFormatter( 1 );
// > "You liked this"
likeFormatter( 2 );
// > "You and someone else liked this"
likeFormatter( 3 );
// > "You and 2 others liked this"
```
Read on [SlexAxton/messageFormatter.js][] for more information on regard of ICU MessageFormat.
[SlexAxton/messageFormatter.js]: https://github.com/SlexAxton/messageformat.js/#no-frills