Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
21 changes: 18 additions & 3 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var vsprintf = require('sprintf').vsprintf,
path = require('path'),
locales = {},
defaultLocale = 'en',
updateFiles = true,
cookiename = null,
debug = false,
verbose = false,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand All @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down