Skip to content

Commit 3f30a89

Browse files
alfonsobriesrmariuzzo
authored andcommitted
Prioritize dot notations if has a parent key (#74)
1 parent 70ece4e commit 3f30a89

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

dist/lang.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lang.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
*/
273273
Lang.prototype._getMessage = function(key, locale) {
274274
locale = locale || this.getLocale();
275+
275276
key = this._parseKey(key, locale);
276277

277278
// Ensure message source exists.
@@ -282,14 +283,9 @@
282283
// Get message from default locale.
283284
var message = this.messages[key.source];
284285
var entries = key.entries.slice();
285-
var subKey = '';
286-
while (entries.length && message !== undefined) {
287-
var subKey = !subKey ? entries.shift() : subKey.concat('.', entries.shift());
288-
if (message[subKey] !== undefined) {
289-
message = message[subKey]
290-
subKey = '';
291-
}
292-
}
286+
var subKey = entries.join('.');
287+
message = message !== undefined ? this._getValueInKey(message, subKey) : undefined;
288+
293289

294290
// Get message from fallback locale.
295291
if (typeof message !== 'string' && this.messages[key.sourceFallback]) {
@@ -312,6 +308,29 @@
312308
return message;
313309
};
314310

311+
Lang.prototype._getValueInKey = function(obj, str) {
312+
// If the full key exists just return the value
313+
if (typeof obj[str] === 'string') {
314+
return obj[str]
315+
}
316+
317+
str = str.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
318+
str = str.replace(/^\./, ''); // strip a leading dot
319+
320+
var parts = str.split('.');
321+
322+
for (var i = 0, n = parts.length; i < n; ++i) {
323+
var currentKey = parts.slice(0, i + 1).join('.');
324+
var restOfTheKey = parts.slice(i + 1, parts.length).join('.')
325+
326+
if (obj[currentKey]) {
327+
return this._getValueInKey(obj[currentKey], restOfTheKey)
328+
}
329+
}
330+
331+
return obj;
332+
};
333+
315334
/**
316335
* Return the locale to be used between default and fallback.
317336
* @param {String} key

test/fixture/messages.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
},
110110
"plural": "one apple|a million apples",
111111
"dot.in.key": "Dot In Key",
112+
"with_parent": "Key That Is Subpart Of A Parent Key",
113+
"with_parent.dot.in.key": "Dot In Key With a Parent Key",
112114
"dot.in.key2.nested": {
113115
"dot.in.key2.nested": "Dot In Key Nested Tricky"
114116
},

test/spec/lang_get_spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ describe('The lang.get() method', function () {
6969
expect(lang.get('messages.dot.in.key')).toBe('Dot In Key');
7070
});
7171

72+
it('should prioritize the dot in key', function() {
73+
expect(lang.get('messages.with_parent.dot.in.key')).toBe('Dot In Key With a Parent Key');
74+
});
75+
7276
it('should return the expected message if the key is nested and has a dot', function() {
7377
expect(lang.get('messages.dotInKey.dot.in.key')).toBe('Dot In Key Nested Simple');
7478
expect(lang.get('messages.dot.in.key2.nested.dot.in.key2.nested')).toBe('Dot In Key Nested Tricky');

0 commit comments

Comments
 (0)