first commit

This commit is contained in:
root
2026-03-14 09:49:00 +00:00
commit 708ff116e1
1958 changed files with 1718027 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
{
"name": "gettext.js",
"ignore": [
"tests",
"lib",
"*.md",
"*.json",
"*.yml",
"gulpfile.js"
],
"keywords": [
"i18n",
"internationalization",
"gettext",
"ngettext",
"po"
],
"author": {
"name": "Guillaume Potier",
"email": "guillaume@wisembly.com",
"url": "http://guillaumepotier.com/"
},
"license": "MIT",
"main": "dist/gettext.js",
"homepage": "https://github.com/guillaumepotier/gettext.js",
"version": "2.0.3",
"_release": "2.0.3",
"_resolution": {
"type": "version",
"tag": "2.0.3",
"commit": "5d18cb4e512eba1210ab7cead968ed85483323e4"
},
"_source": "https://github.com/guillaumepotier/gettext.js.git",
"_target": "^2",
"_originalSource": "gettext.js"
}

View File

@@ -0,0 +1,3 @@
.DS_Store
node_modules/*
tests/*.json

63
www/bower_components/gettext.js/bin/po2json vendored Executable file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env node --no-deprecation
/*
po2json wrapper for gettext.js
https://github.com/mikeedwards/po2json
Dump a .po file in a json like this one:
{
"": {
"language": "en",
"plural-forms": "nplurals=2; plural=(n!=1);"
},
"simple key": "It's tranlation",
"another with %1 parameter": "It's %1 tranlsation",
"a key with plural": [
"a plural form",
"another plural form",
"could have up to 6 forms with some languages"
],
"a context\u0004a contextualized key": "translation here"
}
*/
var
fs = require('fs'),
po2json = require('po2json'),
argv = process.argv,
json = {},
pretty = '-p' === argv[4];
if (argv.length < 4)
return console.error("Wrong parameters.\nFormat: po2json.js input.po output.json [OPTIONS]\n-p for pretty");
fs.readFile(argv[2], function (err, buffer) {
var jsonData = po2json.parse(buffer);
for (var key in jsonData) {
// Special headers handling, we do not need everything
if ('' === key) {
json[''] = {
'language': jsonData['']['language'],
'plural-forms': jsonData['']['plural-forms']
};
continue;
}
console.log(key, jsonData[key]);
// Do not dump untranslated keys, they already are in the templates!
if ('' !== jsonData[key][1].length ? jsonData[key][1][0] : jsonData[key][1])
json[key] = 2 === jsonData[key].length ? jsonData[key][1] : jsonData[key].slice(1);
}
fs.writeFile(argv[3], JSON.stringify(json, null, pretty ? 4 : 0), function(err) {
if (err)
console.log(err);
else
console.log('JSON ' + (pretty ? 'pretty' : 'compactly') + ' saved to ' + argv[3]);
});
});

View File

@@ -0,0 +1,25 @@
{
"name": "gettext.js",
"ignore": [
"tests",
"lib",
"*.md",
"*.json",
"*.yml",
"gulpfile.js"
],
"keywords": [
"i18n",
"internationalization",
"gettext",
"ngettext",
"po"
],
"author": {
"name": "Guillaume Potier",
"email": "guillaume@wisembly.com",
"url": "http://guillaumepotier.com/"
},
"license": "MIT",
"main": "dist/gettext.js"
}

View File

