adding proper evaluation of plural form - uses LGPL licensed code of jsgettext
This commit is contained in:
parent
b229381dda
commit
d97e3c3571
|
@ -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
|
* translate a string
|
||||||
* @param app the id of the app for which to translate the 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
|
* @return string
|
||||||
*/
|
*/
|
||||||
function t(app, text, vars, count){
|
function t(app, text, vars, count){
|
||||||
if( !( t.cache[app] )) {
|
initL10N(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] = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var _build = function (text, vars, count) {
|
var _build = function (text, vars, count) {
|
||||||
return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
|
return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
|
||||||
function (a, b) {
|
function (a, b) {
|
||||||
|
@ -79,6 +114,16 @@ t.cache={};
|
||||||
* @return string
|
* @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) {
|
if(count === 1) {
|
||||||
return t(app, text_singular, vars, count);
|
return t(app, text_singular, vars, count);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue