Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #30 from digitalbazaar/master

Do not set default locale when a request is given as an argument
  • Loading branch information...
commit 4826b40a01f1364151f20d6fc6f16a382772dbc6 2 parents 5df241c + a42fc51
@mashpie authored
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 () {
Please sign in to comment.
Something went wrong with that request. Please try again.