@@ -0,0 +1,255 @@
define(function () { 'use strict';
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '2.0.0');
// default values that could be overriden in i18n() construct
var defaults = {
domain: 'messages',
locale: (typeof document !== 'undefined' ? document.documentElement.getAttribute('lang') : false) || 'en',
plural_func: function (n) { return { nplurals: 2, plural: (n!=1) ? 1 : 0 }; },
ctxt_delimiter: String.fromCharCode(4) // \u0004
};
// handy mixins taken from underscode.js
var _ = {
isObject: function (obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
},
isArray: function (obj) {
return toString.call(obj) === '[object Array]';
}
};
var
_plural_funcs = {},
_locale = options.locale || defaults.locale,
_domain = options.domain || defaults.domain,
_dictionary = {},
_plural_forms = {},
_ctxt_delimiter = options.ctxt_delimiter || defaults.ctxt_delimiter;
if (options.messages) {
_dictionary[_domain] = {};
_dictionary[_domain][_locale] = options.messages;
}
if (options.plural_forms) {
_plural_forms[_locale] = options.plural_forms;
}
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
.replace(/%(\d+)/g, function (str, p1) {
return args[p1];
})
// replace double % and space with single %
.replace(/%% /g, '%')
};
var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}
return str;
};
var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
while (i > 0) {
locale = locale.slice(0, i);
locales.push(locale);
i = locale.lastIndexOf('-');
}
return locales;
};
var normalizeLocale = function (locale) {
// Convert locale to BCP 47. If the locale is in POSIX format, locale variant and encoding is discarded.
locale = locale.replace('_', '-');
var i = locale.search(/[.@]/);
if (i != -1) locale = locale.slice(0, i);
return locale;
};
var getPluralFunc = function (plural_form) {
// Plural form string regexp
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');
var match = plural_form.match(pf_re);
if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));
console.log('>>> Plural form:', plural_form);
// Careful here, this is a hidden eval() equivalent..
// Risk should be reasonable though since we test the plural_form through regex before
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// TODO: should test if https://github.com/soney/jsep present and use it if so
return new Function("n", 'var plural, nplurals; '+ plural_form +' return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };');
};
// Proper translation function that handle plurals and directives
// Contains juicy parts of https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (!options.plural_form)
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));
var plural;
// if a plural func is given, use that one
if (options.plural_func) {
plural = options.plural_func(n);
// if plural form never interpreted before, do it now and store it
} else if (!_plural_funcs[_locale]) {
_plural_funcs[_locale] = getPluralFunc(_plural_forms[_locale]);
plural = _plural_funcs[_locale](n);
// we have the plural function, compute the plural result
} else {
plural = _plural_funcs[_locale](n);
}
// If there is a problem with plurals, fallback to singular one
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};
return {
strfmt: strfmt, // expose strfmt util
expand_locale: expand_locale, // expose expand_locale util
// Declare shortcuts
__: function () { return this.gettext.apply(this, arguments); },
_n: function () { return this.ngettext.apply(this, arguments); },
_p: function () { return this.pgettext.apply(this, arguments); },
setMessages: function (domain, locale, messages, plural_forms) {
if (!domain || !locale || !messages)
throw new Error('You must provide a domain, a locale and messages');
if ('string' !== typeof domain || 'string' !== typeof locale || !_.isObject(messages))
throw new Error('Invalid arguments');
locale = normalizeLocale(locale);
if (plural_forms)
_plural_forms[locale] = plural_forms;
if (!_dictionary[domain])
_dictionary[domain] = {};
_dictionary[domain][locale] = messages;
return this;
},
loadJSON: function (jsonData, domain) {
if (!_.isObject(jsonData))
jsonData = JSON.parse(jsonData);
if (!jsonData[''] || !jsonData['']['language'] || !jsonData['']['plural-forms'])
throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');
var headers = jsonData[''];
delete jsonData[''];
return this.setMessages(domain || defaults.domain, headers['language'], jsonData, headers['plural-forms']);
},
setLocale: function (locale) {
_locale = normalizeLocale(locale);
return this;
},
getLocale: function () {
return _locale;
},
// getter/setter for domain
textdomain: function (domain) {
if (!domain)
return _domain;
_domain = domain;
return this;
},
gettext: function (msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 1)));
},
ngettext: function (msgid, msgid_plural, n /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, msgid_plural, n].concat(Array.prototype.slice.call(arguments, 3)));
},
pgettext: function (msgctxt, msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, msgctxt, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 2)));
},
dcnpgettext: function (domain, msgctxt, msgid, msgid_plural, n /* , extra */) {
domain = domain || _domain;
if ('string' !== typeof msgid)
throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string', msgid));
var
translation,
options = { plural_form: false },
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale,
locales = expand_locale(_locale);
for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];
// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}
if (!exist) {
translation = msgid;
options.plural_func = defaults.plural_func;
} else {
translation = _dictionary[domain][locale][key];
}
// Singular form
if (!msgid_plural)
return t.apply(this, [[translation], n, options].concat(Array.prototype.slice.call(arguments, 5)));
// Plural one
options.plural_form = true;
return t.apply(this, [exist ? translation : [msgid, msgid_plural], n, options].concat(Array.prototype.slice.call(arguments, 5)));
}
};
};
return i18n;
});

View File

