mirror of
https://github.com/Mibew/mibew.git
synced 2025-01-31 13:24:41 +03:00
Move translation logic from js helper to localization object
This commit is contained in:
parent
cb4bd3acaa
commit
2bb6899c33
@ -61,17 +61,13 @@
|
||||
* </code>
|
||||
* where:
|
||||
* - "localization.string" is localization constant.
|
||||
* - arg* are arguments that will be passed to getlocal function. There
|
||||
* can be arbitrary number of such arguments.
|
||||
* - arg* are arguments that will replace the placeholders.
|
||||
*/
|
||||
Handlebars.registerHelper('l10n', function() {
|
||||
var key = arguments[0],
|
||||
placeholders = Array.prototype.slice.call(arguments, 1),
|
||||
localized = (Mibew.Localization.trans(key) || '');
|
||||
var l = Mibew.Localization,
|
||||
slice = Array.prototype.slice;
|
||||
|
||||
return localized.replace(/\{([0-9]+)\}/g, function(match, index) {
|
||||
return placeholders[parseInt(index)] || '';
|
||||
});
|
||||
return l.trans.apply(l, slice.call(arguments));
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -30,15 +30,24 @@
|
||||
var localStrings = {};
|
||||
|
||||
/**
|
||||
* Localize string
|
||||
* @param {String} str String for localization
|
||||
* @returns {String} Localized string
|
||||
* Localize string.
|
||||
*
|
||||
* @param {String} str String for localization.
|
||||
* @param {...String} placeholder A value that will replace a placeholder.
|
||||
* @returns {String} Localized string.
|
||||
*/
|
||||
Mibew.Localization.trans = function(str) {
|
||||
if (! localStrings.hasOwnProperty(str)) {
|
||||
return false;
|
||||
}
|
||||
return localStrings[str];
|
||||
|
||||
// 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) {
|
||||
return placeholders[parseInt(index)] || '';
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,20 +10,39 @@
|
||||
<div id="qunit"></div>
|
||||
<div id="qunit-fixture"></div>
|
||||
|
||||
<!-- Add external libraries -->
|
||||
<script type="text/javascript" src="/mibew/js/libs/json2.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/libs/handlebars.min.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/libs/underscore-min.js"></script>
|
||||
|
||||
<!-- Setup fixture -->
|
||||
<script type="text/javascript">
|
||||
Mibew = {};
|
||||
</script>
|
||||
|
||||
<!-- Start of the mibew_api.js tests -->
|
||||
<script type="text/javascript" src="/mibew/js/libs/json2.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/compiled/mibewapi.js"></script>
|
||||
<script type="text/javascript" src="test_cases/mibewapi_tests.js"></script>
|
||||
<!-- End of the mibew_api.js tests -->
|
||||
|
||||
<!-- Setup fixture -->
|
||||
<script type="text/javascript">
|
||||
// Clean up environment
|
||||
Mibew = {};
|
||||
</script>
|
||||
|
||||
<!-- Start of the localization.js tests -->
|
||||
<script type="text/javascript" src="/mibew/js/compiled/default/localization.js"></script>
|
||||
<script type="text/javascript" src="test_cases/localization_tests.js"></script>
|
||||
<!-- End of the localization.js tests -->
|
||||
|
||||
<!-- Setup fixture -->
|
||||
<script type="text/javascript">
|
||||
// Clean up environment
|
||||
Mibew = {};
|
||||
</script>
|
||||
|
||||
<!-- Start of Handlebars' helpers tests -->
|
||||
<script type="text/javascript" src="/mibew/js/libs/handlebars.min.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/libs/underscore-min.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/compiled/default/localization.js"></script>
|
||||
<script type="text/javascript" src="/mibew/js/compiled/default/handlebars_helpers.js"></script>
|
||||
<script type="text/javascript" src="test_cases/handlebars_helpers_tests.js"></script>
|
||||
|
22
src/tests/client_side/qunit/test_cases/localization_tests.js
Normal file
22
src/tests/client_side/qunit/test_cases/localization_tests.js
Normal file
@ -0,0 +1,22 @@
|
||||
// Testing Localization system
|
||||
module('Localization');
|
||||
|
||||
test('Basic things', function() {
|
||||
// Fill localization container
|
||||
Mibew.Localization.set({
|
||||
one: 'uno',
|
||||
'Hello {0}, {1} and {2}!': '¡Hola {0}, {1} y {2}!'
|
||||
});
|
||||
|
||||
equal(
|
||||
Mibew.Localization.trans('one'),
|
||||
'uno',
|
||||
'Test simple string'
|
||||
);
|
||||
|
||||
equal(
|
||||
Mibew.Localization.trans('Hello {0}, {1} and {2}!', 'Foo', 'Bar', 'Baz'),
|
||||
'¡Hola Foo, Bar y Baz!',
|
||||
'Test placeholders'
|
||||
);
|
||||
});
|
Loading…
Reference in New Issue
Block a user