mirror of
https://github.com/Mibew/mibew.git
synced 2025-01-31 13:24:41 +03:00
Fix localization of unknown strings on client
This commit is contained in:
parent
7f071cbe80
commit
2703f72de8
@ -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)] || '';
|
||||
});
|
||||
}
|
||||
|
@ -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'
|
||||
);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user