@@ -0,0 +1,2 @@
define((function(){"use strict";
/*! gettext.js - Guillaume Potier - MIT Licensed */return function(t){t=t||{},this&&(this.__version="2.0.0");var r={domain:"messages",locale:"undefined"!=typeof document&&document.documentElement.getAttribute("lang")||"en",plural_func:function(t){return{nplurals:2,plural:1!=t?1:0}},ctxt_delimiter:String.fromCharCode(4)},e=function(t){var r=typeof t;return"function"===r||"object"===r&&!!t},n={},l=t.locale||r.locale,a=t.domain||r.domain,o={},i={},u=t.ctxt_delimiter||r.ctxt_delimiter;t.messages&&(o[a]={},o[a][l]=t.messages),t.plural_forms&&(i[l]=t.plural_forms);var s=function(t){var r=arguments;return t.replace(/%%/g,"%% ").replace(/%(\d+)/g,(function(t,e){return r[e]})).replace(/%% /g,"%")},p=function(t){return-1!==t.indexOf(u)?t.split(u)[1]:t},c=function(t){for(var r=[t],e=t.lastIndexOf("-");e>0;)t=t.slice(0,e),r.push(t),e=t.lastIndexOf("-");return r},f=function(t){var r=(t=t.replace("_","-")).search(/[.@]/);return-1!=r&&(t=t.slice(0,r)),t},d=function(t){var r=new RegExp("^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_()])+"),e=t.match(r);if(!e||e[0]!==t)throw new Error(s('The plural form "%1" is not valid',t));return console.log(">>> Plural form:",t),new Function("n","var plural, nplurals; "+t+" return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };")},g=function(t,r,e){return e.plural_form?(e.plural_func?a=e.plural_func(r):(n[l]||(n[l]=d(i[l])),a=n[l](r)),(void 0===a.plural||a.plural>a.nplurals||t.length<=a.plural)&&(a.plural=0),s.apply(this,[p(t[a.plural])].concat(Array.prototype.slice.call(arguments,3)))):s.apply(this,[p(t[0])].concat(Array.prototype.slice.call(arguments,3)));var a};return{strfmt:s,expand_locale:c,__:function(){return this.gettext.apply(this,arguments)},_n:function(){return this.ngettext.apply(this,arguments)},_p:function(){return this.pgettext.apply(this,arguments)},setMessages:function(t,r,n,l){if(!t||!r||!n)throw new Error("You must provide a domain, a locale and messages");if("string"!=typeof t||"string"!=typeof r||!e(n))throw new Error("Invalid arguments");return r=f(r),l&&(i[r]=l),o[t]||(o[t]={}),o[t][r]=n,this},loadJSON:function(t,n){if(e(t)||(t=JSON.parse(t)),!t[""]||!t[""].language||!t[""]["plural-forms"])throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');var l=t[""];return delete t[""],this.setMessages(n||r.domain,l.language,t,l["plural-forms"])},setLocale:function(t){return l=f(t),this},getLocale:function(){return l},textdomain:function(t){return t?(a=t,this):a},gettext:function(t){return this.dcnpgettext.apply(this,[void 0,void 0,t,void 0,void 0].concat(Array.prototype.slice.call(arguments,1)))},ngettext:function(t,r,e){return this.dcnpgettext.apply(this,[void 0,void 0,t,r,e].concat(Array.prototype.slice.call(arguments,3)))},pgettext:function(t,r){return this.dcnpgettext.apply(this,[void 0,t,r,void 0,void 0].concat(Array.prototype.slice.call(arguments,2)))},dcnpgettext:function(t,e,n,i,s){if(t=t||a,"string"!=typeof n)throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string',n));var p,f,d,h={plural_form:!1},m=e?e+u+n:n,y=c(l);for(var v in y)if(d=y[v],f=o[t]&&o[t][d]&&o[t][d][m],f=i?f&&"string"!=typeof o[t][d][m]:f&&"string"==typeof o[t][d][m])break;return f?p=o[t][d][m]:(p=n,h.plural_func=r.plural_func),i?(h.plural_form=!0,g.apply(this,[f?p:[n,i],s,h].concat(Array.prototype.slice.call(arguments,5)))):g.apply(this,[[p],s,h].concat(Array.prototype.slice.call(arguments,5)))}}}}));

View File

@@ -0,0 +1,253 @@
'use strict';
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '2.0.0');
// default values that could be overriden in i18n() construct
var defaults = {
domain: 'messages',
locale: (typeof document !== 'undefined' ? document.documentElement.getAttribute('lang') : false) || 'en',
plural_func: function (n) { return { nplurals: 2, plural: (n!=1) ? 1 : 0 }; },
ctxt_delimiter: String.fromCharCode(4) // \u0004
};
// handy mixins taken from underscode.js
var _ = {
isObject: function (obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
},
isArray: function (obj) {
return toString.call(obj) === '[object Array]';
}
};
var
_plural_funcs = {},
_locale = options.locale || defaults.locale,
_domain = options.domain || defaults.domain,
_dictionary = {},
_plural_forms = {},
_ctxt_delimiter = options.ctxt_delimiter || defaults.ctxt_delimiter;
if (options.messages) {
_dictionary[_domain] = {};
_dictionary[_domain][_locale] = options.messages;
}
if (options.plural_forms) {
_plural_forms[_locale] = options.plural_forms;
}
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
.replace(/%(\d+)/g, function (str, p1) {
return args[p1];
})
// replace double % and space with single %
.replace(/%% /g, '%')
};
var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}
return str;
};
var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
while (i > 0) {
locale = locale.slice(0, i);
locales.push(locale);
i = locale.lastIndexOf('-');
}
return locales;
};
var normalizeLocale = function (locale) {
// Convert locale to BCP 47. If the locale is in POSIX format, locale variant and encoding is discarded.
locale = locale.replace('_', '-');
var i = locale.search(/[.@]/);
if (i != -1) locale = locale.slice(0, i);
return locale;
};
var getPluralFunc = function (plural_form) {
// Plural form string regexp
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');
var match = plural_form.match(pf_re);
if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));
console.log('>>> Plural form:', plural_form);
// Careful here, this is a hidden eval() equivalent..
// Risk should be reasonable though since we test the plural_form through regex before
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// TODO: should test if https://github.com/soney/jsep present and use it if so
return new Function("n", 'var plural, nplurals; '+ plural_form +' return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };');
};
// Proper translation function that handle plurals and directives
// Contains juicy parts of https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (!options.plural_form)
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));
var plural;
// if a plural func is given, use that one
if (options.plural_func) {
plural = options.plural_func(n);
// if plural form never interpreted before, do it now and store it
} else if (!_plural_funcs[_locale]) {
_plural_funcs[_locale] = getPluralFunc(_plural_forms[_locale]);
plural = _plural_funcs[_locale](n);
// we have the plural function, compute the plural result
} else {
plural = _plural_funcs[_locale](n);
}
// If there is a problem with plurals, fallback to singular one
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};
return {
strfmt: strfmt, // expose strfmt util
expand_locale: expand_locale, // expose expand_locale util
// Declare shortcuts
__: function () { return this.gettext.apply(this, arguments); },
_n: function () { return this.ngettext.apply(this, arguments); },
_p: function () { return this.pgettext.apply(this, arguments); },
setMessages: function (domain, locale, messages, plural_forms) {
if (!domain || !locale || !messages)
throw new Error('You must provide a domain, a locale and messages');
if ('string' !== typeof domain || 'string' !== typeof locale || !_.isObject(messages))
throw new Error('Invalid arguments');
locale = normalizeLocale(locale);
if (plural_forms)
_plural_forms[locale] = plural_forms;
if (!_dictionary[domain])
_dictionary[domain] = {};
_dictionary[domain][locale] = messages;
return this;
},
loadJSON: function (jsonData, domain) {
if (!_.isObject(jsonData))
jsonData = JSON.parse(jsonData);
if (!jsonData[''] || !jsonData['']['language'] || !jsonData['']['plural-forms'])
throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');
var headers = jsonData[''];
delete jsonData[''];
return this.setMessages(domain || defaults.domain, headers['language'], jsonData, headers['plural-forms']);
},
setLocale: function (locale) {
_locale = normalizeLocale(locale);
return this;
},
getLocale: function () {
return _locale;
},
// getter/setter for domain
textdomain: function (domain) {
if (!domain)
return _domain;
_domain = domain;
return this;
},
gettext: function (msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 1)));
},
ngettext: function (msgid, msgid_plural, n /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, msgid_plural, n].concat(Array.prototype.slice.call(arguments, 3)));
},
pgettext: function (msgctxt, msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, msgctxt, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 2)));
},
dcnpgettext: function (domain, msgctxt, msgid, msgid_plural, n /* , extra */) {
domain = domain || _domain;
if ('string' !== typeof msgid)
throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string', msgid));
var
translation,
options = { plural_form: false },
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale,
locales = expand_locale(_locale);
for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];
// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}
if (!exist) {
translation = msgid;
options.plural_func = defaults.plural_func;
} else {
translation = _dictionary[domain][locale][key];
}
// Singular form
if (!msgid_plural)
return t.apply(this, [[translation], n, options].concat(Array.prototype.slice.call(arguments, 5)));
// Plural one
options.plural_form = true;
return t.apply(this, [exist ? translation : [msgid, msgid_plural], n, options].concat(Array.prototype.slice.call(arguments, 5)));
}
};
};
module.exports = i18n;

