Navigation Menu

Skip to content

Commit

Permalink
factored out debugging to proxy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Mar 16, 2013
1 parent 58d7e9e commit 4543079
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
47 changes: 22 additions & 25 deletions i18n.js
Expand Up @@ -143,17 +143,14 @@ 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);
}
logDebug("Overriding locale from query: " + urlObj.query.locale);
i18n.setLocale(req, urlObj.query.locale.toLowerCase());
}
};

// ===================
// = private methods =
// ===================

// guess language setting based on http headers
function guessLanguage(request) {
if (typeof request === 'object') {
Expand Down Expand Up @@ -213,9 +210,7 @@ function getLocaleFromObject(obj) {
// 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 $__. Using " + defaultLocale + " (set by request) as current locale");
}
logWarn("WARN: No locale found - check the context of the call to __(). Using " + defaultLocale + " as current locale");
locale = defaultLocale;
}

Expand Down Expand Up @@ -245,23 +240,19 @@ function read(locale) {
var localeFile = {},
file = getStorageFilePath(locale);
try {
if (debug) {
console.log('read ' + file + ' for locale: ' + locale);
}
logDebug('read ' + file + ' for locale: ' + locale);
localeFile = fs.readFileSync(file);
try {
// parsing filecontents to locales[locale]
locales[locale] = JSON.parse(localeFile);
} catch (parseError) {
console.error('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
logError('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
}
} catch (readError) {
// unable to read, so intialize that file
// locales[locale] are already set in memory, so no extra read required
// or locales[locale] are empty, which initializes an empty locale.json file
if (debug) {
console.log('initializing ' + file);
}
logDebug('initializing ' + file);
write(locale);
}
}
Expand All @@ -279,9 +270,7 @@ function write(locale) {
try {
stats = fs.lstatSync(directory);
} catch (e) {
if (debug) {
console.log('creating locales dir in: ' + directory);
}
logDebug('creating locales dir in: ' + directory);
fs.mkdirSync(directory, parseInt('755', 8));
}

Expand All @@ -299,10 +288,10 @@ function write(locale) {
if (stats.isFile()) {
fs.renameSync(tmp, target);
} else {
console.error('unable to write locales to file (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
logError('unable to write locales to file (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
}
} catch (e) {
console.error('unexpected error writing files (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
logError('unexpected error writing files (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
}
}

Expand All @@ -315,16 +304,24 @@ function getStorageFilePath(locale) {
// use .js as fallback if already existing
try {
if (fs.statSync(filepathJS)) {
if (debug) {
console.log('using existing file ' + filepathJS);
}
logDebug('using existing file ' + filepathJS);
extension = '.js';
return filepathJS;
}
} catch (e) {
if (debug) {
console.log('will write to ' + filepath);
}
logDebug('will write to ' + filepath);
}
return filepath;
}

function logDebug(msg) {
if (debug) console.log(msg);
}

function logWarn(msg) {
if (debug) console.warn(msg);
}

function logError(msg) {
if (debug) console.error(msg);
}
4 changes: 3 additions & 1 deletion test/i18n.configure.js
Expand Up @@ -10,10 +10,11 @@ describe('Module Config', function () {

beforeEach(function () {
i18n.configure({
// debug: true,
locales: ['en', 'de'],
register: testScope,
directory: './customlocales',
extension: '.customextension',
extension: '.customextension'
});
testScope.__('Hello');
});
Expand All @@ -26,6 +27,7 @@ describe('Module Config', function () {
fs.unlinkSync('./customlocales/en.customextension');
fs.rmdirSync('./customlocales');
}

});

it('should be possible to setup a custom directory', function () {
Expand Down
1 change: 1 addition & 0 deletions test/i18n.defaults.js
Expand Up @@ -25,6 +25,7 @@ describe('Module Defaults', function () {
fs.unlinkSync('./defaultlocales/en.json');
fs.rmdirSync('./defaultlocales');
}

});

it('should be possible to setup a custom directory', function () {
Expand Down

0 comments on commit 4543079

Please sign in to comment.