Browse Source

Update jed.js to 1.1.0

pull/170/head
Evan Theurer 10 years ago
parent
commit
63324502c3
  1. 2
      package.json
  2. 2
      src/i18n/helpers/po2json
  3. 66
      static/js/libs/jed.js
  4. 16
      static/js/services/translation.js

2
package.json

@ -2,7 +2,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"autoprefixer": ">= 3.1.0", "autoprefixer": ">= 3.1.0",
"po2json": ">= 0.3.0", "po2json": ">= 0.4.1",
"jshint": ">= 2.5.5" "jshint": ">= 2.5.5"
} }
} }

2
src/i18n/helpers/po2json

@ -7,7 +7,7 @@ var po2json = require('po2json'),
assert.equal(argv.length, 4, 'Usage: po2json <input_file.po> <output_file.json>'); assert.equal(argv.length, 4, 'Usage: po2json <input_file.po> <output_file.json>');
var result = po2json.parseFileSync(argv[2], { stringify: true, format: 'jed', pretty: false }), var result = po2json.parseFileSync(argv[2], { stringify: true, format: 'jed1.x', pretty: false }),
stream = fs.createWriteStream(argv[3], {}); stream = fs.createWriteStream(argv[3], {});
stream.write(result); stream.write(result);

66
static/js/libs/jed.js