View File

@@ -0,0 +1,2 @@
"use strict";
/*! gettext.js - Guillaume Potier - MIT Licensed */var i18n=function(t){t=t||{},this&&(this.__version="2.0.0");var r={domain:"messages",locale:"undefined"!=typeof document&&document.documentElement.getAttribute("lang")||"en",plural_func:function(t){return{nplurals:2,plural:1!=t?1:0}},ctxt_delimiter:String.fromCharCode(4)},e=function(t){var r=typeof t;return"function"===r||"object"===r&&!!t},n={},l=t.locale||r.locale,a=t.domain||r.domain,o={},i={},u=t.ctxt_delimiter||r.ctxt_delimiter;t.messages&&(o[a]={},o[a][l]=t.messages),t.plural_forms&&(i[l]=t.plural_forms);var s=function(t){var r=arguments;return t.replace(/%%/g,"%% ").replace(/%(\d+)/g,(function(t,e){return r[e]})).replace(/%% /g,"%")},p=function(t){return-1!==t.indexOf(u)?t.split(u)[1]:t},c=function(t){for(var r=[t],e=t.lastIndexOf("-");e>0;)t=t.slice(0,e),r.push(t),e=t.lastIndexOf("-");return r},f=function(t){var r=(t=t.replace("_","-")).search(/[.@]/);return-1!=r&&(t=t.slice(0,r)),t},d=function(t){var r=new RegExp("^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_()])+"),e=t.match(r);if(!e||e[0]!==t)throw new Error(s('The plural form "%1" is not valid',t));return console.log(">>> Plural form:",t),new Function("n","var plural, nplurals; "+t+" return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };")},g=function(t,r,e){return e.plural_form?(e.plural_func?a=e.plural_func(r):(n[l]||(n[l]=d(i[l])),a=n[l](r)),(void 0===a.plural||a.plural>a.nplurals||t.length<=a.plural)&&(a.plural=0),s.apply(this,[p(t[a.plural])].concat(Array.prototype.slice.call(arguments,3)))):s.apply(this,[p(t[0])].concat(Array.prototype.slice.call(arguments,3)));var a};return{strfmt:s,expand_locale:c,__:function(){return this.gettext.apply(this,arguments)},_n:function(){return this.ngettext.apply(this,arguments)},_p:function(){return this.pgettext.apply(this,arguments)},setMessages:function(t,r,n,l){if(!t||!r||!n)throw new Error("You must provide a domain, a locale and messages");if("string"!=typeof t||"string"!=typeof r||!e(n))throw new Error("Invalid arguments");return r=f(r),l&&(i[r]=l),o[t]||(o[t]={}),o[t][r]=n,this},loadJSON:function(t,n){if(e(t)||(t=JSON.parse(t)),!t[""]||!t[""].language||!t[""]["plural-forms"])throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');var l=t[""];return delete t[""],this.setMessages(n||r.domain,l.language,t,l["plural-forms"])},setLocale:function(t){return l=f(t),this},getLocale:function(){return l},textdomain:function(t){return t?(a=t,this):a},gettext:function(t){return this.dcnpgettext.apply(this,[void 0,void 0,t,void 0,void 0].concat(Array.prototype.slice.call(arguments,1)))},ngettext:function(t,r,e){return this.dcnpgettext.apply(this,[void 0,void 0,t,r,e].concat(Array.prototype.slice.call(arguments,3)))},pgettext:function(t,r){return this.dcnpgettext.apply(this,[void 0,t,r,void 0,void 0].concat(Array.prototype.slice.call(arguments,2)))},dcnpgettext:function(t,e,n,i,s){if(t=t||a,"string"!=typeof n)throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string',n));var p,f,d,m={plural_form:!1},h=e?e+u+n:n,y=c(l);for(var v in y)if(d=y[v],f=o[t]&&o[t][d]&&o[t][d][h],f=i?f&&"string"!=typeof o[t][d][h]:f&&"string"==typeof o[t][d][h])break;return f?p=o[t][d][h]:(p=n,m.plural_func=r.plural_func),i?(m.plural_form=!0,g.apply(this,[f?p:[n,i],s,m].concat(Array.prototype.slice.call(arguments,5)))):g.apply(this,[[p],s,m].concat(Array.prototype.slice.call(arguments,5)))}}};module.exports=i18n;

