Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Do not set default locale when a request is given as an argument #30

Merged
merged 3 commits into from

4 participants

@dlongley

The current setLocale() call will set the "defaultLocale" variable even when a request is given as an argument; I believe this is a bug.

If "defaultLocale" is set when a request is given (the 2-argument form of setLocale), then there is an unfortunate side-effect where each subsequent request is treated as if it used the language from the previous one. For example, if the default locale is configured as "de" and a request with "Accept-Language: en" is sent to the server, then any subsequent request from any client that does not have an "Accept-Language" header will be assigned the language "en" instead of "de". Any request that sends an "Accept-Language" header will change the default locale from what was originally configured.

When a request is given as an argument to setLocale, only the locale on the request should be set. Only if no request is given should setLocale modify "defaultLocale".

This pull request also adds a couple of tests to ensure passing a request does not modify the default locale.

@mashpie
Owner

good point... but changes behavior and thus might break some implementations. let's rethink this a bit

@dlongley

Well, an alternative to this that might be a little less heavy-handed would be to just fix the internal handling of requests. So when guessLanguage() is called on a request, it doesn't call setLocale(), but duplicates what it does just for the request.

However, I don't think that anyone really wants their application's site-wide default locale to be changed every time a new request with a different "Accept-Language" comes in. I would imagine that people either haven't noticed that this is happening with their applications because their clients are all sending "Accept-Language" and don't see the side-effect themselves, or they did notice and are having to reset the default locale before every incoming request.

Neither of these is the correct way to solve the problem, IMO. I wouldn't expect many applications to be depending on this behavior.

@mashpie
Owner
msporny added some commits
@msporny

Added a feature to turn off automatic locale file updating. This became an issue for us when we were pushing our application to production. In production mode, we package up the software and install it in a read-only location on the file system. We needed a way of disabling node-i18n's behavior of automatically trying to write i18n changes. The changes include:

  • Addition of a configuration parameter called updateFiles that defaults to true.
  • Updated the documentation to show people how to turn off automatic locale file updates.
@mashpie mashpie merged commit 4826b40 into mashpie:master
@twoi

Any plans on creating a new version that includes the updateFiles feature?

In the meantime, I'll go the non-npm way and get the version from git.

@mashpie
Owner
@mashpie mashpie was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2012
  1. @dlongley
Commits on Aug 8, 2012
  1. @msporny
  2. @msporny

    Update README.md

    msporny authored
    Fixed minor spaces-to-tabs formatting issue in README.md
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 5 deletions.
  1. +10 −2 README.md
  2. +18 −3 i18n.js
  3. +2 −0  test/i18n.test.js
View
12 README.md
@@ -164,7 +164,15 @@ that file can be edited or just uploaded to [webtranslateit](http://docs.webtran
},
"tree": "Baum"
}
-
+
+to turn off automatic locale file updates:
+
+ // turn off locale file updating in production mode
+ i18n.configure({
+ // disable locale file updates
+ updateFiles: false
+ });
+
## Changelog
* 0.3.5: fixed some issues, prepared refactoring, prepared publishing to npm finally
@@ -174,4 +182,4 @@ that file can be edited or just uploaded to [webtranslateit](http://docs.webtran
* 0.3.0: added configure and init with express support (calling guessLanguage() via 'accept-language')
* 0.2.0: added plurals
* 0.1.0: added tests
-* 0.0.1: start
+* 0.0.1: start
View
21 i18n.js
@@ -13,6 +13,7 @@ var vsprintf = require('sprintf').vsprintf,
path = require('path'),
locales = {},
defaultLocale = 'en',
+ updateFiles = true,
cookiename = null,
debug = false,
verbose = false,
@@ -44,6 +45,11 @@ i18n.configure = function (opt) {
directory = './locales';
}
+ // write new locale information to disk
+ if (typeof opt.updateFiles === 'boolean') {
+ updateFiles = opt.updateFiles
+ }
+
// where to store json files
if (typeof opt.extension === 'string') {
extension = opt.extension;
@@ -110,7 +116,7 @@ i18n.__n = function () {
// setLocale('en') or like
// setLocale(req, 'en')
i18n.setLocale = function (arg1, arg2) {
- var request = {},
+ var request = undefined,
target_locale = arg1;
if (arg2 && locales[arg2]) {
@@ -119,8 +125,12 @@ i18n.setLocale = function (arg1, arg2) {
}
if (locales[target_locale]) {
- request.locale = target_locale;
- defaultLocale = target_locale;
+ if (request === undefined) {
+ defaultLocale = target_locale;
+ }
+ else {
+ request.locale = target_locale;
+ }
}
return i18n.getLocale(request);
};
@@ -246,6 +256,11 @@ function read(locale) {
// try writing a file in a created directory
function write(locale) {
+ // don't write new locale information to disk if updateFiles isn't true
+ if(!updateFiles) {
+ return;
+ }
+
// creating directory if necessary
try {
var stats = fs.lstatSync(directory);
View
2  test/i18n.test.js
@@ -20,6 +20,8 @@ module.exports = {
assert.equal('en', i18n.getLocale(), 'should return default setting');
assert.equal('de', i18n.setLocale('de'), 'should return the new setting');
assert.equal('de', i18n.getLocale(), 'should return the new setting');
+ assert.equal('en', i18n.setLocale({}, 'en'), 'should return the request setting');
+ assert.equal('de', i18n.getLocale(), 'should return the previous default setting');
},
'check singular': function () {
Something went wrong with that request. Please try again.