@ -1,8 +1,7 @@
/**
* @preserve jed.js 1.1.0 https://github.com/SlexAxton/Jed
*/
/* /*
jed.js
v0.5.4
https://github.com/SlexAxton/Jed
----------- -----------
A gettext compatible i18n library for modern JavaScript Applications A gettext compatible i18n library for modern JavaScript Applications
@ -91,7 +90,9 @@ in order to offer easy upgrades -- jsgettext.berlios.de
} }
}, },
// The default domain if one is missing // The default domain if one is missing
"domain" : "messages" "domain" : "messages",
// enable debug mode to log untranslated strings to the console
"debug" : false
}; };
// Mix in the sent options with the default options // Mix in the sent options with the default options
@ -136,7 +137,7 @@ in order to offer easy upgrades -- jsgettext.berlios.de
}, },
fetch : function ( sArr ) { fetch : function ( sArr ) {
if ( {}.toString.call( sArr ) != '[object Array]' ) { if ( {}.toString.call( sArr ) != '[object Array]' ) {
sArr = [].slice.call(arguments); sArr = [].slice.call(arguments, 0);
} }
return ( sArr && sArr.length ? Jed.sprintf : function(x){ return x; } )( return ( sArr && sArr.length ? Jed.sprintf : function(x){ return x; } )(
this._i18n.dcnpgettext(this._domain, this._context, this._key, this._pkey, this._val), this._i18n.dcnpgettext(this._domain, this._context, this._key, this._pkey, this._val),
@ -221,9 +222,6 @@ in order to offer easy upgrades -- jsgettext.berlios.de
// isn't explicitly passed in // isn't explicitly passed in
domain = domain || this._textdomain; domain = domain || this._textdomain;
// Default the value to the singular case
val = typeof val == 'undefined' ? 1 : val;
var fallback; var fallback;
// Handle special cases // Handle special cases
@ -257,23 +255,34 @@ in order to offer easy upgrades -- jsgettext.berlios.de
throw new Error('No translation key found.'); throw new Error('No translation key found.');
} }
// Handle invalid numbers, but try casting strings for good measure
if ( typeof val != 'number' ) {
val = parseInt( val, 10 );
if ( isNaN( val ) ) {
throw new Error('The number that was passed in is not a number.');
}
}
var key = context ? context + Jed.context_delimiter + singular_key : singular_key, var key = context ? context + Jed.context_delimiter + singular_key : singular_key,
locale_data = this.options.locale_data, locale_data = this.options.locale_data,
dict = locale_data[ domain ], dict = locale_data[ domain ],
pluralForms = dict[""].plural_forms || (locale_data.messages || this.defaults.locale_data.messages)[""].plural_forms, defaultConf = (locale_data.messages || this.defaults.locale_data.messages)[""],
val_idx = getPluralFormFunc(pluralForms)(val) + 1, pluralForms = dict[""].plural_forms || dict[""]["Plural-Forms"] || dict[""]["plural-forms"] || defaultConf.plural_forms || defaultConf["Plural-Forms"] || defaultConf["plural-forms"],
val_list, val_list,
res; res;
var val_idx;
if (val === undefined) {
// No value passed in; assume singular key lookup.
val_idx = 0;
} else {
// Value has been passed in; use plural-forms calculations.
// Handle invalid numbers, but try casting strings for good measure
if ( typeof val != 'number' ) {
val = parseInt( val, 10 );
if ( isNaN( val ) ) {
throw new Error('The number that was passed in is not a number.');
}
}
val_idx = getPluralFormFunc(pluralForms)(val);
}
// Throw an error if a domain isn't found // Throw an error if a domain isn't found
if ( ! dict ) { if ( ! dict ) {
throw new Error('No domain named `' + domain + '` could be found.'); throw new Error('No domain named `' + domain + '` could be found.');
@ -283,20 +292,25 @@ in order to offer easy upgrades -- jsgettext.berlios.de
// If there is no match, then revert back to // If there is no match, then revert back to
// english style singular/plural with the keys passed in. // english style singular/plural with the keys passed in.
if ( ! val_list || val_idx >= val_list.length ) { if ( ! val_list || val_idx > val_list.length ) {
if (this.options.missing_key_callback) { if (this.options.missing_key_callback) {
this.options.missing_key_callback(key); this.options.missing_key_callback(key, domain);
}
res = [ singular_key, plural_key ];
// collect untranslated strings
if (this.options.debug===true) {
console.log(res[ getPluralFormFunc(pluralForms)( val ) ]);
} }
res = [ null, singular_key, plural_key ]; return res[ getPluralFormFunc()( val ) ];
return res[ getPluralFormFunc(pluralForms)( val ) + 1 ];
} }
res = val_list[ val_idx ]; res = val_list[ val_idx ];
// This includes empty strings on purpose // This includes empty strings on purpose
if ( ! res ) { if ( ! res ) {
res = [ null, singular_key, plural_key ]; res = [ singular_key, plural_key ];
return res[ getPluralFormFunc(pluralForms)( val ) + 1 ]; return res[ getPluralFormFunc()( val ) ];
} }
return res; return res;
} }

16
static/js/services/translation.js

@ -30,25 +30,25 @@ define(["jed", "underscore"], function(Jed, _) {
this._ = _.bind(function() { this._ = _.bind(function() {
if (domain && context) { if (domain && context) {
return _.bind(function(singular) { return _.bind(function(singular) {
var vars = Array.prototype.slice.call(arguments, 1); var vars = Array.prototype.slice.call(arguments, 0);
var r = i18n.translate(singular).onDomain(domain).withContext(context); var r = i18n.translate(singular).onDomain(domain).withContext(context);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}, this); }, this);
} else if (domain) { } else if (domain) {
return _.bind(function(singular) { return _.bind(function(singular) {
var vars = Array.prototype.slice.call(arguments, 1); var vars = Array.prototype.slice.call(arguments, 0);
var r = i18n.translate(singular).onDomain(domain); var r = i18n.translate(singular).onDomain(domain);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}, this); }, this);
} else if (context) { } else if (context) {
return _.bind(function(singular) { return _.bind(function(singular) {
var vars = Array.prototype.slice.call(arguments, 1); var vars = Array.prototype.slice.call(arguments, 0);
var r = i18n.translate(singular).withContext(context); var r = i18n.translate(singular).withContext(context);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}, this); }, this);
} else { } else {
return _.bind(function(singular) { return _.bind(function(singular) {
var vars = Array.prototype.slice.call(arguments, 1); var vars = Array.prototype.slice.call(arguments, 0);
var r = i18n.translate(singular); var r = i18n.translate(singular);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}, this); }, this);
@ -59,25 +59,25 @@ define(["jed", "underscore"], function(Jed, _) {
this._n = _.bind(function() { this._n = _.bind(function() {
if (domain && context) { if (domain && context) {
return _.bind(function(singular, plural) { return _.bind(function(singular, plural) {
var vars = Array.prototype.slice.call(arguments, 2); var vars = Array.prototype.slice.call(arguments, 1);
var r = i18n.translate(singular).onDomain(domain).withContext(context).ifPlural(vars[0], plural); var r = i18n.translate(singular).onDomain(domain).withContext(context).ifPlural(vars[0], plural);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}); });
} else if (domain) { } else if (domain) {
return _.bind(function(singular, plural) { return _.bind(function(singular, plural) {
var vars = Array.prototype.slice.call(arguments, 2); var vars = Array.prototype.slice.call(arguments, 1);
var r = i18n.translate(singular).onDomain(domain).ifPlural(vars[0], plural); var r = i18n.translate(singular).onDomain(domain).ifPlural(vars[0], plural);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}); });
} else if (context) { } else if (context) {
return _.bind(function(singular, plural) { return _.bind(function(singular, plural) {
var vars = Array.prototype.slice.call(arguments, 2); var vars = Array.prototype.slice.call(arguments, 1);
var r = i18n.translate(singular).withContext(context).ifPlural(vars[0], plural); var r = i18n.translate(singular).withContext(context).ifPlural(vars[0], plural);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}); });
} else { } else {
return _.bind(function(singular, plural) { return _.bind(function(singular, plural) {
var vars = Array.prototype.slice.call(arguments, 2); var vars = Array.prototype.slice.call(arguments, 1);
var r = i18n.translate(singular).ifPlural(vars[0], plural); var r = i18n.translate(singular).ifPlural(vars[0], plural);
return r.fetch.apply(r, vars); return r.fetch.apply(r, vars);
}) })

Loading…
Cancel
Save