View File

@@ -0,0 +1,251 @@
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '2.0.0');
// default values that could be overriden in i18n() construct
var defaults = {
domain: 'messages',
locale: (typeof document !== 'undefined' ? document.documentElement.getAttribute('lang') : false) || 'en',
plural_func: function (n) { return { nplurals: 2, plural: (n!=1) ? 1 : 0 }; },
ctxt_delimiter: String.fromCharCode(4) // \u0004
};
// handy mixins taken from underscode.js
var _ = {
isObject: function (obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
},
isArray: function (obj) {
return toString.call(obj) === '[object Array]';
}
};
var
_plural_funcs = {},
_locale = options.locale || defaults.locale,
_domain = options.domain || defaults.domain,
_dictionary = {},
_plural_forms = {},
_ctxt_delimiter = options.ctxt_delimiter || defaults.ctxt_delimiter;
if (options.messages) {
_dictionary[_domain] = {};
_dictionary[_domain][_locale] = options.messages;
}
if (options.plural_forms) {
_plural_forms[_locale] = options.plural_forms;
}
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
.replace(/%(\d+)/g, function (str, p1) {
return args[p1];
})
// replace double % and space with single %
.replace(/%% /g, '%')
};
var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}
return str;
};
var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
while (i > 0) {
locale = locale.slice(0, i);
locales.push(locale);
i = locale.lastIndexOf('-');
}
return locales;
};
var normalizeLocale = function (locale) {
// Convert locale to BCP 47. If the locale is in POSIX format, locale variant and encoding is discarded.
locale = locale.replace('_', '-');
var i = locale.search(/[.@]/);
if (i != -1) locale = locale.slice(0, i);
return locale;
};
var getPluralFunc = function (plural_form) {
// Plural form string regexp
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');
var match = plural_form.match(pf_re);
if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));
console.log('>>> Plural form:', plural_form);
// Careful here, this is a hidden eval() equivalent..
// Risk should be reasonable though since we test the plural_form through regex before
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// TODO: should test if https://github.com/soney/jsep present and use it if so
return new Function("n", 'var plural, nplurals; '+ plural_form +' return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };');
};
// Proper translation function that handle plurals and directives
// Contains juicy parts of https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (!options.plural_form)
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));
var plural;
// if a plural func is given, use that one
if (options.plural_func) {
plural = options.plural_func(n);
// if plural form never interpreted before, do it now and store it
} else if (!_plural_funcs[_locale]) {
_plural_funcs[_locale] = getPluralFunc(_plural_forms[_locale]);
plural = _plural_funcs[_locale](n);
// we have the plural function, compute the plural result
} else {
plural = _plural_funcs[_locale](n);
}
// If there is a problem with plurals, fallback to singular one
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};
return {
strfmt: strfmt, // expose strfmt util
expand_locale: expand_locale, // expose expand_locale util
// Declare shortcuts
__: function () { return this.gettext.apply(this, arguments); },
_n: function () { return this.ngettext.apply(this, arguments); },
_p: function () { return this.pgettext.apply(this, arguments); },
setMessages: function (domain, locale, messages, plural_forms) {
if (!domain || !locale || !messages)
throw new Error('You must provide a domain, a locale and messages');
if ('string' !== typeof domain || 'string' !== typeof locale || !_.isObject(messages))
throw new Error('Invalid arguments');
locale = normalizeLocale(locale);
if (plural_forms)
_plural_forms[locale] = plural_forms;
if (!_dictionary[domain])
_dictionary[domain] = {};
_dictionary[domain][locale] = messages;
return this;
},
loadJSON: function (jsonData, domain) {
if (!_.isObject(jsonData))
jsonData = JSON.parse(jsonData);
if (!jsonData[''] || !jsonData['']['language'] || !jsonData['']['plural-forms'])
throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');
var headers = jsonData[''];
delete jsonData[''];
return this.setMessages(domain || defaults.domain, headers['language'], jsonData, headers['plural-forms']);
},
setLocale: function (locale) {
_locale = normalizeLocale(locale);
return this;
},
getLocale: function () {
return _locale;
},
// getter/setter for domain
textdomain: function (domain) {
if (!domain)
return _domain;
_domain = domain;
return this;
},
gettext: function (msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 1)));
},
ngettext: function (msgid, msgid_plural, n /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, msgid_plural, n].concat(Array.prototype.slice.call(arguments, 3)));
},
pgettext: function (msgctxt, msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, msgctxt, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 2)));
},
dcnpgettext: function (domain, msgctxt, msgid, msgid_plural, n /* , extra */) {
domain = domain || _domain;
if ('string' !== typeof msgid)
throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string', msgid));
var
translation,
options = { plural_form: false },
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale,
locales = expand_locale(_locale);
for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];
// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}
if (!exist) {
translation = msgid;
options.plural_func = defaults.plural_func;
} else {
translation = _dictionary[domain][locale][key];
}
// Singular form
if (!msgid_plural)
return t.apply(this, [[translation], n, options].concat(Array.prototype.slice.call(arguments, 5)));
// Plural one
options.plural_form = true;
return t.apply(this, [exist ? translation : [msgid, msgid_plural], n, options].concat(Array.prototype.slice.call(arguments, 5)));
}
};
};
export default i18n;

