mirror of
https://github.com/Mibew/mibew.git
synced 2025-01-31 21:34:42 +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>
|
* </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)] || '';
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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)] || '';
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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>
|
||||||
|
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