3

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"...

Oliboy50
  • 2,661
  • 3
  • 27
  • 36
  • I'm a bit confused when you mentioned about the output of the client language file being `"Uknown user myUserLogin"`. I'm going to assume that you are saying the output is still the default language instead of the client's language. What happens when you override the locale? ie: `__({phrase: 'Unknown user {{user}}, locale: 'fr'}, { user: 'Marcus' });` – iwatakeshi Oct 24 '14 at 19:26

1 Answers1

1

I know this question is kind of old, but I ran into the same issue and found a solution.

Since your using the flash method from the req object, you should also use the __ method available in the same object:

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

This way it's gonna be translated using the current locale of the request.

George Marques
  • 820
  • 7
  • 21