Fix localization of unknown strings on client

This commit is contained in:
Dmitriy Simushev 2014-10-14 12:36:23 +00:00
parent 7f071cbe80
commit 2703f72de8
2 changed files with 18 additions and 5 deletions

View File

@ -37,15 +37,14 @@
* @returns {String} Localized string.
*/
Mibew.Localization.trans = function(str) {
if (! localStrings.hasOwnProperty(str)) {
return false;
}
// Replace "{n}" style placeholders with specified arguments. The first
// argument is skipped because it is the localized string.
var placeholders = Array.prototype.slice.call(arguments, 1);
return localStrings[str].replace(/\{([0-9]+)\}/g, function(match, index) {
// If there is no localized string use passed in one.
var localized = localStrings.hasOwnProperty(str) ? localStrings[str] : str;
return localized.replace(/\{([0-9]+)\}/g, function(match, index) {
return placeholders[parseInt(index)] || '';
});
}

View File

@ -20,3 +20,17 @@ test('Basic things', function() {
'Test placeholders'
);
});
test('Unknown string', function() {
equal(
Mibew.Localization.trans('An unknown string'),
'An unknown string',
'Test simple string'
);
equal(
Mibew.Localization.trans('An unknown string with a placeholder: {0}', 'test!'),
'An unknown string with a placeholder: test!',
'Test placeholders'
);
})