EzDevInfo.com

i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.

How to divide locale files in a modular express app with i18n-node?

I'm using i18n-node to translate a multilingual express web-app. The i18n-node package allows you to specify a single folder for all of the locale json files which contain all the strings in the UI of the app. Assuming the app is getting bigger & bigger, It would've been ideal to be able to divide the long monolithic language locale json which holds ALL of the app strings into separate files, each will reside in its custom module folder. Is there a way to specify multiple locale folders/files? Cheers Ajar


Source: (StackOverflow)

Paths in nodejs

I'm facing a problem with paths in nodeJs, I route the user to the index page when he specifies the language in the url like this :

   app.all('/:locale?/index',function(req,res,next){
      if(!req.params.locale){
         console.log('no param');
     res.render('index');
      } else {
       var language = req.params.locale;
        i18n.setLocale(language);
       res.render('index');
      }
    });

However, in my index.html page the source of images are specified this way : ./images/img1.png , when I route the user, my index.html shows image not found because it considers the path " lang/images/img1.png , it considers the languge in my URL, could you please help?

Thank you


Source: (StackOverflow)

Advertisements

node.js: is there no way to put HTML into i18n-node JSON translation files?

The question says it all. If I put HTML directly into the (JSON-formatted) translation file, like this:

"test_html" : "click <a rel='nofollow' href='http://stackoverflow.com/'>here</a>",

I get this in my HTML:

click &lt;a rel='nofollow' href=&#39;http://stackoverflow.com/&#39;&gt;here&lt;/a&gt;

I also tried combining this in my translation file:

"test_html_placeholder" : "click %shere%s",

With this in my HTML:

<%= __('test_html_placeholder', '<a rel='nofollow' href="http://stackoverflow.com">', '</a>') %>

But got similar results.

The only thing I can get to work is this clumsiness:

"test_html_pre" : "click ",
"test_html_link" : "here",
"test_html_post" : ".",

with this:

<%= __('test_html_pre') %><a rel='nofollow' href="http://stackoverflow.com"><%= __('test_html_link') %></a><%= __('test_html_post') %>

But it's so cumbersome as to be almost not worth doing, and moreover the word order in some languages would force me to put some empty strings in my translation files, which i18n-node doesn't seem to like as it spits out the key (attribute) name when it encounters an empty string.

I also tried using "\" as an escape character in front of the symbols, but I got an invalid JSON error when I lifted sails (restarted the server).

Any ideas, workarounds? I'm using sails.js, it wasn't my decision but I'm stuck with it and it comes with i18n-node. It's kind of late in the day on this project to consider using another library, but not completely out of the question.


Source: (StackOverflow)

Where should I store localized dynamic data - node.js

I have a node.js express 4 web-app I need to translate and display in several languages. I'm learning to use i18n-node package and came to the following -
Assume a simple title in a view template (I'm using handlebars):

"Hey there {Londoners}! how about going to {Paris} next spring, meet some {parisians} and learn how to cook {parisian} desserts?"

You get the picture... londoners, paris, parisian/s all are dynamic data... I can save the full sentence in a locale json with simple key:value pairs as long as it is static. I can even inject numbers dates and dynamic data into those sentences. but where will I store the dynamic data translation? What is consider to be the best practice in these situations? save this localized data in the DB? in other json locale files? Cheers Ajar


Source: (StackOverflow)

Express and i18n default locale not working

Im trying to set up i18n-node and Expressjs. My conf is this:

// i18n Configuration =========================================================

var i18nconf = {
   locales        : ["en", "es"],
   cookie         : "locale",
   defaultLocale : "en",
   directory      : path.join(__dirname, "lang"),
   indent : "  "
};

i18n.configure(i18nconf);
app.use(i18n.init);

There are 2 locales on lang/ folder en.json and es.json, I can switch between them no problem but express always load es.json as default, somehow is ignoring defaultLocale: 'en' in the conf.

any ideas?

Thanks in advance!


Source: (StackOverflow)

i18n-node with handlebar template in express 4.0

after I register the hepler

var i18n = require('i18n');
i18n.configure({
locales: ['en', 'cn'],
cookie: 'locale',
directory: "" + __dirname + "/config/lang",
defaultLocale: 'en'
});
app.use(i18n.init);
// register hbs helpers in res.locals' context which provides this.locale
hbs.registerHelper('__', function () {
return i18n.__.apply(this, arguments);
});
hbs.registerHelper('__n', function () {
return i18n.__n.apply(this, arguments);
});

when i use each loop in handlebar template, it doesn't work

{{#each lang}}
       <li><a rel='nofollow' href="#">{{this}}</a></li>
{{/each}}

The json file is like

     {    
       "lang"     : [
                      "English",
                      "中文",
                      "Espalul"
                    ],
      }

Further quesetion: how to create a button to toggle to load different language.json?


Source: (StackOverflow)

How to localize a node.js app with different word positioning per locale mixing static & dynamic localized text?

I have a node.js express 4 web-app I need to translate and display in several languages. I'm learning to use i18n-node package in handlebars. Some of the localized text data comes from the db (dynamic data) and some comes from a static text json file using i18n-node. As a reference point i'll use the following sentence: "Hey there {Londoners}! how about going to {Paris} on your next spring vacation starting on {March 29}, meet some {parisians} and learn how to cook {parisian} desserts?" My question is: how do you deal with different grammar in various languages that will change the order of the words both static words and dynamic injected words?


Source: (StackOverflow)

How can get a non-trivial i18n translation to work using Sails.js?

Using Sails.js version 0.10.x and trying to get the i18n stuff to work.

I have the following in my config/locales/en.json

{
  "countries": {
    "au": {
      "name": "Australia",
      "fiatCurrency": "AUD",
      "subnationalDivisions": {
        "NSW": "New South Wales",
        "WA": "Western Australia",
        "VIC": "Victoria",
        "QLD": "Queensland",
        "TAS": "Tasmania",
        "SA": "South Australia",
        "NT": "Northern Territory",
        "ACT": "Australian Capital Territory"
      }
    }
  }
}

My config/i18n.js file looks like

module.exports.i18n = {

  // Which locales are supported?
  locales: ['en', 'es', 'fr', 'de'],

  objectNotation: true

};

In my controller I am trying to retrieve the correct subnationalDivision name via

res.i18n("countries." + country + ".subnationalDivisions." + state)

but that just gives me "countries.au.subnationalDivisions.ACT", not "Australian Capital Territory"

I've check with a trivial example:

Given en.json file containing { "bingo" : "sparky" }, res.i18n("bingo") outputs "sparky"

But examples using objectNotation don't work despite the instructions in the i18n-node documentation.

How should I get this to work?


Source: (StackOverflow)

use i18n-node in server-side express application

I can't figure out how I should use i18n-node module inside my application.

Within views, for static texts, it's easy, it works perfectly but...

Here is my problem :

Sometimes I have to set some error messages or something else, e.g :

req.flash('message', __('Unknown user %s', login));

Then I'll have to send this message to my views, e.g :

res.render('myview', {message: req.flash('message')});

But first, my message "Unknown user %s" will only be set in the default language json file, and then even if I put "Unknown user %s": "Something in the client language" in the client language json file, it will still display "Unknown user myUserLogin".

Does someone have a good working example to share ?

Edit: And because, there is a variable in the translated string, I can't just do that :

res.render('myview', {message: __(req.flash('message'))});

because it will set "Unknown user myUserLogin" in the client language json file, instead of "Unknown user %s"...


Source: (StackOverflow)