From b26fb08732c4459ef01cd705f601173d4b79afd0 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Tue, 8 Sep 2015 20:48:18 +0200 Subject: [PATCH] Fixing issues with en translation, thanks @wware for spotting it --- js/zerobin.js | 64 +++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/js/zerobin.js b/js/zerobin.js index 82a6dce3..479afb59 100644 --- a/js/zerobin.js +++ b/js/zerobin.js @@ -233,7 +233,8 @@ $(function() { } else { val = args[i]; // A switch statement so that the formatter can be extended. - switch (m) { + switch (m) + { case '%d': val = parseFloat(val); if (isNaN(val)) { @@ -258,25 +259,6 @@ $(function() { */ supportedLanguages: ['de', 'fr', 'pl'], - /** - * per language functions to use to determine the plural form - * From: http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html - * - * @param int number - * @return int array key - */ - pluralRules: { - de: function(n) { - return (n != 1 ? 1 : 0); - }, - fr: function(n) { - return (n > 1 ? 1 : 0); - }, - pl: function(n) { - return (n == 1 ? 0 : n%10 >= 2 && n %10 <=4 && (n%100 < 10 || n%100 >= 20) ? 1 : 2); - } - }, - /** * translate a string, alias for translate() * @@ -312,13 +294,15 @@ $(function() { if (messageId.length == 0) return messageId; if (!this.translations.hasOwnProperty(messageId)) { - console.log('Missing translation for: ' + messageId); + if (this.language != 'en') console.debug( + 'Missing translation for: ' + messageId + ); this.translations[messageId] = args[0]; } if (usesPlurals && $.isArray(this.translations[messageId])) { var n = parseInt(args[1] || 1), - key = this.pluralRules[this.language](n), + key = this.getPluralForm(n), maxKey = this.translations[messageId].length - 1; if (key > maxKey) key = maxKey; args[0] = this.translations[messageId][key]; @@ -331,6 +315,26 @@ $(function() { return helper.sprintf(args); }, + /** + * per language functions to use to determine the plural form + * From: http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html + * + * @param int number + * @return int array key + */ + getPluralForm: function(n) { + switch (this.language) + { + case 'fr': + return (n > 1 ? 1 : 0); + case 'pl': + return (n == 1 ? 0 : n%10 >= 2 && n %10 <=4 && (n%100 < 10 || n%100 >= 20) ? 1 : 2); + // en, de + default: + return (n != 1 ? 1 : 0); + } + }, + /** * load translations into cache, then execute callback function * @@ -340,12 +344,18 @@ $(function() { { var language = (navigator.language || navigator.userLanguage).substring(0, 2); // note that 'en' is built in, so no translation is necessary - if (this.supportedLanguages.indexOf(language) == -1) return; - $.getJSON('i18n/' + language + '.json', function(data) { - i18n.language = language; - i18n.translations = data; + if (this.supportedLanguages.indexOf(language) == -1) + { callback(); - }); + } + else + { + $.getJSON('i18n/' + language + '.json', function(data) { + i18n.language = language; + i18n.translations = data; + callback(); + }); + } }, /**