Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rmariuzzo committed Feb 6, 2019
2 parents dc50919 + 3f30a89 commit 6c90d1f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/lang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions src/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
*/
Lang.prototype._getMessage = function(key, locale) {
locale = locale || this.getLocale();

key = this._parseKey(key, locale);

// Ensure message source exists.
Expand All @@ -282,14 +283,9 @@
// Get message from default locale.
var message = this.messages[key.source];
var entries = key.entries.slice();
var subKey = '';
while (entries.length && message !== undefined) {
var subKey = !subKey ? entries.shift() : subKey.concat('.', entries.shift());
if (message[subKey] !== undefined) {
message = message[subKey]
subKey = '';
}
}
var subKey = entries.join('.');
message = message !== undefined ? this._getValueInKey(message, subKey) : undefined;


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

Lang.prototype._getValueInKey = function(obj, str) {
// If the full key exists just return the value
if (typeof obj[str] === 'string') {
return obj[str]
}

str = str.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
str = str.replace(/^\./, ''); // strip a leading dot

var parts = str.split('.');

for (var i = 0, n = parts.length; i < n; ++i) {
var currentKey = parts.slice(0, i + 1).join('.');
var restOfTheKey = parts.slice(i + 1, parts.length).join('.')

if (obj[currentKey]) {
return this._getValueInKey(obj[currentKey], restOfTheKey)
}
}

return obj;
};

/**
* Return the locale to be used between default and fallback.
* @param {String} key
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
},
"plural": "one apple|a million apples",
"dot.in.key": "Dot In Key",
"with_parent": "Key That Is Subpart Of A Parent Key",
"with_parent.dot.in.key": "Dot In Key With a Parent Key",
"dot.in.key2.nested": {
"dot.in.key2.nested": "Dot In Key Nested Tricky"
},
Expand Down
4 changes: 4 additions & 0 deletions test/spec/lang_get_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe('The lang.get() method', function () {
expect(lang.get('messages.dot.in.key')).toBe('Dot In Key');
});

it('should prioritize the dot in key', function() {
expect(lang.get('messages.with_parent.dot.in.key')).toBe('Dot In Key With a Parent Key');
});

it('should return the expected message if the key is nested and has a dot', function() {
expect(lang.get('messages.dotInKey.dot.in.key')).toBe('Dot In Key Nested Simple');
expect(lang.get('messages.dot.in.key2.nested.dot.in.key2.nested')).toBe('Dot In Key Nested Tricky');
Expand Down

0 comments on commit 6c90d1f

Please sign in to comment.