Move translation logic from js helper to localization object

This commit is contained in:
Dmitriy Simushev 2014-09-19 13:51:59 +00:00
parent cb4bd3acaa
commit 2bb6899c33
4 changed files with 61 additions and 15 deletions

View File

@ -61,17 +61,13 @@
* </code> * </code>
* where: * where:
* - "localization.string" is localization constant. * - "localization.string" is localization constant.
* - arg* are arguments that will be passed to getlocal function. There * - arg* are arguments that will replace the placeholders.
* can be arbitrary number of such arguments.
*/ */
Handlebars.registerHelper('l10n', function() { Handlebars.registerHelper('l10n', function() {
var key = arguments[0], var l = Mibew.Localization,
placeholders = Array.prototype.slice.call(arguments, 1), slice = Array.prototype.slice;
localized = (Mibew.Localization.trans(key) || '');
return localized.replace(/\{([0-9]+)\}/g, function(match, index) { return l.trans.apply(l, slice.call(arguments));
return placeholders[parseInt(index)] || '';
});
}); });
/** /**

View File

@ -30,15 +30,24 @@
var localStrings = {}; var localStrings = {};
/** /**
* Localize string * Localize string.
* @param {String} str String for localization *
* @returns {String} Localized 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) { Mibew.Localization.trans = function(str) {
if (! localStrings.hasOwnProperty(str)) { if (! localStrings.hasOwnProperty(str)) {
return false; 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)] || '';
});
} }
/** /**

View File

@ -10,20 +10,39 @@
<div id="qunit"></div> <div id="qunit"></div>
<div id="qunit-fixture"></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 --> <!-- Setup fixture -->
<script type="text/javascript"> <script type="text/javascript">
Mibew = {}; Mibew = {};
</script> </script>
<!-- Start of the mibew_api.js tests --> <!-- 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="/mibew/js/compiled/mibewapi.js"></script>
<script type="text/javascript" src="test_cases/mibewapi_tests.js"></script> <script type="text/javascript" src="test_cases/mibewapi_tests.js"></script>
<!-- End of the mibew_api.js tests --> <!-- 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 --> <!-- 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/localization.js"></script>
<script type="text/javascript" src="/mibew/js/compiled/default/handlebars_helpers.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> <script type="text/javascript" src="test_cases/handlebars_helpers_tests.js"></script>

View 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'
);
});