diff --git a/core/js/js.js b/core/js/js.js index 37540bd15e..b687c408bc 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -24,6 +24,55 @@ if (oc_debug !== true || typeof console === "undefined" || typeof console.log == } } +function initL10N(app) { + if (!( t.cache[app] )) { + $.ajax(OC.filePath('core', 'ajax', 'translations.php'), { + async: false,//todo a proper solution for this without sync ajax calls + data: {'app': app}, + type: 'POST', + success: function (jsondata) { + t.cache[app] = jsondata.data; + t.plural_form = jsondata.plural_form; + } + }); + + // Bad answer ... + if (!( t.cache[app] )) { + t.cache[app] = []; + } + } + if (typeof t.plural_function == 'undefined') { + t.plural_function = function (n) { + var p = (n != 1) ? 1 : 0; + return { 'nplural' : 2, 'plural' : p }; + }; + + /** + * code below has been taken from jsgettext - which is LGPL licensed + * https://developer.berlios.de/projects/jsgettext/ + * http://cvs.berlios.de/cgi-bin/viewcvs.cgi/jsgettext/jsgettext/lib/Gettext.js + */ + var pf_re = new RegExp('^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_\(\)])+)', 'm'); + if (pf_re.test(t.plural_form)) { + //ex english: "Plural-Forms: nplurals=2; plural=(n != 1);\n" + //pf = "nplurals=2; plural=(n != 1);"; + //ex russian: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10< =4 && (n%100<10 or n%100>=20) ? 1 : 2) + //pf = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"; + var pf = t.plural_form; + if (! /;\s*$/.test(pf)) pf = pf.concat(';'); + /* We used to use eval, but it seems IE has issues with it. + * We now use "new Function", though it carries a slightly + * bigger performance hit. + var code = 'function (n) { var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) }; };'; + Gettext._locale_data[domain].head.plural_func = eval("("+code+")"); + */ + var code = 'var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };'; + t.plural_function = new Function("n", code); + } else { + console.log("Syntax error in language file. Plural-Forms header is invalid ["+plural_forms+"]"); + } + } +} /** * translate a string * @param app the id of the app for which to translate the string @@ -33,21 +82,7 @@ if (oc_debug !== true || typeof console === "undefined" || typeof console.log == * @return string */ function t(app, text, vars, count){ - if( !( t.cache[app] )) { - $.ajax(OC.filePath('core','ajax','translations.php'),{ - async:false,//todo a proper sollution for this without sync ajax calls - data:{'app': app}, - type:'POST', - success:function(jsondata){ - t.cache[app] = jsondata.data; - } - }); - - // Bad answer ... - if( !( t.cache[app] )){ - t.cache[app] = []; - } - } + initL10N(app); var _build = function (text, vars, count) { return text.replace(/%n/g, count).replace(/{([^{}]*)}/g, function (a, b) { @@ -67,7 +102,7 @@ function t(app, text, vars, count){ return translation; } } -t.cache={}; +t.cache = {}; /** * translate a string @@ -78,7 +113,17 @@ t.cache={}; * @param vars (optional) FIXME * @return string */ -function n(app, text_singular, text_plural, count, vars){ +function n(app, text_singular, text_plural, count, vars) { + initL10N(app); + var identifier = '_' + text_singular + '__' + text_plural + '_'; + if( typeof( t.cache[app][identifier] ) !== 'undefined' ){ + var translation = t.cache[app][identifier]; + if ($.isArray(translation)) { + var plural = t.plural_function(count); + return t(app, translation[plural.plural], vars, count); + } + } + if(count === 1) { return t(app, text_singular, vars, count); }