@@ -31,12 +31,17 @@ Messages are defined in a datastructure which maps _message keys_ to _message
31
31
content_ . In its simplest form, a messages structure looks like the following:
32
32
33
33
``` javascript
34
- {
35
- greeting: " Hello, world"
36
- }
34
+ const messages = new Map ()
35
+ .put (" greeting" , " Hello, world" )
36
+ .put (" bye" , " See you soon" )
37
+
37
38
```
38
39
39
- This defines a single message identified by the key ` greeting ` and the content ` "Hello, world" ` .
40
+ This defines a single message identified by the key ` greeting ` and the content ` Hello, world `
41
+ as well as a message with key ` bye ` and the content ` See you soon ` . Note that in this
42
+ example all keys and messages are ` string ` s. While this is true for all message keys,
43
+ messages may also be a nested ` Map ` when used with _ plurals_ (see below).
44
+
40
45
41
46
## Loading Messages
42
47
@@ -82,9 +87,8 @@ first argument.
82
87
If you have a messages definition like
83
88
84
89
``` javascript
85
- {
86
- " greeting" : " Welcome, {{0}}!" ,
87
- }
90
+ const messages = new Map ()
91
+ .put (" greeting" , " Welcome, {{0}}!" )
88
92
```
89
93
90
94
and you perform a call like
@@ -108,12 +112,12 @@ You can define a message's content to be an object with keys describing the _amo
108
112
Here is the message definition from the example above:
109
113
110
114
``` javascript
111
- {
112
- " inbox.summary" : {
113
- 0 : " No new message" ,
114
- 1 : " One new message" ,
115
- n : " {{n}} new messages" ,
116
- },
115
+ const messages = new Map ()
116
+ . put ( " inbox.summary" , new Map ()
117
+ . put ( 0 , " No new message" )
118
+ . put ( 1 , " One new message" )
119
+ . put (n, " {{n}} new messages" )
120
+ )
117
121
}
118
122
```
119
123
@@ -127,14 +131,12 @@ console.log(messageResolver.mpl("inbox.summary", numberOfNewMessages))
127
131
You can use placeholders in plural messages and provide additional arguments, such as:
128
132
129
133
``` javascript
130
- {
131
- " inbox.folder.summary" : {
132
- 0 : " Your {{0}} folder contains no messages." ,
133
- 1 : " Your {{0}} folder contains one message." ,
134
- n: " Your {{0}} folder contains {{n}} messages." ,
135
- }
136
- }
137
-
134
+ const messages = new Map ()
135
+ .put (" inbox.folder.summary" , new Map ()
136
+ .put (0 , " Your {{0}} folder contains no messages." )
137
+ .put (1 , " Your {{0}} folder contains one message." )
138
+ .put (n, " Your {{0}} folder contains {{n}} messages." )
139
+ )
138
140
// ...
139
141
140
142
console .log (messageResolver .mpl (" inbox.folder.summary" , numberOfMessages, " trash" ))
@@ -150,11 +152,25 @@ Make sure you also check out the source code and doc comments for the classes.
150
152
A ` MessageLoader ` that "loads" messages from a given object. This message loader is typically be
151
153
used in an environment where messages are ` import ` ed from static files during Javascript assembly.
152
154
155
+ The object loader's input uses plain Javascript ` object ` s like the following:
156
+
157
+ ``` javascript
158
+ {
159
+ " message.key" : " message content" ,
160
+ " plural.message.key" : {
161
+ " 0" : " empty message" ,
162
+ " n" : " {{n}} message" ,
163
+ },
164
+ }
165
+ ```
166
+
167
+ See the following example for how to use the ` ObjectMessageLoader ` :
168
+
153
169
``` javascript
154
170
import { MessageResolver , ObjectMessageLoader } from " @weccoframework/i18n"
155
- import { en , de , fr } from " ./messages.json "
171
+ import { en , de , fr } from " ./messages"
156
172
157
- const messageResolver = MessageResolver .create (new ObjectMessageLoader (en, {
173
+ const messageResolver = await MessageResolver .create (new ObjectMessageLoader (en, {
158
174
en: en,
159
175
de: de,
160
176
fr: fr,
@@ -164,9 +180,74 @@ const messageResolver = MessageResolver.create(new ObjectMessageLoader(en, {
164
180
This pattern is very easy to get running and works very for few translations with a small number
165
181
of messages.
166
182
183
+ ### JsonMessageLoader
184
+
185
+ The ` JsonMessageLoader ` supports loading messages from ` JsonSource ` . A ` JsonSource ` is a function
186
+ which accepts a language as a single argument and retuns a ` Promise ` resolving to a JSON-formatted
187
+ ` string ` which defines the messages. The format of the JSON messages is almost identical to the one used
188
+ by the ` ObjectMessageLoader ` :
189
+
190
+ ``` json
191
+ {
192
+ "message.key" : " message content" ,
193
+ "plural.message.key" : {
194
+ "0" : " empty message" ,
195
+ "n" : " {{n}} message" ,
196
+ },
197
+ }
198
+ ```
199
+
200
+ To construct a ` JsonMessagesLoader ` you need to pass at least one ` JsonSource ` . You may pass
201
+ two. In this case the first one is used to load the default messages while the second one is
202
+ used to load the localized messages.
203
+
204
+ ``` javascript
205
+ const jsonSource = (language ) => {
206
+ // ...
207
+ // return a Promise resolving to a string.
208
+ // When loading the default messages, language is undefined
209
+ }
210
+
211
+ const messageResolver = await MessageResolver .create (new JsonMessageLoader (jsonSource))
212
+ ```
213
+
214
+ #### fetchJsonSource
215
+
216
+ ` i18n ` comes with with a ready to use implementation of ` JsonSource `
217
+ which uses ` fetch ` to load messages identified by language. The implementation uses
218
+ the following conventions to load messages:
219
+
220
+ * all messages are located under a given _ base URL_
221
+ * default messages can be loaded from ` <baseURL>/default.json `
222
+ * localized messages for ` <lang> ` can be loaded from ` <baseURL>/<lang>.json `
223
+
224
+ You can pass additional request init options, such as ` CORS ` mode, change the
225
+ request method (which defaults to ` GET ` ), set headers, ...
226
+
167
227
### CascadingMessageLoader
228
+ The ` CascadingMessageLoader ` loads messages by using a set of underlying ` MessageLoader ` s
229
+ and merging the messages returned by each loader together in a way similiar to how CSS
230
+ rules are merged (thus the name). Use this loader to get an aggregated set of messages
231
+ from multiple sources.
232
+
233
+ The following (rather academic) example demonstrates the merging.
234
+
235
+ ``` javascript
236
+
237
+ const loader1 = new ObjectMessageLoader ({
238
+ foo: " foo" ,
239
+ bar: " bar" ,
240
+ })
241
+
242
+ const loader2 = new ObjectMessageLoader ({
243
+ bar: " Bar" ,
244
+ spam: " Eggs" ,
245
+ })
168
246
169
- ** TODO**
247
+ const messageResolver = await MessageResolver .create (new CascadingMessageLoader (loader1, loader2))
248
+
249
+ console .log (messageResolver .m (" bar" )) // Logs: Bar
250
+ ```
170
251
171
252
# Author
172
253
0 commit comments