|
35 | 35 | }
|
36 | 36 |
|
37 | 37 |
|
38 |
| - var CONTEXT_KEY = '--intl--'; |
| 38 | + var CONTEXT_KEY = 'intl'; |
39 | 39 |
|
40 | 40 |
|
41 | 41 | /**
|
|
81 | 81 | }
|
82 | 82 |
|
83 | 83 |
|
| 84 | + /** |
| 85 | + Returns something from deep within the a value in the context, taking into |
| 86 | + consideration the context stack. (The built-in version of context.get() |
| 87 | + isn't quite sophisticated enough for us.) |
| 88 | + @protected |
| 89 | + @method _contextGet |
| 90 | + @param {Object} ctx The dust context. |
| 91 | + @param {Array} keys An ordered list of keys to drill down into the data structure. |
| 92 | + @return {mixed} Value found for the key path, or undefined if not found. |
| 93 | + */ |
| 94 | + function _contextGet(ctx, keys) { |
| 95 | + var frame, // the current stack frame |
| 96 | + data, // the spot within the stack frame we're inspecting |
| 97 | + last = keys.length - 1, |
| 98 | + k, |
| 99 | + key; |
| 100 | + for (frame = ctx.stack; frame; frame = frame.tail) { |
| 101 | + data = frame.head; |
| 102 | + for (k = 0; k < last; k += 1) { |
| 103 | + key = keys[k]; |
| 104 | + if (! data.hasOwnProperty(key)) { |
| 105 | + break; |
| 106 | + } |
| 107 | + data = data[key]; |
| 108 | + } |
| 109 | + if (k === last && data.hasOwnProperty(keys[last])) { |
| 110 | + return data[keys[last]]; |
| 111 | + } |
| 112 | + } |
| 113 | + return undefined; // value not found anywhere |
| 114 | + } |
| 115 | + |
| 116 | + |
84 | 117 | /**
|
85 | 118 | Determins the current locales, possibly looking in parent contexts
|
86 | 119 | if they've been defined there. Defaults to the global `this`.
|
|
94 | 127 | if (params.locales) {
|
95 | 128 | return _tap(params.locales, chunk, context);
|
96 | 129 | }
|
97 |
| - return context.get([CONTEXT_KEY, 'locales']) || this.locale; |
| 130 | + return _contextGet(context, [CONTEXT_KEY, 'locales']) || this.locale; |
98 | 131 | }
|
99 | 132 |
|
100 | 133 |
|
|
107 | 140 | @param {Object} context The dust context stack.
|
108 | 141 | @return {Object} The format options.
|
109 | 142 | */
|
110 |
| - function _getFormatOptions(chunk, params, context) { |
| 143 | + function _getFormatOptions(type, chunk, params, context) { |
111 | 144 | var raw,
|
112 | 145 | k,
|
113 | 146 | fixed = {},
|
114 | 147 | fmt;
|
115 |
| - if (params._fmt) { |
116 |
| - fmt = _tap(params._fmt, chunk, context); |
117 |
| - delete params._fmt; |
118 |
| - raw = context.get([CONTEXT_KEY, 'formats', fmt]); |
| 148 | + if (params.formatName) { |
| 149 | + fmt = _tap(params.formatName, chunk, context); |
| 150 | + delete params.formatName; |
| 151 | + raw = _contextGet(context, [CONTEXT_KEY, 'formats', type, fmt]); |
119 | 152 | // TODO: only need to copy-and-merge if there are still parameters
|
120 | 153 | raw = _extend({}, raw); // shallow copy
|
121 | 154 | _extend(raw, params);
|
|
152 | 185 | msg = params._msg;
|
153 | 186 | }
|
154 | 187 | else if (params._key) {
|
155 |
| - msg = context.get([CONTEXT_KEY, 'messages', _tap(params._key, chunk, context)]); |
| 188 | + msg = _contextGet(context, [CONTEXT_KEY, 'messages', _tap(params._key, chunk, context)]); |
156 | 189 | }
|
157 | 190 | else {
|
158 | 191 | throw new ReferenceError('@intlMessage needs either a `_msg` or `_key` parameter');
|
|
164 | 197 | return chunk;
|
165 | 198 | }
|
166 | 199 |
|
167 |
| - formatOptions = context.get([CONTEXT_KEY, 'formats']); |
| 200 | + formatOptions = _contextGet(context, [CONTEXT_KEY, 'formats']); |
168 | 201 | locales = _getLocales(chunk, params, context);
|
169 | 202 | formatter = new IntlMessageFormat(msg, locales, formatOptions);
|
170 | 203 | chunk.write(formatter.format(params));
|
|
194 | 227 | val = _tap(params.val, chunk, context);
|
195 | 228 | delete params.val; // since params might be interpretted as format options
|
196 | 229 |
|
197 |
| - formatOptions = _getFormatOptions(chunk, params, context); |
| 230 | + formatOptions = _getFormatOptions('number', chunk, params, context); |
198 | 231 | locales = _getLocales(chunk, params, context);
|
199 | 232 | // TODO: caching based on key [locales, formatOptions]
|
200 | 233 | formatter = new Intl.NumberFormat(locales, formatOptions);
|
|
226 | 259 | delete params.val; // since params might be interpretted as format options
|
227 | 260 | val = new Date(val).getTime();
|
228 | 261 |
|
229 |
| - formatOptions = _getFormatOptions(chunk, params, context); |
| 262 | + formatOptions = _getFormatOptions('date', chunk, params, context); |
230 | 263 | locales = _getLocales(chunk, params, context);
|
231 | 264 | // TODO: caching based on key [locales, formatOptions]
|
232 | 265 | formatter = new Intl.DateTimeFormat(locales, formatOptions);
|
|
0 commit comments