Socket
Socket
Sign inDemoInstall

i18next

Package Overview
Dependencies
Maintainers
1
Versions
495
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next - npm Package Compare versions

Comparing version 0.0.1 to 0.5.0

4

lib/dep/i18next-1.1.js

@@ -206,3 +206,3 @@ (function() {

var found;
for (i = 0, len = languages.length; i < len; i++ ) {
for (var i = 0, len = languages.length; i < len; i++ ) {
if (found) break;

@@ -247,3 +247,3 @@

var missingLngs = [];
for (i = 0, len = lngs.length; i < len; i++) {
for (var i = 0, len = lngs.length; i < len; i++) {
if (!store[lngs[i]]) missingLngs.push(lngs[i]);

@@ -250,0 +250,0 @@ }

(function(){function k(a,c){e.extend(d,a),typeof d.ns=="string"&&(d.ns={namespaces:[d.ns],defaultNs:d.ns}),d.lng||(d.lng=e.detectLanguage()),g=d.lng,j=[],g&&j.push(g),g.length===5&&j.push(g.substr(0,2)),j.indexOf(d.fallbackLng)===-1&&j.push(d.fallbackLng);if(d.resStore){f=d.resStore,b&&d.setJqueryExt&&m(),c&&c(q);return}t.load(j,d.ns,d.useLocalStorage,d.dynamicLoad,function(a,e){f=e,b&&d.setJqueryExt&&m(),c&&c(q)})}function l(a,b){k({lng:a},b)}function m(){function a(a,c){if(c.length===0)return;var d="text";if(c.indexOf("[")===0){var e=c.split("]");c=e[1],d=e[0].substr(1,e[0].length-1)}c.indexOf(";")===c.length-1&&(c=c.substr(0,c.length-2)),d==="text"?a.text(b.t(c,{defaultValue:a.text()})):a.attr(d,b.t(c,{defaultValue:a.attr(d)}))}b.t=b.t||q,b.fn.i18n=function(c){return this.each(function(){var c=b(this).find("[data-i18n]");c.each(function(){var c=b(this),d=c.attr("data-i18n");if(d.indexOf(";")<=d.length-1){var e=d.split(";");b.each(e,function(b,d){a(c,d)})}else a(c,d)})})}}function n(a,b){return e.each(b,function(b,c){a=a.replace([d.interpolationPrefix,b,d.interpolationSuffix].join(""),c)}),a}function o(a,b){while(a.indexOf(d.reusePrefix)!=-1){h++;if(h>d.maxRecursion)break;var c=a.indexOf(d.reusePrefix),e=a.indexOf(d.reuseSuffix,c)+d.reuseSuffix.length,f=a.substring(c,e),g=f.replace(d.reusePrefix,"").replace(d.reuseSuffix,""),i=r(g,b);a=a.replace(f,i)}return a}function p(a){return a.count&&typeof a.count!="string"&&a.count>1}function q(a,b){return h=0,r(a,b)}function r(a,b){b=b||{};var c=b.defaultValue||a;if(!f)return c;var h=d.ns.defaultNs;if(a.indexOf(d.nsseparator)>-1){var k=a.split(d.nsseparator);h=k[0],a=k[1]}if(p(b)){var l=e.extend({},b);delete l.count,l.defaultValue=d.pluralNotFound;var m=a+d.pluralSuffix,r=u.get(g,b.count);r!=="other"&&(m=m+"_"+r);var s=q(m,l);if(s!=d.pluralNotFound)return n(s,{count:b.count})}var v;for(i=0,len=j.length;i<len;i++){if(v)break;var w=j[i],x=a.split(d.keyseparator),y=0,z=f[w][h];while(x[y])z=z&&z[x[y]],y++;z&&(z=n(z,b),z=o(z,b),v=z)}return!v&&d.sendMissing&&t.postMissing(h,a,c),v?v:c}function s(){return navigator?navigator.language?navigator.language:navigator.userLanguage:d.fallbackLng}function v(){return g}var a=this,b=a.jQuery,c={};typeof module!="undefined"&&module.exports?module.exports=c:b?b.i18n=b.i18n||c:a.i18n=a.i18n||c;var d={lng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:!1,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),setJqueryExt:!0},e={extend:b?b.extend:undefined,each:b?b.each:undefined,ajax:b?b.ajax:undefined,detectLanguage:s},f=!1,g=!1,h=0,j=[],t={load:function(a,b,c,d,f){c?t._loadLocal(a,function(c,g){var h=[];for(i=0,len=a.length;i<len;i++)g[a[i]]||h.push(a[i]);h.length>0?t._fetch(h,b,d,function(a,b){e.extend(g,b),t._storeLocal(b),f(null,g)}):f(null,g)}):t._fetch(a,b,d,function(a,b){f(null,b)})},_loadLocal:function(a,b){var c={};if(window.localStorage){var d=a.length;e.each(a,function(a,e){var f=window.localStorage.getItem("res_"+e);f&&(c[e]=JSON.parse(f)),d--,d===0&&b(null,c)})}},_storeLocal:function(a){if(window.localStorage)for(var b in a)window.localStorage.setItem("res_"+b,JSON.stringify(a[b]));return},_fetch:function(a,b,c,f){var g={};if(!c){var h=b.namespaces.length*a.length;e.each(b.namespaces,function(b,c){e.each(a,function(a,b){t._fetchOne(b,c,function(a,d){g[b]=g[b]||{},g[b][c]=d,h--,h===0&&f(null,g)})})})}else e.ajax({url:n(d.resGetPath,{lng:a.join("+"),ns:b.namespaces.join("+")}),success:function(a,b,c){f(null,a)},error:function(a,b,c){f("failed loading resource.json error: "+c)},dataType:"json"})},_fetchOne:function(a,b,c){e.ajax({url:n(d.resGetPath,{lng:a,ns:b}),success:function(a,b,d){c(null,a)},error:function(a,b,d){c(d,{})},dataType:"json"})},postMissing:function(a,b,c){var g={};g[b]=c,e.ajax({url:n(d.resPostPath,{lng:d.fallbackLng,ns:a}),type:"POST",data:g,success:function(e,g,h){f[d.fallbackLng][a][b]=c},error:function(a,b,c){},dataType:"json"})}},u={rules:{sl:function(a){return a%100===1?"one":a%100===2?"two":a%100===3||a%100===4?"few":"other"}},addRule:function(a,b){u.rules[a]=b},get:function(a,b){function d(a,b){return u.rules[a]?u.rules[a](b):b===1?"one":"other"}var c=a.split("-");return c.length===2?d(c[0],b):d(a,b)}};c.init=k,c.setLng=l,c.t=q,c.translate=q,c.detectLanguage=e.detectLanguage,c.pluralExtensions=u,c.sync=t,c.functions=e,c.lng=v,c.options=d})()
(function(){function j(a,c){e.extend(d,a),typeof d.ns=="string"&&(d.ns={namespaces:[d.ns],defaultNs:d.ns}),d.lng||(d.lng=e.detectLanguage()),g=d.lng,i=[],g&&i.push(g),g.length===5&&i.push(g.substr(0,2)),i.indexOf(d.fallbackLng)===-1&&i.push(d.fallbackLng);if(d.resStore){f=d.resStore,b&&d.setJqueryExt&&l(),c&&c(p);return}s.load(i,d.ns,d.useLocalStorage,d.dynamicLoad,function(a,e){f=e,b&&d.setJqueryExt&&l(),c&&c(p)})}function k(a,b){j({lng:a},b)}function l(){function a(a,c){if(c.length===0)return;var d="text";if(c.indexOf("[")===0){var e=c.split("]");c=e[1],d=e[0].substr(1,e[0].length-1)}c.indexOf(";")===c.length-1&&(c=c.substr(0,c.length-2)),d==="text"?a.text(b.t(c,{defaultValue:a.text()})):a.attr(d,b.t(c,{defaultValue:a.attr(d)}))}b.t=b.t||p,b.fn.i18n=function(c){return this.each(function(){var c=b(this).find("[data-i18n]");c.each(function(){var c=b(this),d=c.attr("data-i18n");if(d.indexOf(";")<=d.length-1){var e=d.split(";");b.each(e,function(b,d){a(c,d)})}else a(c,d)})})}}function m(a,b){return e.each(b,function(b,c){a=a.replace([d.interpolationPrefix,b,d.interpolationSuffix].join(""),c)}),a}function n(a,b){while(a.indexOf(d.reusePrefix)!=-1){h++;if(h>d.maxRecursion)break;var c=a.indexOf(d.reusePrefix),e=a.indexOf(d.reuseSuffix,c)+d.reuseSuffix.length,f=a.substring(c,e),g=f.replace(d.reusePrefix,"").replace(d.reuseSuffix,""),i=q(g,b);a=a.replace(f,i)}return a}function o(a){return a.count&&typeof a.count!="string"&&a.count>1}function p(a,b){return h=0,q(a,b)}function q(a,b){b=b||{};var c=b.defaultValue||a;if(!f)return c;var h=d.ns.defaultNs;if(a.indexOf(d.nsseparator)>-1){var j=a.split(d.nsseparator);h=j[0],a=j[1]}if(o(b)){var k=e.extend({},b);delete k.count,k.defaultValue=d.pluralNotFound;var l=a+d.pluralSuffix,q=t.get(g,b.count);q!=="other"&&(l=l+"_"+q);var r=p(l,k);if(r!=d.pluralNotFound)return m(r,{count:b.count})}var u;for(var v=0,w=i.length;v<w;v++){if(u)break;var x=i[v],y=a.split(d.keyseparator),z=0,A=f[x][h];while(y[z])A=A&&A[y[z]],z++;A&&(A=m(A,b),A=n(A,b),u=A)}return!u&&d.sendMissing&&s.postMissing(h,a,c),u?u:c}function r(){return navigator?navigator.language?navigator.language:navigator.userLanguage:d.fallbackLng}function u(){return g}var a=this,b=a.jQuery,c={};typeof module!="undefined"&&module.exports?module.exports=c:b?b.i18n=b.i18n||c:a.i18n=a.i18n||c;var d={lng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:!1,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),setJqueryExt:!0},e={extend:b?b.extend:undefined,each:b?b.each:undefined,ajax:b?b.ajax:undefined,detectLanguage:r},f=!1,g=!1,h=0,i=[],s={load:function(a,b,c,d,f){c?s._loadLocal(a,function(c,g){var h=[];for(var i=0,j=a.length;i<j;i++)g[a[i]]||h.push(a[i]);h.length>0?s._fetch(h,b,d,function(a,b){e.extend(g,b),s._storeLocal(b),f(null,g)}):f(null,g)}):s._fetch(a,b,d,function(a,b){f(null,b)})},_loadLocal:function(a,b){var c={};if(window.localStorage){var d=a.length;e.each(a,function(a,e){var f=window.localStorage.getItem("res_"+e);f&&(c[e]=JSON.parse(f)),d--,d===0&&b(null,c)})}},_storeLocal:function(a){if(window.localStorage)for(var b in a)window.localStorage.setItem("res_"+b,JSON.stringify(a[b]));return},_fetch:function(a,b,c,f){var g={};if(!c){var h=b.namespaces.length*a.length;e.each(b.namespaces,function(b,c){e.each(a,function(a,b){s._fetchOne(b,c,function(a,d){g[b]=g[b]||{},g[b][c]=d,h--,h===0&&f(null,g)})})})}else e.ajax({url:m(d.resGetPath,{lng:a.join("+"),ns:b.namespaces.join("+")}),success:function(a,b,c){f(null,a)},error:function(a,b,c){f("failed loading resource.json error: "+c)},dataType:"json"})},_fetchOne:function(a,b,c){e.ajax({url:m(d.resGetPath,{lng:a,ns:b}),success:function(a,b,d){c(null,a)},error:function(a,b,d){c(d,{})},dataType:"json"})},postMissing:function(a,b,c){var g={};g[b]=c,e.ajax({url:m(d.resPostPath,{lng:d.fallbackLng,ns:a}),type:"POST",data:g,success:function(e,g,h){f[d.fallbackLng][a][b]=c},error:function(a,b,c){},dataType:"json"})}},t={rules:{sl:function(a){return a%100===1?"one":a%100===2?"two":a%100===3||a%100===4?"few":"other"}},addRule:function(a,b){t.rules[a]=b},get:function(a,b){function d(a,b){return t.rules[a]?t.rules[a](b):b===1?"one":"other"}var c=a.split("-");return c.length===2?d(c[0],b):d(a,b)}};c.init=j,c.setLng=k,c.t=p,c.translate=p,c.detectLanguage=e.detectLanguage,c.pluralExtensions=t,c.sync=s,c.functions=e,c.lng=u,c.options=d})()

@@ -5,3 +5,3 @@ {

, "description": "A i18n middleware for the Express.js framework."
, "version": "0.0.1"
, "version": "0.5.0"
, "repository": {

@@ -8,0 +8,0 @@ "type": "git"

@@ -6,4 +6,60 @@ # Introduction

- Translation inside your server code or template
- loading resourcefiles
- update resourcefiles with missing strings
- interpolation, plurals, nesting or resources
- serve the same resources to the clientside
# installation
npm install i18next
# usage
First require the module and init i18next:
var i18next = require('i18next');
i18next.init(); // for options see i18next-node gh-page
Register the express middleware, so we can check current language settings:
// Configuration
app.configure(function() {
app.use(express.bodyParser());
app.use(i18next.handle);
app.use(app.router);
[...]
});
Register AppHelper so you can use the translate function in your template:
i18next.registerAppHelper(app)
Now you can (depending on your template language) do something like this in you template:
// sample in jade
body
span= t('app.name')
To serve the clientside script and needed routes for resources and missing keys:
i18next.serveClientScript(app)
.serveDynamicResources(app)
.serveMissingKeyRoute(app);
now you can add the script to you page and use i18next on the client like on the server:
script(src='i18next/i18next.js', type='text/javascript')
$.i18n.init([options], function() {
$('#appname').text($.t('app.name'));
});
for more information on clientside usage have a look at [i18next](http://jamuhl.github.com/i18next/)
# sample
- [i18next-node_Sample](https://github.com/jamuhl/i18next-node/tree/master/sample)
# License

@@ -10,0 +66,0 @@

var i18n = require('../lib/i18nextWrapper')
, should = require('should');
, assert = require('assert');

@@ -19,5 +19,5 @@

}, function(t) {
t('simpleTest_en-US').should.equal('ok_from_en-US');
t('simpleTest_en').should.equal('ok_from_en');
t('simpleTest_dev').should.equal('ok_from_dev');
assert.equal(t('simpleTest_en-US'),'ok_from_en-US');
assert.equal(t('simpleTest_en'),'ok_from_en');
assert.equal(t('simpleTest_dev'),'ok_from_dev');

@@ -38,6 +38,6 @@ done();

}, function(t) {
t('simpleTest').should.equal('ok_from_en-US');
assert.equal(t('simpleTest'),'ok_from_en-US');
i18n.setLng('de-DE', function(t) {
t('simpleTest').should.equal('ok_from_de-DE');
assert.equal(t('simpleTest'),'ok_from_de-DE');

@@ -72,6 +72,6 @@ done();

}, function(t) {
t('nesting1').should.equal('1 2 3');
t('pluralTest', {count: 1}).should.equal('no plural');
t('pluralTest', {count: 2}).should.equal('plural');
t('interpolationTest', {toAdd: 'something'}).should.equal('added something');
assert.equal(t('nesting1'),'1 2 3');
assert.equal(t('pluralTest', {count: 1}),'no plural');
assert.equal(t('pluralTest', {count: 2}),'plural');
assert.equal(t('interpolationTest', {toAdd: 'something'}),'added something');

@@ -99,7 +99,7 @@ done();

}, function(t) {
t('beer', {count: 1}).should.equal('Pivo');
t('beer', {count: 2}).should.equal('Pivi');
t('beer', {count: 3}).should.equal('Piva');
t('beer', {count: 4}).should.equal('Piva');
t('beer', {count: 5}).should.equal('no idea ;)');
assert.equal(t('beer', {count: 1}),'Pivo');
assert.equal(t('beer', {count: 2}),'Pivi');
assert.equal(t('beer', {count: 3}),'Piva');
assert.equal(t('beer', {count: 4}),'Piva');
assert.equal(t('beer', {count: 5}),'no idea ;)');

@@ -106,0 +106,0 @@ done();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc