balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.85k stars 1.95k forks source link

Sails 1.0 internationalization issue #4343

Open vkkmehra opened 6 years ago

vkkmehra commented 6 years ago

Hello,

I have an issue with sails internationalization. I have created a simple example for that. I want to translate a key to a different language other than default language in the controller. My default language is en but in international controller I want to translate welcome in french. I referred few articles but got no success in old sails documentation there was a way

sails.__({ phrase: 'Welcome', locale: 'fr' });

to translate a key on the fly but it is running into error.

I am using

Sails version 1.0.0-45 Node version 8.9.1 npm version 5.5.1 DB Adaptor : sails-postgresql Operating System : Windows 7, 8 as well as Ubuntu

The example project is here on git hub https://github.com/vkkmehra/international

Thanks

sailsbot commented 6 years ago

Hi @vkkmehra! It looks like you may have removed some required elements from the initial comment template, without which I can't verify that this post meets our contribution guidelines. To re-open this issue, please copy the template from here, paste it at the beginning of your initial comment, and follow the instructions in the text. Then post a new comment (e.g. "ok, fixed!") so that I know to go back and check.

Sorry to be a hassle, but following these instructions ensures that we can help you in the best way possible and keep the Sails project running smoothly.

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact inquiries@sailsjs.com

mikermcneil commented 6 years ago

Thanks Vivek- especially for the repo demonstrating exactly what you mean!

I've updated the 1.0 docs (1, 2) to more clearly document intended usage.

The following still works for me in Sails v1.0:

image

I was able to recreate the issue you're seeing when using the second argument-- but I then realized why. As of 1.0, we switched to a different, lighter-weight i18n module in the default i18n support provided out of the box. That means some functionality that worked in v0.12 doesn't work anymore in v1.0. Specifically, in this case, other arguments to sails.__ are now restricted to util.format()-esque (aka printf-style) usage.

If the original functionality is crucial to you, you can get it back by installing a hook we built for this contingency (see https://github.com/balderdashy/sails-docs/blob/af687db90049661cb0f9a25c586843cd1c4ba34c/upgrading/To1.0.md#i18n)

But from what I'm gathering, you might actually be better served by doing something like this in your view:

<%= __('Hello') %>

That'll work for most cases, assuming you want Sails to automatically use the right locale based on the requesting user's browser language settings.

But if you want more control (e.g. a "language dropdown" in the top right corner of your site), then you can also override this. In the corresponding controller action that serves that view:

var locale = 'es';//« get this however you want, from database, session, etc.
this.req.setLocale('es');

Hopefully that gets it sorted!

krewx commented 6 years ago

Sails version 1.0.0-47 Node version 8.9.4 npm version 5.6.0 DB Adaptor : sails-disk Operating System : Mac Os 10.12.6

and finally...

Ideally, this involves creating a new repo that demonstrates the problem (see instructions at http://bit.ly/sails-issue-repro). Even though your issue may seem so simple to reproduce that a new repo is unnecessary, you'd be surprised how many solutions present themselves when you start from sails new and attempt to recreate your issue from scratch in a new app. This ensures that the real issue isn't in your user code (a forgotten policy file, perhaps?) or in a third-party module. If you're absolutely convinced that a new repo is unnecessary, provide clear, concise and specific steps to reproduce the problem in your post (not "create a model then do blueprint create").

Hello i was using the documentation and the example shown by @mikermcneil to set the locale to es in my action, my default locale is en, and then i call sails. and it does not use the locale from es.json. How do i know? i changed the default locale to es, use sails. and it changes perfectly the string but if i use this.req.setLocale('en'); and call again the sails.__ it still uses the es locale.

code that illustrates the issue.

this.req.getLocale() => "es" console.log(sails.__("Welcome")); => "Bienvenido" this.req.setLocale('en'); this.req.getLocale() => "en" console.log(sails.__("Welcome")); => "Bienvenido"

shweshi commented 6 years ago

@mikermcneil I am facing the same issue as @krewx has mentioned above. I think its a bug. Kindly provide a solution for this.

CyBot commented 5 years ago

sails. uses a separate i18n instance. However, there is no req. / req.i18n in a controller - but you can use res.locals.__ (which uses the locale set by req.setLocale)

dpnick commented 3 years ago

@CyBot thank you so much ! That's should be documented !