Comparing version 0.1.9 to 0.1.10
246
gengo.js
@@ -1,8 +0,6 @@ | ||
/*jslint forin: true*/ | ||
/*jslint white: true*/ | ||
/*jshint immed: true*/ | ||
/*global module, $, console*/ | ||
/*jslint node: true, forin: true, jslint white: true*/ | ||
/*global console*/ | ||
/* | ||
* gengojs | ||
* version : 0.1.9 | ||
* version : 0.1.10 | ||
* author : Takeshi Iwana | ||
@@ -41,2 +39,45 @@ * https://github.com/iwatakeshi | ||
function isDefined(obj) { | ||
function isNull(val) { | ||
if (val === null) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function isUndefined(val) { | ||
if (val === undefined) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function isStringEmpty(val) { | ||
if (val === '' || val === '') { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
if (typeof obj === 'string') { | ||
if (!isStringEmpty(obj)) { | ||
if (obj !== 'null') { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
if (typeof obj === "undefined") { | ||
debug("In undefined!"); | ||
return !isUndefined(obj); | ||
} else { | ||
return !isNull(obj); | ||
} | ||
} | ||
/************************************ | ||
@@ -53,3 +94,3 @@ Constants & Variables | ||
hasModule = (typeof module !== 'undefined' && module.exports), | ||
VERSION = '0.1.8', | ||
VERSION = '0.1.10', | ||
//configuration with defaults set | ||
@@ -68,3 +109,5 @@ CONFIG = { | ||
viewAware: false, | ||
universe: false, | ||
views: { | ||
'*': 'gengo', | ||
'/': 'index' | ||
@@ -94,5 +137,14 @@ } | ||
}, | ||
LANG = { | ||
//key:locale, value: lang | ||
'ja': 'Japanese', | ||
'en': 'English', | ||
'en_US': 'English US' | ||
}, | ||
ROUTE, | ||
COOKIELOCALE, | ||
NUMERALPATH = 'numeral/languages/'; | ||
COOKIELOCALE = "", | ||
NUMERALPATH = 'numeral/languages/', | ||
CURRENTLOCALE = "", | ||
CURRENTLANG = "", | ||
UNIVERSE = {}; | ||
@@ -115,35 +167,35 @@ | ||
debug('fn: gengo, isDefault'); | ||
CURRENTLOCALE = CONFIG.default; | ||
CURRENTLANG = LANG[CURRENTLOCALE]; | ||
loadMoment(LOCALES.moment[CONFIG.default]); | ||
loadNumeral(LOCALES.numeral[CONFIG.default]); | ||
if (arg !== undefined) { | ||
debug(replace(input, arg), 'fn: gengo, Output with arg'); | ||
return replace(input, arg); | ||
} else { | ||
return input; | ||
} | ||
return hasArg(input, arg); | ||
} else { | ||
loadLocale(); | ||
if (CONFIG.viewAware) { | ||
if (router() !== undefined) { | ||
debug(router()[input], "fn: gengo, Output with viewAware"); | ||
debug('-----------------------------------'); | ||
if (arg !== undefined) { | ||
return replace(router()[input], arg); | ||
switch (loadLocale()) { | ||
case true: | ||
if (CONFIG.viewAware) { | ||
if (router()) { | ||
debug(router()[input], "fn: gengo, Output with viewAware"); | ||
if (router()[input] !== undefined) { | ||
return hasArg(router()[input], arg); | ||
} else if (UNIVERSE[input] !== undefined) { | ||
debug("HAS arg"); | ||
return hasArg(UNIVERSE[input], arg); | ||
} | ||
} | ||
} else { | ||
return router()[input]; | ||
debug(LOCALE[input], 'fn: gengo, Output'); | ||
if (LOCALE[input] !== undefined) { | ||
return hasArg(LOCALE[input], arg); | ||
} else if (UNIVERSE[input] !== undefined) { | ||
return hasArg(UNIVERSE[input], arg); | ||
} | ||
} | ||
} | ||
} else { | ||
debug(LOCALE[input], 'fn: gengo, Output'); | ||
debug('-----------------------------------'); | ||
if (arg !== undefined) { | ||
return replace(LOCALE[input], arg); | ||
} else { | ||
return LOCALE[input]; | ||
} | ||
break; | ||
case false: | ||
return hasArg(input, arg); | ||
} | ||
} | ||
@@ -161,7 +213,5 @@ }; | ||
ROUTE = req.path; | ||
//get the cookie local if it exists | ||
if (req.cookies.locale !== '' || req.cookies.locale !== undefined) { | ||
debug(req.cookies.locale, "fn: init, req.cookies.locale"); | ||
COOKIELOCALE = req.cookies.locale; | ||
COOKIELOCALE = req.cookies.locale; | ||
} | ||
debug(ROUTE, "fn: init, Route"); | ||
@@ -177,2 +227,10 @@ debug(req.headers['accept-language'], "fn: init, Accept-Language"); | ||
} | ||
if (moment) { | ||
debug("fn: init, moment is defined."); | ||
} | ||
if (numeral) { | ||
debug("fn: init, numeral is defined."); | ||
} | ||
setLangLocale(); | ||
res.locals[CONFIG.gengo] = gengo; | ||
@@ -189,2 +247,12 @@ res.locals[CONFIG.moment] = moment; | ||
gengo.getLocale = function() { | ||
return CURRENTLOCALE; | ||
}; | ||
gengo.getLanguage = function() { | ||
debug(CURRENTLANG, "fn: getLanguage, current language is"); | ||
return CURRENTLANG; | ||
}; | ||
/************************************ | ||
@@ -196,21 +264,38 @@ Private Functions | ||
//COOKIELOCALE has top priority if set | ||
if (COOKIELOCALE) { | ||
debug('fn: loadLocale, In COOKIELOCALE'); | ||
if (isDefined(COOKIELOCALE)) { | ||
debug(COOKIELOCALE, 'fn: loadLocale, In COOKIELOCALE'); | ||
LOCALE = require(CONFIG.localePath + LOCALES.gengo[COOKIELOCALE] + '.js'); | ||
loadMoment(LOCALES.moment[COOKIELOCALE]); | ||
loadNumeral(LOCALES.numeral[COOKIELOCALE]); | ||
//set the current locale | ||
setLangLocale(); | ||
return true; | ||
} else { | ||
debug('fn: loadLocale, In BESTMATCH'); | ||
LOCALE = require(CONFIG.localePath + LOCALES.gengo[BESTMATCH] + '.js'); | ||
loadMoment(LOCALES.moment[BESTMATCH]); | ||
loadNumeral(LOCALES.numeral[BESTMATCH]); | ||
if ((BESTMATCH === CONFIG.default) === false) { | ||
debug(BESTMATCH, 'fn: loadLocale, In BESTMATCH'); | ||
LOCALE = require(CONFIG.localePath + LOCALES.gengo[BESTMATCH] + '.js'); | ||
loadMoment(LOCALES.moment[BESTMATCH]); | ||
loadNumeral(LOCALES.numeral[BESTMATCH]); | ||
setLangLocale(); | ||
return true; | ||
} else { | ||
//fall back to default | ||
debug('fn: loadLocale, Falling back to default.'); | ||
LOCALE = undefined; | ||
setLangLocale(); | ||
loadMoment(LOCALES.moment[CONFIG.default]); | ||
loadNumeral(LOCALES.numeral[CONFIG.default]); | ||
debug("Here"); | ||
return false; | ||
} | ||
} | ||
if (LOCALE) { | ||
debug("fn: loadLocale, LOCALE loaded"); | ||
debug("fn: loadLocale, LOCALE loaded."); | ||
} else { | ||
debug("fn: loadLocale, Could not load LOCALE"); | ||
debug("fn: loadLocale, Could not load LOCALE."); | ||
} | ||
}; | ||
} | ||
@@ -240,3 +325,3 @@ function loadMoment(locale) { | ||
function debug(obj, msg) { | ||
if (CONFIG.debug === true) { | ||
if (CONFIG.debug) { | ||
if (msg) { | ||
@@ -248,3 +333,3 @@ console.log(msg + ': '); | ||
} | ||
}; | ||
} | ||
@@ -254,3 +339,3 @@ //check if COOKIELOCALe or best match is default | ||
//COOKIELOCALe has top priority if set | ||
if (COOKIELOCALE) { | ||
if (isDefined(COOKIELOCALE)) { | ||
//if COOKIELOCALE === default | ||
@@ -270,7 +355,19 @@ if (COOKIELOCALE === CONFIG.default) { | ||
} | ||
}; | ||
} | ||
function hasArg(input, arg) { | ||
debug('-----------------------------------'); | ||
debug(arg, 'arg'); | ||
if (arg) { | ||
debug(replace(input, arg), 'fn: gengo, Output with arg'); | ||
return replace(input, arg); | ||
} else { | ||
return input; | ||
} | ||
} | ||
//for viewAware | ||
function router() { | ||
//check if the route matches the views from config | ||
if (CONFIG.views[ROUTE] !== undefined) { | ||
if (CONFIG.views[ROUTE]) { | ||
/* | ||
@@ -285,6 +382,6 @@ { | ||
var locale = LOCALE[CONFIG.views[ROUTE]]; | ||
loadUniverse(); | ||
//check if locale is defined | ||
if (locale !== undefined) { | ||
debug(locale, "fn: router, Loaded locale with viewAware"); | ||
if (locale) { | ||
debug("fn: router, Loaded locale with viewAware"); | ||
return locale; | ||
@@ -297,4 +394,15 @@ } else { | ||
} | ||
}; | ||
} | ||
function loadUniverse() { | ||
if (CONFIG.universe) { | ||
UNIVERSE = LOCALE[CONFIG.views["*"]]; | ||
if (UNIVERSE) { | ||
debug(UNIVERSE, 'fn: loadUniverse, UNIVERSE loaded'); | ||
} else { | ||
debug("Could not load UNIVERSE"); | ||
} | ||
} | ||
} | ||
//sprintf function | ||
@@ -308,3 +416,27 @@ function replace(input, arg) { | ||
} | ||
}; | ||
} | ||
function setLangLocale() { | ||
if (isDefault()) { | ||
CURRENTLOCALE = CONFIG.default; | ||
CURRENTLANG = LANG[CURRENTLOCALE]; | ||
} else { | ||
if (isDefined(COOKIELOCALE)) { | ||
CURRENTLOCALE = LOCALES.gengo[COOKIELOCALE]; | ||
CURRENTLANG = LANG[CURRENTLOCALE]; | ||
} else { | ||
if ((BESTMATCH === CONFIG.default) === false) { | ||
CURRENTLOCALE = LOCALES.gengo[BESTMATCH]; | ||
CURRENTLANG = LANG[CURRENTLOCALE]; | ||
} else { | ||
CURRENTLOCALE = CONFIG.default; | ||
CURRENTLANG = LANG[CURRENTLOCALE]; | ||
} | ||
} | ||
} | ||
debug(CURRENTLOCALE, "fn: loadLocale, current locale is"); | ||
debug(CURRENTLANG, "fn: loadLocale, current language is"); | ||
} | ||
/************************************ | ||
@@ -311,0 +443,0 @@ Exposing Gengo |
{ | ||
"name": "gengojs", | ||
"version": "0.1.9", | ||
"description": "An uber basic and simple i18n library for Express 4", | ||
"main": "gengo.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"app-root-path": "^0.1.0", | ||
"locale": "0.0.17", | ||
"moment": "^2.8.2", | ||
"numeral": "^1.5.3", | ||
"sprintf-js": "0.0.7" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iwatakeshi/gengojs.git" | ||
}, | ||
"keywords": [ | ||
"i18n", | ||
"translate", | ||
"gengo" | ||
], | ||
"author": "iwatakeshi", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/iwatakeshi/gengojs/issues" | ||
}, | ||
"homepage": "https://github.com/iwatakeshi/gengojs" | ||
"name": "gengojs", | ||
"version": "0.1.10", | ||
"description": "An uber basic and simple i18n library for Express 4", | ||
"main": "gengo.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"app-root-path": "^0.1.0", | ||
"locale": "0.0.17", | ||
"moment": "^2.8.2", | ||
"numeral": "^1.5.3", | ||
"sprintf-js": "0.0.7" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iwatakeshi/gengojs.git" | ||
}, | ||
"keywords": [ | ||
"i18n", | ||
"translate", | ||
"gengo" | ||
], | ||
"author": "iwatakeshi", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/iwatakeshi/gengojs/issues" | ||
}, | ||
"homepage": "https://github.com/iwatakeshi/gengojs" | ||
} |
Gengo.js | ||
======== | ||
##Help needed!! | ||
First, I want to thank those who downloaded and tried gengo. gengo has a lot of room to grow but is really limited without your help. gengo now has a new site and is available at [gengojs.com](http://www.gengojs.com), but needs your help to improve it | ||
in means of translations and of course gengo itself. So, please visit the Github page and fork away gengo and the site! | ||
##Change Log | ||
Quite sinful, but starting the log from 0.1.10 | ||
**0.1.10** | ||
* Fixed issues with boolean values. Using isDefined should help figuring out whether COOKIELOCALE is set (well I hope). | ||
* Added new functions to expose the current language and current locale. (not in the wiki yet) | ||
* Seperated redundant calls to a function when setting current locales and language. | ||
* Made gengo a bit more modular by creating more functions to clean up clutter. | ||
* Added new universe option which allows you to use definitions on all routes. (not in the wiki yet) | ||
##QA | ||
@@ -4,0 +21,0 @@ |
Sorry, the diff of this file is not supported yet
20141
6
406
113