Comments (9)
Could you test if this is fixed in current Release? Req.locale now gets set in i18n.init too.
from i18n-node.
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.
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.
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.
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.
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.
this should work in your indes.js:
exports.index = function(req, res){
res.setLocale('en');
res.render('index', {globaltitle: res.__('title')});
}
from i18n-node.
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.
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)
- how to pass a default translation if a phrase isnt existed in the en.json file HOT 3
- Locales are case sensitive HOT 3
- __h has strange behavior when he doesn't have the key HOT 1
- [Discussion] Contrast reports path traversal vulnerability HOT 4
- [Discussion] Allow a parser option to customize storage format HOT 2
- Maximum call stack size exceeded with some settings HOT 4
- Get string from locale file without substitution HOT 4
- passing a zero number value (0) incorrectly returns the full json string, brackets and all, without injecting the replacement value HOT 3
- When i18n is packaged into exe cannot be used, i18n directory cannot find the files under the directory path. HOT 3
- yml parser TypeScript and locale issues
- [Bug] translation API is returning `undefined` when provided locale is `undefined` or `null`
- There are no 0.15.1 release and tag.
- There are no 0.15.1 release and tag. HOT 1
- i18n ignores en.json language file HOT 1
- Error: Cannot find module Cyclic.sh sever HOT 1
- Problems with local files HOT 3
- ES Modules HOT 9
- feature request: support plurals with specific numbers HOT 5
- Readonly environment HOT 2
- `__n` not working as per documentation in the description
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from i18n-node.