@@ -71,9 +71,9 @@ export default class Localization {
71
71
/**
72
72
* Format translations into {value, attributes} objects.
73
73
*
74
- * The fallback logic is the same as in `formatValues` but the argument type
75
- * is stricter (an array of arrays) and it returns {value, attributes}
76
- * objects which are suitable for the translation of DOM elements.
74
+ * The fallback logic is the same as in `formatValues` but it returns {value,
75
+ * attributes} objects which are suitable for the translation of DOM
76
+ * elements.
77
77
*
78
78
* docL10n.formatMessages([
79
79
* {id: 'hello', args: { who: 'Mary' }},
@@ -101,8 +101,8 @@ export default class Localization {
101
101
/**
102
102
* Retrieve translations corresponding to the passed keys.
103
103
*
104
- * A generalized version of `DOMLocalization.formatValue`. Keys can
105
- * either be simple string identifiers or `[ id, args]` arrays .
104
+ * A generalized version of `DOMLocalization.formatValue`. Keys must
105
+ * be `{ id, args}` objects .
106
106
*
107
107
* docL10n.formatValues([
108
108
* {id: 'hello', args: { who: 'Mary' }},
@@ -164,26 +164,26 @@ export default class Localization {
164
164
}
165
165
166
166
/**
167
- * Format the value of a message into a string.
167
+ * Format the value of a message into a string or `null` .
168
168
*
169
169
* This function is passed as a method to `keysFromBundle` and resolve
170
170
* a value of a single L10n Entity using provided `FluentBundle`.
171
171
*
172
- * If the function fails to retrieve the entity, it will return an ID of it.
173
- * If formatting fails, it will return a partially resolved entity.
174
- *
175
- * In both cases, an error is being added to the errors array.
172
+ * If the message doesn't have a value, return `null`.
176
173
*
177
174
* @param {FluentBundle } bundle
178
- * @param {Array<Error> } errors
179
- * @param {string } id
180
- * @param {Object } args
181
- * @returns {string }
175
+ * @param {Array<Error> } errors
176
+ * @param {Object } message
177
+ * @param {Object } args
178
+ * @returns {string|null }
182
179
* @private
183
180
*/
184
- function valueFromBundle ( bundle , errors , id , args ) {
185
- const msg = bundle . getMessage ( id ) ;
186
- return bundle . format ( msg , args , errors ) ;
181
+ function valueFromBundle ( bundle , errors , message , args ) {
182
+ if ( message . value ) {
183
+ return bundle . formatPattern ( message . value , args , errors ) ;
184
+ }
185
+
186
+ return null ;
187
187
}
188
188
189
189
/**
@@ -195,34 +195,29 @@ function valueFromBundle(bundle, errors, id, args) {
195
195
* The function will return an object with a value and attributes of the
196
196
* entity.
197
197
*
198
- * If the function fails to retrieve the entity, the value is set to the ID of
199
- * an entity, and attributes to `null`. If formatting fails, it will return
200
- * a partially resolved value and attributes.
201
- *
202
- * In both cases, an error is being added to the errors array.
203
- *
204
198
* @param {FluentBundle } bundle
205
- * @param {Array<Error> } errors
206
- * @param {String } id
207
- * @param {Object } args
199
+ * @param {Array<Error> } errors
200
+ * @param {Object } message
201
+ * @param {Object } args
208
202
* @returns {Object }
209
203
* @private
210
204
*/
211
- function messageFromBundle ( bundle , errors , id , args ) {
212
- const msg = bundle . getMessage ( id ) ;
213
-
205
+ function messageFromBundle ( bundle , errors , message , args ) {
214
206
const formatted = {
215
- value : bundle . format ( msg , args , errors ) ,
207
+ value : null ,
216
208
attributes : null ,
217
209
} ;
218
210
219
- if ( msg . attrs ) {
220
- formatted . attributes = [ ] ;
221
- for ( const [ name , attr ] of Object . entries ( msg . attrs ) ) {
222
- const value = bundle . format ( attr , args , errors ) ;
223
- if ( value !== null ) {
224
- formatted . attributes . push ( { name, value} ) ;
225
- }
211
+ if ( message . value ) {
212
+ formatted . value = bundle . formatPattern ( message . value , args , errors ) ;
213
+ }
214
+
215
+ let attrNames = Object . keys ( message . attributes ) ;
216
+ if ( attrNames . length > 0 ) {
217
+ formatted . attributes = new Array ( attrNames . length ) ;
218
+ for ( let [ i , name ] of attrNames . entries ( ) ) {
219
+ let value = bundle . formatPattern ( message . attributes [ name ] , args , errors ) ;
220
+ formatted . attributes [ i ] = { name, value} ;
226
221
}
227
222
}
228
223
@@ -270,9 +265,10 @@ function keysFromBundle(method, bundle, keys, translations) {
270
265
return ;
271
266
}
272
267
273
- if ( bundle . hasMessage ( id ) ) {
268
+ let message = bundle . getMessage ( id ) ;
269
+ if ( message ) {
274
270
messageErrors . length = 0 ;
275
- translations [ i ] = method ( bundle , messageErrors , id , args ) ;
271
+ translations [ i ] = method ( bundle , messageErrors , message , args ) ;
276
272
// XXX: Report resolver errors
277
273
} else {
278
274
missingIds . add ( id ) ;
0 commit comments