View File

@@ -0,0 +1,2 @@
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n=function(t){t=t||{},this&&(this.__version="2.0.0");var r={domain:"messages",locale:"undefined"!=typeof document&&document.documentElement.getAttribute("lang")||"en",plural_func:function(t){return{nplurals:2,plural:1!=t?1:0}},ctxt_delimiter:String.fromCharCode(4)},e=function(t){var r=typeof t;return"function"===r||"object"===r&&!!t},n={},l=t.locale||r.locale,a=t.domain||r.domain,o={},i={},u=t.ctxt_delimiter||r.ctxt_delimiter;t.messages&&(o[a]={},o[a][l]=t.messages),t.plural_forms&&(i[l]=t.plural_forms);var s=function(t){var r=arguments;return t.replace(/%%/g,"%% ").replace(/%(\d+)/g,(function(t,e){return r[e]})).replace(/%% /g,"%")},p=function(t){return-1!==t.indexOf(u)?t.split(u)[1]:t},c=function(t){for(var r=[t],e=t.lastIndexOf("-");e>0;)t=t.slice(0,e),r.push(t),e=t.lastIndexOf("-");return r},f=function(t){var r=(t=t.replace("_","-")).search(/[.@]/);return-1!=r&&(t=t.slice(0,r)),t},d=function(t){var r=new RegExp("^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_()])+"),e=t.match(r);if(!e||e[0]!==t)throw new Error(s('The plural form "%1" is not valid',t));return console.log(">>> Plural form:",t),new Function("n","var plural, nplurals; "+t+" return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };")},g=function(t,r,e){return e.plural_form?(e.plural_func?a=e.plural_func(r):(n[l]||(n[l]=d(i[l])),a=n[l](r)),(void 0===a.plural||a.plural>a.nplurals||t.length<=a.plural)&&(a.plural=0),s.apply(this,[p(t[a.plural])].concat(Array.prototype.slice.call(arguments,3)))):s.apply(this,[p(t[0])].concat(Array.prototype.slice.call(arguments,3)));var a};return{strfmt:s,expand_locale:c,__:function(){return this.gettext.apply(this,arguments)},_n:function(){return this.ngettext.apply(this,arguments)},_p:function(){return this.pgettext.apply(this,arguments)},setMessages:function(t,r,n,l){if(!t||!r||!n)throw new Error("You must provide a domain, a locale and messages");if("string"!=typeof t||"string"!=typeof r||!e(n))throw new Error("Invalid arguments");return r=f(r),l&&(i[r]=l),o[t]||(o[t]={}),o[t][r]=n,this},loadJSON:function(t,n){if(e(t)||(t=JSON.parse(t)),!t[""]||!t[""].language||!t[""]["plural-forms"])throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');var l=t[""];return delete t[""],this.setMessages(n||r.domain,l.language,t,l["plural-forms"])},setLocale:function(t){return l=f(t),this},getLocale:function(){return l},textdomain:function(t){return t?(a=t,this):a},gettext:function(t){return this.dcnpgettext.apply(this,[void 0,void 0,t,void 0,void 0].concat(Array.prototype.slice.call(arguments,1)))},ngettext:function(t,r,e){return this.dcnpgettext.apply(this,[void 0,void 0,t,r,e].concat(Array.prototype.slice.call(arguments,3)))},pgettext:function(t,r){return this.dcnpgettext.apply(this,[void 0,t,r,void 0,void 0].concat(Array.prototype.slice.call(arguments,2)))},dcnpgettext:function(t,e,n,i,s){if(t=t||a,"string"!=typeof n)throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string',n));var p,f,d,h={plural_form:!1},m=e?e+u+n:n,y=c(l);for(var v in y)if(d=y[v],f=o[t]&&o[t][d]&&o[t][d][m],f=i?f&&"string"!=typeof o[t][d][m]:f&&"string"==typeof o[t][d][m])break;return f?p=o[t][d][m]:(p=n,h.plural_func=r.plural_func),i?(h.plural_form=!0,g.apply(this,[f?p:[n,i],s,h].concat(Array.prototype.slice.call(arguments,5)))):g.apply(this,[[p],s,h].concat(Array.prototype.slice.call(arguments,5)))}}};export default i18n;

View File

