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

Add extension option, swap out console.log for debug #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 18 additions & 20 deletions i18n.js
Expand Up @@ -12,13 +12,14 @@ var vsprintf = require('sprintf').vsprintf,
fs = require('fs'),
url = require('url'),
path = require('path'),
debug = require('debug')('i18n'),

// defaults
locales = {},
defaultLocale = 'en',
cookiename = null,
debug = false;
directory = './locales';
directory = './locales',
extension = '.json';

// public exports
var i18n = exports;
Expand All @@ -37,19 +38,16 @@ i18n.configure = function(opt) {
if (typeof opt.cookie === 'string') {
cookiename = opt.cookie;
}
// where to store json files

// where to store json files
if (typeof opt.directory === 'string') {
directory = opt.directory;
}else{
directory = './locales';
}

// enabled some debug output
if (opt.debug) {
debug = opt.debug;
}

}

if (typeof opt.extension === 'string') {
extension = opt.extension;
}

// implicitly read all locales
if (typeof opt.locales === 'object') {
opt.locales.forEach(function(l) {
Expand Down Expand Up @@ -134,7 +132,7 @@ i18n.overrideLocaleFromQuery = function(req) {
}
var urlObj = url.parse(req.url, true);
if (urlObj.query.locale) {
if (debug) console.log("Overriding locale from query: " + urlObj.query.locale);
debug('Overriding locale from query: ' + urlObj.query.locale);
i18n.setLocale(req, urlObj.query.locale.toLowerCase());
}
}
Expand Down Expand Up @@ -188,7 +186,7 @@ function guessLanguage(request) {
// read locale file, translate a msg and write to fs if new
function translate(locale, singular, plural) {
if (locale === undefined) {
if (debug) console.warn("WARN: No locale found - check the context of the call to $__?");
debug('No locale found - check the context of the call to $__?');
locale = defaultLocale;
}

Expand Down Expand Up @@ -220,17 +218,17 @@ function read(locale) {
// try to read from FS
try {
localeFile = fs.readFileSync(file);
console.log('read ' + file + ' for locale: ' + locale);
debug('read ' + file + ' for locale: ' + locale);
} catch(e) {
console.log('initializing ' + file);
debug('initializing ' + file);
write(locale);
}

// try to parse to JSON
try {
locales[locale] = JSON.parse(localeFile);
} catch(e) {
console.error('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
debug('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ');
}
}

Expand All @@ -239,7 +237,7 @@ function write(locale) {
try {
stats = fs.lstatSync(directory);
} catch(e) {
if (debug) console.log('creating locales dir in: ' + directory);
debug('creating locales dir in: ' + directory);
fs.mkdirSync(directory, 0755);
}
var target = locate(locale),
Expand All @@ -260,5 +258,5 @@ function write(locale) {

// basic normalization of filepath
function locate(locale) {
return path.normalize(directory + '/' + locale + '.js');
return path.normalize(directory + '/' + locale + extension);
}
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -12,10 +12,11 @@
"directories": {
"lib": "."
},
"dependencies" : {
"dependencies" : {
"debug": "*",
"sprintf" : ">=0.1.1"
},
"engines": {
"node": ">=0.4.0"
}
}
}