Giter Club home page Giter Club logo

Comments (9)

mashpie avatar mashpie commented on July 16, 2024

Could you test if this is fixed in current Release? Req.locale now gets set in i18n.init too.

from i18n-node.

nicosommi avatar nicosommi commented on July 16, 2024

As I'm using it, still does not work.
I removed the manual response method registration for changes included in this 0.4.0 version.

For getting it to work as I excpected I need to comment the line 138 of the i18n.js:

request.locale = target_locale;

In my Jade templates I'm translating like this (I think that registered functions on responses made this method request-scoped):

#{__('locale.entry')}

On the other hand I've also noticed that in this version I need to configure i18n with './locales' directory, because it doesn't work by default.

from i18n-node.

mashpie avatar mashpie commented on July 16, 2024

this is wired. could you gist a sample of your complete implementation? There must be a reason why target_locale can't be filled as intended.

from i18n-node.

nicosommi avatar nicosommi commented on July 16, 2024

Marcus, I've prepared a little example but I cannot attach zip files here so there is a gist here with main files (created with express -s command and with i18n added): https://gist.github.com/nicosommi/a513bf2d1feea10794bd

The problem is that jade template is taking the locale from the response. In the example is registered global and changed globally, and accessing it by i18n.__ method uses globally setted locale, but templates still uses response locale that is guessed from the request. Maybe listening for locale changes and updating the response registered __ method can solve this use case.
For getting it to work as I did, you need to get your browser in spanish (es), and you will see that the template function is using 'es' even when i18n.__ is using 'en'.

Besides of that, in lines 18 and 35 of app.js I've commented other two minor issues.

from i18n-node.

mashpie avatar mashpie commented on July 16, 2024

Thanks I'll review your example shortly. Please note that you shouldn't register globally when using express & template. The intended use is for cli skripting only.

Let me track down your issue via gist

from i18n-node.

nicosommi avatar nicosommi commented on July 16, 2024

Yes you are correct about CLI script. It's specified in the README. What I need to do is to let the user choose the application language despite of the browser's language.

from i18n-node.

mashpie avatar mashpie commented on July 16, 2024

this should work in your indes.js:

exports.index = function(req, res){
  res.setLocale('en');
  res.render('index', {globaltitle: res.__('title')});
}

from i18n-node.

nicosommi avatar nicosommi commented on July 16, 2024

Marcus thanks for that tip. Combining what you say and adding cookie support I can reach my goal.

The change language methods looks finally like this (supossing that is on the index function):

exports.index = function(req, res){
  res.setLocale('en'); //for current request
  res.cookie('userlang', 'en'); //for future request of that user
  res.render('index', {globaltitle: res.__('title')});
};

And the configuration must have cookie:

i18n.configure({
    locales: ["en", "es"],
    defaultLocale: "en",
    directory: "./locales",
    extension: "js",
    cookie: 'userlang',
    debug: true
});

...
app.use(express.cookieParser());
...

from i18n-node.

mashpie avatar mashpie commented on July 16, 2024

Excellent! Glad to have helped you - I'll collect some more examples to get that clear for future docs :)

from i18n-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.