@@ -0,0 +1,256 @@
var i18n = (function () {
'use strict';
/*! gettext.js - Guillaume Potier - MIT Licensed */
var i18n = function (options) {
options = options || {};
this && (this.__version = '2.0.0');
// default values that could be overriden in i18n() construct
var defaults = {
domain: 'messages',
locale: (typeof document !== 'undefined' ? document.documentElement.getAttribute('lang') : false) || 'en',
plural_func: function (n) { return { nplurals: 2, plural: (n!=1) ? 1 : 0 }; },
ctxt_delimiter: String.fromCharCode(4) // \u0004
};
// handy mixins taken from underscode.js
var _ = {
isObject: function (obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
},
isArray: function (obj) {
return toString.call(obj) === '[object Array]';
}
};
var
_plural_funcs = {},
_locale = options.locale || defaults.locale,
_domain = options.domain || defaults.domain,
_dictionary = {},
_plural_forms = {},
_ctxt_delimiter = options.ctxt_delimiter || defaults.ctxt_delimiter;
if (options.messages) {
_dictionary[_domain] = {};
_dictionary[_domain][_locale] = options.messages;
}
if (options.plural_forms) {
_plural_forms[_locale] = options.plural_forms;
}
// sprintf equivalent, takes a string and some arguments to make a computed string
// eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
// eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
// NB: removes msg context if there is one present
var strfmt = function (fmt) {
var args = arguments;
return fmt
// put space after double % to prevent placeholder replacement of such matches
.replace(/%%/g, '%% ')
// replace placeholders
.replace(/%(\d+)/g, function (str, p1) {
return args[p1];
})
// replace double % and space with single %
.replace(/%% /g, '%')
};
var removeContext = function(str) {
// if there is context, remove it
if (str.indexOf(_ctxt_delimiter) !== -1) {
var parts = str.split(_ctxt_delimiter);
return parts[1];
}
return str;
};
var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
while (i > 0) {
locale = locale.slice(0, i);
locales.push(locale);
i = locale.lastIndexOf('-');
}
return locales;
};
var normalizeLocale = function (locale) {
// Convert locale to BCP 47. If the locale is in POSIX format, locale variant and encoding is discarded.
locale = locale.replace('_', '-');
var i = locale.search(/[.@]/);
if (i != -1) locale = locale.slice(0, i);
return locale;
};
var getPluralFunc = function (plural_form) {
// Plural form string regexp
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');
var match = plural_form.match(pf_re);
if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));
console.log('>>> Plural form:', plural_form);
// Careful here, this is a hidden eval() equivalent..
// Risk should be reasonable though since we test the plural_form through regex before
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
// TODO: should test if https://github.com/soney/jsep present and use it if so
return new Function("n", 'var plural, nplurals; '+ plural_form +' return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };');
};
// Proper translation function that handle plurals and directives
// Contains juicy parts of https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
var t = function (messages, n, options /* ,extra */) {
// Singular is very easy, just pass dictionnary message through strfmt
if (!options.plural_form)
return strfmt.apply(this, [removeContext(messages[0])].concat(Array.prototype.slice.call(arguments, 3)));
var plural;
// if a plural func is given, use that one
if (options.plural_func) {
plural = options.plural_func(n);
// if plural form never interpreted before, do it now and store it
} else if (!_plural_funcs[_locale]) {
_plural_funcs[_locale] = getPluralFunc(_plural_forms[_locale]);
plural = _plural_funcs[_locale](n);
// we have the plural function, compute the plural result
} else {
plural = _plural_funcs[_locale](n);
}
// If there is a problem with plurals, fallback to singular one
if ('undefined' === typeof plural.plural || plural.plural > plural.nplurals || messages.length <= plural.plural)
plural.plural = 0;
return strfmt.apply(this, [removeContext(messages[plural.plural])].concat(Array.prototype.slice.call(arguments, 3)));
};
return {
strfmt: strfmt, // expose strfmt util
expand_locale: expand_locale, // expose expand_locale util
// Declare shortcuts
__: function () { return this.gettext.apply(this, arguments); },
_n: function () { return this.ngettext.apply(this, arguments); },
_p: function () { return this.pgettext.apply(this, arguments); },
setMessages: function (domain, locale, messages, plural_forms) {
if (!domain || !locale || !messages)
throw new Error('You must provide a domain, a locale and messages');
if ('string' !== typeof domain || 'string' !== typeof locale || !_.isObject(messages))
throw new Error('Invalid arguments');
locale = normalizeLocale(locale);
if (plural_forms)
_plural_forms[locale] = plural_forms;
if (!_dictionary[domain])
_dictionary[domain] = {};
_dictionary[domain][locale] = messages;
return this;
},
loadJSON: function (jsonData, domain) {
if (!_.isObject(jsonData))
jsonData = JSON.parse(jsonData);
if (!jsonData[''] || !jsonData['']['language'] || !jsonData['']['plural-forms'])
throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');
var headers = jsonData[''];
delete jsonData[''];
return this.setMessages(domain || defaults.domain, headers['language'], jsonData, headers['plural-forms']);
},
setLocale: function (locale) {
_locale = normalizeLocale(locale);
return this;
},
getLocale: function () {
return _locale;
},
// getter/setter for domain
textdomain: function (domain) {
if (!domain)
return _domain;
_domain = domain;
return this;
},
gettext: function (msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 1)));
},
ngettext: function (msgid, msgid_plural, n /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, undefined, msgid, msgid_plural, n].concat(Array.prototype.slice.call(arguments, 3)));
},
pgettext: function (msgctxt, msgid /* , extra */) {
return this.dcnpgettext.apply(this, [undefined, msgctxt, msgid, undefined, undefined].concat(Array.prototype.slice.call(arguments, 2)));
},
dcnpgettext: function (domain, msgctxt, msgid, msgid_plural, n /* , extra */) {
domain = domain || _domain;
if ('string' !== typeof msgid)
throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string', msgid));
var
translation,
options = { plural_form: false },
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist,
locale,
locales = expand_locale(_locale);
for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];
// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}
if (!exist) {
translation = msgid;
options.plural_func = defaults.plural_func;
} else {
translation = _dictionary[domain][locale][key];
}
// Singular form
if (!msgid_plural)
return t.apply(this, [[translation], n, options].concat(Array.prototype.slice.call(arguments, 5)));
// Plural one
options.plural_form = true;
return t.apply(this, [exist ? translation : [msgid, msgid_plural], n, options].concat(Array.prototype.slice.call(arguments, 5)));
}
};
};
return i18n;
}());

