@@ -71,9 +71,9 @@ export default class Localization {
7171 /**
7272 * Format translations into {value, attributes} objects.
7373 *
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.
7777 *
7878 * docL10n.formatMessages([
7979 * {id: 'hello', args: { who: 'Mary' }},
@@ -101,8 +101,8 @@ export default class Localization {
101101 /**
102102 * Retrieve translations corresponding to the passed keys.
103103 *
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 .
106106 *
107107 * docL10n.formatValues([
108108 * {id: 'hello', args: { who: 'Mary' }},
@@ -164,26 +164,26 @@ export default class Localization {
164164}
165165
166166/**
167- * Format the value of a message into a string.
167+ * Format the value of a message into a string or `null` .
168168 *
169169 * This function is passed as a method to `keysFromBundle` and resolve
170170 * a value of a single L10n Entity using provided `FluentBundle`.
171171 *
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`.
176173 *
177174 * @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 }
182179 * @private
183180 */
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 ;
187187}
188188
189189/**
@@ -195,34 +195,29 @@ function valueFromBundle(bundle, errors, id, args) {
195195 * The function will return an object with a value and attributes of the
196196 * entity.
197197 *
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- *
204198 * @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
208202 * @returns {Object }
209203 * @private
210204 */
211- function messageFromBundle ( bundle , errors , id , args ) {
212- const msg = bundle . getMessage ( id ) ;
213-
205+ function messageFromBundle ( bundle , errors , message , args ) {
214206 const formatted = {
215- value : bundle . format ( msg , args , errors ) ,
207+ value : null ,
216208 attributes : null ,
217209 } ;
218210
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} ;
226221 }
227222 }
228223
@@ -270,9 +265,10 @@ function keysFromBundle(method, bundle, keys, translations) {
270265 return ;
271266 }
272267
273- if ( bundle . hasMessage ( id ) ) {
268+ let message = bundle . getMessage ( id ) ;
269+ if ( message ) {
274270 messageErrors . length = 0 ;
275- translations [ i ] = method ( bundle , messageErrors , id , args ) ;
271+ translations [ i ] = method ( bundle , messageErrors , message , args ) ;
276272 // XXX: Report resolver errors
277273 } else {
278274 missingIds . add ( id ) ;
0 commit comments