Permalink
Browse files

swapped out console.log using npm:debug, closed #22

  • Loading branch information...
1 parent 4543079 commit 2408443ab43156813df81a1373cad5d2fb60703b @mashpie committed Mar 16, 2013
Showing with 56 additions and 20 deletions.
  1. +19 −4 README.md
  2. +34 −13 i18n.js
  3. +3 −2 package.json
  4. +0 −1 test/i18n.configure.js
View
@@ -61,9 +61,6 @@ Minimal example, just setup two locales
// setting extension of json files - defaults to '.json' (you might want to set this to '.js' according to webtranslateit)
extension: '.js',
-
- // enabled some debug output - defaults to false
- debug: true
});
## Basic global use
@@ -168,7 +165,7 @@ Will get modular support for different storage engines, currently just json file
### json file
-the above will automatically generate a `en.js` by default inside `./locales/` which looks like
+the above will automatically generate a `en.json` by default inside `./locales/` which looks like
{
"Hello": "Hello",
@@ -208,6 +205,24 @@ that file can be edited or just uploaded to [webtranslateit](http://docs.webtran
"tree": "Baum"
}
+## Logging & Debugging
+
+Logging any kind of output is moved to [debug](https://github.com/visionmedia/debug) module. To let i18n output anything run your app with `DEBUG` env set like so:
+
+ $ DEBUG=i18n:* node app.js
+
+i18n exposes three log-levels:
+
+* i18n:debug
+* i18n:warn
+* i18n:error
+
+if you only want to get errors and warnings reported start your node server like so:
+
+ $ DEBUG=i18n:warn,i18n:error node app.js
+
+Combine those settings with you existing application if any of you other modules or libs also uses __debug__
+
## Changelog
* 0.3.9: express.js usage, named api, jscoverage + more test, refactored configure, closed: #51, #20, #16, #49
View
47 i18n.js
@@ -1,4 +1,3 @@
-/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, indent: 2 */
/**
* @author Created by Marcus Spiegel <marcus.spiegel@gmail.com> on 2011-03-25.
* @link https://github.com/mashpie/i18n-node
@@ -12,6 +11,9 @@ var vsprintf = require('sprintf').vsprintf,
fs = require('fs'),
url = require('url'),
path = require('path'),
+ debug = require('debug')('i18n:debug'),
+ warn = require('debug')('i18n:warn'),
+ error = require('debug')('i18n:error'),
locales = {},
defaultLocale, updateFiles, cookiename, debug, extension, directory;
@@ -45,9 +47,6 @@ i18n.configure = function i18nConfigure(opt) {
// setting defaultLocale
defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';
- // enabled some debug output
- debug = (typeof opt.debug === 'boolean') ? opt.debug : false;
-
// implicitly read all locales
if (typeof opt.locales === 'object') {
opt.locales.forEach(function (l) {
@@ -151,7 +150,10 @@ i18n.overrideLocaleFromQuery = function (req) {
// ===================
// = private methods =
// ===================
-// guess language setting based on http headers
+/**
+ * guess language setting based on http headers
+ */
+
function guessLanguage(request) {
if (typeof request === 'object') {
var language_header = request.headers['accept-language'],
@@ -195,7 +197,10 @@ function guessLanguage(request) {
}
}
-// searches for locale in given object
+/**
+ * searches for locale in given object
+ */
+
function getLocaleFromObject(obj) {
var locale;
if (obj && obj.scope) {
@@ -207,7 +212,10 @@ function getLocaleFromObject(obj) {
return locale;
}
-// read locale file, translate a msg and write to fs if new
+/**
+ * read locale file, translate a msg and write to fs if new
+ */
+
function translate(locale, singular, plural) {
if (locale === undefined) {
logWarn("WARN: No locale found - check the context of the call to __(). Using " + defaultLocale + " as current locale");
@@ -235,7 +243,10 @@ function translate(locale, singular, plural) {
return locales[locale][singular];
}
-// try reading a file
+/**
+ * try reading a file
+ */
+
function read(locale) {
var localeFile = {},
file = getStorageFilePath(locale);
@@ -257,7 +268,10 @@ function read(locale) {
}
}
-// try writing a file in a created directory
+/**
+ * try writing a file in a created directory
+ */
+
function write(locale) {
var stats, target, tmp;
@@ -295,7 +309,10 @@ function write(locale) {
}
}
-// basic normalization of filepath
+/**
+ * basic normalization of filepath
+ */
+
function getStorageFilePath(locale) {
// changed API to use .json as default, #16
var ext = extension || '.json',
@@ -314,14 +331,18 @@ function getStorageFilePath(locale) {
return filepath;
}
+/**
+ * Logging proxies
+ */
+
function logDebug(msg) {
- if (debug) console.log(msg);
+ debug(msg);
}
function logWarn(msg) {
- if (debug) console.warn(msg);
+ warn(msg);
}
function logError(msg) {
- if (debug) console.error(msg);
+ error(msg);
}
View
@@ -13,11 +13,12 @@
"lib": "."
},
"dependencies": {
- "sprintf": ">=0.1.1"
+ "sprintf": ">=0.1.1",
+ "debug": "*"
},
"devDependencies": {
"mocha": ">=1.8.1",
- "should": ">=1.2.1"
+ "should": ">=1.2.1",
},
"engines": {
"node": ">=0.4.0"
View
@@ -10,7 +10,6 @@ describe('Module Config', function () {
beforeEach(function () {
i18n.configure({
- // debug: true,
locales: ['en', 'de'],
register: testScope,
directory: './customlocales',

0 comments on commit 2408443

Please sign in to comment.