florian / x18n

A sane JavaScript internationalisation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

server-side usage

tracker1 opened this issue · comments

would be nice to see server-side examples of usage... in that rendering from one user in one language won't clobber the rendering for another user in another language... there's no examples that create a separate rendering instance for a given locale.

would be nice to see server-side examples of usage

Server-side usage is a good point, it's on my list! :)

in that rendering from one user in one language won't clobber the rendering for another user in another language... there's no examples that create a separate rendering instance for a given locale.

Node is single threaded so just using x18n.set, to change the language everytime you want to render the page for a new user, wouldn't be a problem.
The set method is also really lightweight (it only changes how t will retrieve translations, but it won't actually copy any translation sets or stuff like that), so there's also nothing to worry about from a performance standpoint.

But depending on how you are using server-side rendering, that would require several changes to the focus to "set" ... Would be better if one could have a rendering .t instance that's set to a specific language, so that it can be passed in context (think react modules) to children via the context.

Without that, one would have to call .set(context.locale) everywhere you use a template, which would severely inhibit the value this module provides.

I'm not quite convinced yet, can give you give an example of a node setup, e.g. a template system, where I can pass x18n into and it's not copied by reference?

If you require x18n several times, then you'll always get a reference to the same object. So if you set the language once, it's changed everywhere.

It's the same if you pass x18n with React props. You wouldn't really need to do that though, you could just require x18n in every React component that needs it, or just use react-x18n which also helps you with other things regarding React.