123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- (function(root, factory) {
- if (typeof define === 'function' && define.amd) {
- define([], function() {
- return factory(root);
- });
- } else if (typeof exports === 'object') {
- module.exports = factory(root);
- } else {
- root.Polyglot = factory(root);
- }
- }(this, function(root) {
- 'use strict';
-
- function Polyglot(options) {
- options = options || {};
- this.phrases = {};
- this.extend(options.phrases || {});
- this.currentLocale = options.locale || 'en';
- this.allowMissing = !!options.allowMissing;
- this.warn = options.warn || warn;
- }
-
- Polyglot.VERSION = '0.4.3';
-
-
-
- Polyglot.prototype.locale = function(newLocale) {
- if (newLocale) this.currentLocale = newLocale;
- return this.currentLocale;
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Polyglot.prototype.extend = function(morePhrases, prefix) {
- var phrase;
- for (var key in morePhrases) {
- if (morePhrases.hasOwnProperty(key)) {
- phrase = morePhrases[key];
- if (prefix) key = prefix + '.' + key;
- if (typeof phrase === 'object') {
- this.extend(phrase, key);
- } else {
- this.phrases[key] = phrase;
- }
- }
- }
- };
-
-
-
-
-
- Polyglot.prototype.clear = function() {
- this.phrases = {};
- };
-
-
-
-
-
- Polyglot.prototype.replace = function(newPhrases) {
- this.clear();
- this.extend(newPhrases);
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Polyglot.prototype.t = function(key, options) {
- var phrase, result;
- options = options == null ? {} : options;
-
- if (typeof options === 'number') {
- options = {smart_count: options};
- }
- if (typeof this.phrases[key] === 'string') {
- phrase = this.phrases[key];
- } else if (typeof options._ === 'string') {
- phrase = options._;
- } else if (this.allowMissing) {
- phrase = key;
- } else {
- this.warn('Missing translation for key: "'+key+'"');
- result = key;
- }
- if (typeof phrase === 'string') {
- options = clone(options);
- result = choosePluralForm(phrase, this.currentLocale, options.smart_count);
- result = interpolate(result, options);
- }
- return result;
- };
-
-
-
- Polyglot.prototype.has = function(key) {
- return key in this.phrases;
- };
-
-
- var delimeter = '||||';
-
- var pluralTypes = {
- chinese: function(n) { return 0; },
- german: function(n) { return n !== 1 ? 1 : 0; },
- french: function(n) { return n > 1 ? 1 : 0; },
- russian: function(n) { return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; },
- czech: function(n) { return (n === 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2; },
- polish: function(n) { return (n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2); },
- icelandic: function(n) { return (n % 10 !== 1 || n % 100 === 11) ? 1 : 0; }
- };
-
- var pluralTypeToLanguages = {
- chinese: ['fa', 'id', 'ja', 'ko', 'lo', 'ms', 'th', 'tr', 'zh'],
- german: ['da', 'de', 'en', 'es', 'fi', 'el', 'he', 'hu', 'it', 'nl', 'no', 'pt', 'sv'],
- french: ['fr', 'tl', 'pt-br'],
- russian: ['hr', 'ru'],
- czech: ['cs'],
- polish: ['pl'],
- icelandic: ['is']
- };
- function langToTypeMap(mapping) {
- var type, langs, l, ret = {};
- for (type in mapping) {
- if (mapping.hasOwnProperty(type)) {
- langs = mapping[type];
- for (l in langs) {
- ret[langs[l]] = type;
- }
- }
- }
- return ret;
- }
-
- function trim(str){
- var trimRe = /^\s+|\s+$/g;
- return str.replace(trimRe, '');
- }
-
-
-
- function choosePluralForm(text, locale, count){
- var ret, texts, chosenText;
- if (count != null && text) {
- texts = text.split(delimeter);
- chosenText = texts[pluralTypeIndex(locale, count)] || texts[0];
- ret = trim(chosenText);
- } else {
- ret = text;
- }
- return ret;
- }
- function pluralTypeName(locale) {
- var langToPluralType = langToTypeMap(pluralTypeToLanguages);
- return langToPluralType[locale] || langToPluralType.en;
- }
- function pluralTypeIndex(locale, count) {
- return pluralTypes[pluralTypeName(locale)](count);
- }
-
-
-
-
- function interpolate(phrase, options) {
- for (var arg in options) {
- if (arg !== '_' && options.hasOwnProperty(arg)) {
-
-
-
- phrase = phrase.replace(new RegExp('%\\{'+arg+'\\}', 'g'), options[arg]);
- }
- }
- return phrase;
- }
-
-
-
- function warn(message) {
- root.console && root.console.warn && root.console.warn('WARNING: ' + message);
- }
-
-
-
- function clone(source) {
- var ret = {};
- for (var prop in source) {
- ret[prop] = source[prop];
- }
- return ret;
- }
- return Polyglot;
- }));
|