View File

@@ -0,0 +1,2 @@
var i18n=function(){"use strict";
/*! gettext.js - Guillaume Potier - MIT Licensed */return function(t){t=t||{},this&&(this.__version="2.0.0");var r={domain:"messages",locale:"undefined"!=typeof document&&document.documentElement.getAttribute("lang")||"en",plural_func:function(t){return{nplurals:2,plural:1!=t?1:0}},ctxt_delimiter:String.fromCharCode(4)},n=function(t){var r=typeof t;return"function"===r||"object"===r&&!!t},e={},l=t.locale||r.locale,a=t.domain||r.domain,o={},i={},u=t.ctxt_delimiter||r.ctxt_delimiter;t.messages&&(o[a]={},o[a][l]=t.messages),t.plural_forms&&(i[l]=t.plural_forms);var s=function(t){var r=arguments;return t.replace(/%%/g,"%% ").replace(/%(\d+)/g,(function(t,n){return r[n]})).replace(/%% /g,"%")},p=function(t){return-1!==t.indexOf(u)?t.split(u)[1]:t},c=function(t){for(var r=[t],n=t.lastIndexOf("-");n>0;)t=t.slice(0,n),r.push(t),n=t.lastIndexOf("-");return r},f=function(t){var r=(t=t.replace("_","-")).search(/[.@]/);return-1!=r&&(t=t.slice(0,r)),t},d=function(t){var r=new RegExp("^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_()])+"),n=t.match(r);if(!n||n[0]!==t)throw new Error(s('The plural form "%1" is not valid',t));return console.log(">>> Plural form:",t),new Function("n","var plural, nplurals; "+t+" return { nplurals: nplurals, plural: (plural === true ? 1 : (plural ? plural : 0)) };")},g=function(t,r,n){return n.plural_form?(n.plural_func?a=n.plural_func(r):(e[l]||(e[l]=d(i[l])),a=e[l](r)),(void 0===a.plural||a.plural>a.nplurals||t.length<=a.plural)&&(a.plural=0),s.apply(this,[p(t[a.plural])].concat(Array.prototype.slice.call(arguments,3)))):s.apply(this,[p(t[0])].concat(Array.prototype.slice.call(arguments,3)));var a};return{strfmt:s,expand_locale:c,__:function(){return this.gettext.apply(this,arguments)},_n:function(){return this.ngettext.apply(this,arguments)},_p:function(){return this.pgettext.apply(this,arguments)},setMessages:function(t,r,e,l){if(!t||!r||!e)throw new Error("You must provide a domain, a locale and messages");if("string"!=typeof t||"string"!=typeof r||!n(e))throw new Error("Invalid arguments");return r=f(r),l&&(i[r]=l),o[t]||(o[t]={}),o[t][r]=e,this},loadJSON:function(t,e){if(n(t)||(t=JSON.parse(t)),!t[""]||!t[""].language||!t[""]["plural-forms"])throw new Error('Wrong JSON, it must have an empty key ("") with "language" and "plural-forms" information');var l=t[""];return delete t[""],this.setMessages(e||r.domain,l.language,t,l["plural-forms"])},setLocale:function(t){return l=f(t),this},getLocale:function(){return l},textdomain:function(t){return t?(a=t,this):a},gettext:function(t){return this.dcnpgettext.apply(this,[void 0,void 0,t,void 0,void 0].concat(Array.prototype.slice.call(arguments,1)))},ngettext:function(t,r,n){return this.dcnpgettext.apply(this,[void 0,void 0,t,r,n].concat(Array.prototype.slice.call(arguments,3)))},pgettext:function(t,r){return this.dcnpgettext.apply(this,[void 0,t,r,void 0,void 0].concat(Array.prototype.slice.call(arguments,2)))},dcnpgettext:function(t,n,e,i,s){if(t=t||a,"string"!=typeof e)throw new Error(this.strfmt('Msgid "%1" is not a valid translatable string',e));var p,f,d,h={plural_form:!1},m=n?n+u+e:e,y=c(l);for(var v in y)if(d=y[v],f=o[t]&&o[t][d]&&o[t][d][m],f=i?f&&"string"!=typeof o[t][d][m]:f&&"string"==typeof o[t][d][m])break;return f?p=o[t][d][m]:(p=e,h.plural_func=r.plural_func),i?(h.plural_form=!0,g.apply(this,[f?p:[e,i],s,h].concat(Array.prototype.slice.call(arguments,5)))):g.apply(this,[[p],s,h].concat(Array.prototype.slice.call(arguments,5)))}}}}();