mirror of
https://github.com/Mibew/mibew.git
synced 2024-11-15 16:44:11 +03:00
Add placeholders to HBS.js "l10n" helper
This commit is contained in:
parent
957e67686a
commit
fa615bc2e0
@ -55,10 +55,23 @@
|
||||
/**
|
||||
* Register 'l10n' Handlebars helper
|
||||
*
|
||||
* This helper returns translated string with specified key
|
||||
* This helper returns translated string with specified key. Example of usage:
|
||||
* <code>
|
||||
* {{l10n "localization.string" arg1 arg2 arg3}}
|
||||
* </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.
|
||||
*/
|
||||
Handlebars.registerHelper('l10n', function(key) {
|
||||
return (Mibew.Localization.trans(key) || '');
|
||||
Handlebars.registerHelper('l10n', function() {
|
||||
var key = arguments[0],
|
||||
placeholders = Array.prototype.slice.call(arguments, 1),
|
||||
localized = (Mibew.Localization.trans(key) || '');
|
||||
|
||||
return localized.replace(/\{([0-9]+)\}/g, function(match, index) {
|
||||
return placeholders[parseInt(index)] || '';
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -22,9 +22,11 @@
|
||||
<!-- End of the mibew_api.js tests -->
|
||||
|
||||
<!-- Start of Handlebars' helpers tests -->
|
||||
<script type="text/javascript" src="/mibew/js/libs/handlebars.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>
|
||||
<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>
|
||||
<!-- Start of Handlebars' helpers tests -->
|
||||
<!-- End of Handlebars' helpers tests -->
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
@ -196,3 +196,24 @@ test('cutString', function() {
|
||||
'Test cutting of a string that is longer than specified length'
|
||||
);
|
||||
});
|
||||
|
||||
// Test "l10n" Handlebars helper
|
||||
test('l10n', function() {
|
||||
// Add some localization strings that are needed helper testing
|
||||
Mibew.Localization.set({
|
||||
'one': 'uno',
|
||||
'Hello {0}!': '¡Hola {0}!'
|
||||
});
|
||||
|
||||
equal(
|
||||
Handlebars.compile('{{l10n "one"}}')({}),
|
||||
'uno',
|
||||
'Test simple string'
|
||||
);
|
||||
|
||||
equal(
|
||||
Handlebars.compile('{{l10n "Hello {0}!" "world"}}')({}),
|
||||
'¡Hola world!',
|
||||
'Test string with placeholder'
|
||||
);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user