Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using i18next-node with moment.js #51

Closed
remoe opened this issue Dec 15, 2012 · 2 comments
Closed

Using i18next-node with moment.js #51

remoe opened this issue Dec 15, 2012 · 2 comments

Comments

@remoe
Copy link

remoe commented Dec 15, 2012

Does anyone know a way to sync the language selection between i18next and moment.js? I mean something like:

app.locals({
  format_date: function(val) {
    moment.lang(i18next.lng());
    return moment(val);
  }
});

It should be automatic switch the language of moment.js when i18n switch to a new language.

Thanks

@jamuhl
Copy link
Member

jamuhl commented Dec 17, 2012

on clientside this is rather easy. If you don't pass language to init the detected language fill be detected and avaiable when the init call back is called: i18next.lng().

on the serverside the current language is set per request, so grab it from the request object req.lng.

i'm not sure but i think moment itself is a singleton - so setting it's language should be done just before using it (if you got some async stuff inside your route handling - else another request could overwrite the current set language in moment.js resulting in wrong localization).

you might add your own middleware after i18next:

// Configuration
app.configure(function() {
    app.use(i18n.handle);
    app.use(function(req, res, next) {
        res.locals.format_date: function(val) {
           moment.lang(req.lng);
           return moment(val);
        }
    });
});

@remoe
Copy link
Author

remoe commented Dec 17, 2012

Thanks for this hint. I use the following:

app.use(i18next.handle);

app.use(function(req,res,next){
  res.locals.format = {
    date: function(val) {
      moment.lang(req.lng);
      return moment(val).format("L LT");
    }
  };
  next();
});

@remoe remoe closed this as completed Dec 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants