Comparing version 0.0.3 to 0.1.3
106
gengo.js
@@ -0,1 +1,15 @@ | ||
/*jslint forin: true*/ | ||
/*jslint white: true*/ | ||
/*jshint immed: true*/ | ||
/*global module, $, console*/ | ||
/* | ||
* gengojs | ||
* version : 0.0.3 | ||
* author : Takeshi Iwana | ||
* https://github.com/iwatakeshi | ||
* license : MIT | ||
* Code heavily borrowed from Adam Draper | ||
* https://github.com/adamwdraper | ||
*/ | ||
(function() { | ||
@@ -32,8 +46,15 @@ 'use strict'; | ||
var gengo, | ||
moment = require('moment'), | ||
numeral = require('numeral'), | ||
sprintf = require("sprintf-js").sprintf, | ||
vsprintf = require("sprintf-js").vsprintf, | ||
// check for nodeJS | ||
hasModule = (typeof module !== 'undefined' && module.exports), | ||
VERSION = '0.0.3', | ||
VERSION = '0.1.3', | ||
//configuration with defaults set | ||
CONFIG = { | ||
//path to locales | ||
gengo: "__", | ||
moment: "moment", | ||
numeral: "numeral", | ||
localePath: require('app-root-path') + '/locales/', | ||
@@ -53,9 +74,23 @@ //debugging purposes | ||
LOCALE = {}, | ||
//maps the languages according to each library's file name | ||
LOCALES = { | ||
ja: 'ja', | ||
en: 'en', | ||
en_US: 'en_US' | ||
gengo: { | ||
ja: 'ja', | ||
en: 'en', | ||
en_US: 'en_US' | ||
}, | ||
numeral: { | ||
ja: 'ja', | ||
en: 'en', | ||
en_US: 'en' | ||
}, | ||
moment: { | ||
ja: 'ja', | ||
en: 'en', | ||
en_US: 'en' | ||
} | ||
}, | ||
ROUTE, | ||
COOKIELOCALE; | ||
COOKIELOCALE, | ||
NUMERALPATH = 'numeral/languages/'; | ||
@@ -66,9 +101,18 @@ | ||
************************************/ | ||
/* | ||
* @method main | ||
* @param {String} input | ||
* @param {Object, Array, String} arg | ||
*/ | ||
gengo = function(input, arg) { | ||
debug('-----------------------------------'); | ||
debug(input, 'fn: gengo, Input'); | ||
debug(input, 'fn: gengo, Input'); | ||
//check to see if COOKIELOCALE || BESTMATCH === default | ||
if (isDefault()) { | ||
debug('-----------------------------------'); | ||
debug('fn: gengo, isDefault'); | ||
loadMoment(LOCALES.moment[CONFIG.default]); | ||
loadNumeral(LOCALES.numeral[CONFIG.default]); | ||
if (arg !== undefined) { | ||
@@ -79,3 +123,2 @@ return replace(input, arg); | ||
} | ||
} else { | ||
@@ -94,6 +137,3 @@ loadLocale(); | ||
} | ||
} else { | ||
debug(LOCALE[input], 'fn: gengo, Output'); | ||
@@ -116,3 +156,2 @@ debug('-----------------------------------'); | ||
var _locale = require('locale'); | ||
app.use(_locale(CONFIG.supported)); | ||
@@ -134,3 +173,5 @@ app.use(function(req, res, next) { | ||
} | ||
res.locals.__ = gengo; | ||
res.locals[CONFIG.gengo] = gengo; | ||
res.locals[CONFIG.moment] = moment; | ||
res.locals[CONFIG.numeral] = numeral; | ||
next(); | ||
@@ -151,14 +192,38 @@ }); | ||
if (COOKIELOCALE) { | ||
LOCALE = require(CONFIG.localePath + LOCALES[COOKIELOCALE] + '.js'); | ||
debug('fn: loadLocale, In COOKIELOCALE'); | ||
LOCALE = require(CONFIG.localePath + LOCALES.gengo[COOKIELOCALE] + '.js'); | ||
loadMoment(LOCALES.moment[COOKIELOCALE]); | ||
loadNumeral(LOCALES.numeral[COOKIELOCALE]); | ||
} else { | ||
LOCALE = require(CONFIG.localePath + LOCALES[BESTMATCH] + '.js'); | ||
debug('fn: loadLocale, In BESTMATCH'); | ||
LOCALE = require(CONFIG.localePath + LOCALES.gengo[BESTMATCH] + '.js'); | ||
loadMoment(LOCALES.moment[BESTMATCH]); | ||
loadNumeral(LOCALEs.numeral[BESTMATCH]); | ||
} | ||
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"); | ||
} | ||
}; | ||
function loadMoment(locale) { | ||
moment.locale(locale); | ||
debug(moment.locale(), 'fn: loadMoment, Current moment language'); | ||
debug(moment().format('dddd'), 'fn: loadMoment, Current moment: Today is'); | ||
} | ||
function loadNumeral(locale) { | ||
if (locale === 'en') { | ||
numeral.language('en'); | ||
} else { | ||
numeral.language(locale, require(NUMERALPATH + locale)); | ||
numeral.language(locale); | ||
} | ||
debug(numeral.language(), 'fn: loadNumeral, Current numeral language'); | ||
debug(numeral(10000).format('$0,0.00'), 'fn: loadNumeral, Current numeral: I don\'t have'); | ||
} | ||
/* | ||
@@ -199,3 +264,3 @@ *@method debug | ||
}; | ||
//for viewAware | ||
function router() { | ||
@@ -226,5 +291,4 @@ //check if the route matches the views from config | ||
//sprintf function | ||
function replace(input, arg) { | ||
var sprintf = require("sprintf-js").sprintf, | ||
vsprintf = require("sprintf-js").vsprintf; | ||
@@ -231,0 +295,0 @@ if (typeof arg === Array) { |
{ | ||
"name": "gengojs", | ||
"version": "0.0.3", | ||
"version": "0.1.3", | ||
"description": "An uber basic and simple i18n library for Express 4", | ||
@@ -12,2 +12,4 @@ "main": "gengo.js", | ||
"locale": "0.0.17", | ||
"moment": "^2.8.2", | ||
"numeral": "^1.5.3", | ||
"sprintf-js": "0.0.7" | ||
@@ -20,3 +22,5 @@ }, | ||
"keywords": [ | ||
"i18n" | ||
"i18n", | ||
"translate", | ||
"gengo" | ||
], | ||
@@ -23,0 +27,0 @@ "author": "iwatakeshi", |
117
README.md
@@ -1,13 +0,18 @@ | ||
gengojs | ||
======= | ||
An **uber** basic and **simple** i18n library for Express 4. | ||
Gengo.js | ||
========= | ||
##QA | ||
##Usage | ||
###What is Gengo.js? | ||
gengo is a library that allows you to translate your pages automatically (hot swapping) without having to do tedious stuff...well that is my goal. gengo only requires you to provide the translation files and then your done :). | ||
Also, before moving on, Moment.js and Numeral.js are part of gengo. What does that mean? Well it simply means you get the best of the best in a small package. Technically it means that moment and numeral will change along with gengo (see [Translating](https://github.com/iwatakeshi/gengojs/wiki/Translating) for more details). | ||
###npm | ||
###Where to begin? | ||
gengo is available on npm. To begin: | ||
```bash | ||
sudo npm install gengojs | ||
##or | ||
sudo npm install gengojs --save | ||
``` | ||
### In app.js | ||
then in your app.js | ||
```js | ||
@@ -22,3 +27,2 @@ //require | ||
supported: ['ja','en_US'] | ||
//see API for more configurations | ||
}); | ||
@@ -29,3 +33,8 @@ //init before your routes. if using express generator it would be right after the last app.use | ||
``` | ||
### In locales folder | ||
for more configurations options see [API](https://github.com/iwatakeshi/gengojs/wiki/API). | ||
From there you have two options, you can have gengo to: | ||
* load the words/sentences from the translation file directly | ||
* load the words/sentences from the translation file by route (not fully tested) | ||
an example will look like this in your locale folder: | ||
```js | ||
@@ -47,4 +56,3 @@ //ja.js | ||
### In Jade | ||
I'll need testers to try it in other templates, but it shouldn't matter. | ||
Now in your template file (Note: I've only used Jade, others should work) | ||
```jade | ||
@@ -58,86 +66,9 @@ extends layout | ||
``` | ||
Note: It is possible to replace words in your translations. gengo uses [sprintf](https://github.com/alexei/sprintf.js) as its core for | ||
replacements. The only feature that is not supported is [Argument swapping](https://github.com/alexei/sprintf.js#argument-swapping) due to the use of n-arugments. I haven't figured out how to use n-arguments with gengo already using two. | ||
For more templating and translation file examples see [Translating](https://github.com/iwatakeshi/gengojs/wiki/Translating) | ||
##API | ||
###Config | ||
#####localePath (String) | ||
This is the path to the locale files. Default is [the app's root]/locales. | ||
#####debug (bool) | ||
Allows you to see the nitty gritty stuff. Default is `false`. | ||
#####supported (Array of Strings) | ||
The supported locales in your app. Default is `['en_US', 'en']`. | ||
###Can users change the language? | ||
Yes! The only way the user can change language (at the moment) is by using cookies. see [Translating](https://github.com/iwatakeshi/gengojs/wiki/Translating) for an example. | ||
Note: gengo is strict concerning comparisons. If your default does contain similar languages, ie. `'en_US'` and `'en'`, you must provide both. I personally am not aware of of the differences, but if there are none then just duplicate the file and rename the file. | ||
#####default (String) | ||
The default language used in your html, jade, etc. This means you can use a native language and translate it. Default is `'en_US'`. | ||
#####viewAware (bool) | ||
View Aware allows you to organize your translation files into paths/routes. See example above. Note: In theory the template shouldn't matter. Default is `false`. | ||
#####views (JSON like Object) | ||
Allows you to set your view's path and the name of your view to that path(or the route set in the translation file). ie. the default is: | ||
```js | ||
{ | ||
"/": 'index' | ||
} | ||
``` | ||
##Note | ||
This may not be for you, but it is highly customizable (ie. In theory, it should work with browsers, but that part has not been implemented) so feel free to fork and improve it @ http://github.com/iwatakeshi/gengojs . Also, the sample version is in the 'master' and the npm version is in the 'npm' branch. (The sample isn't using npm version of gengojs, but a local version for the mean time.) | ||
##Tested | ||
* Express | ||
* I've only tested with Express 4 (using generator) and Jade. | ||
* Languages | ||
* English | ||
* Japanese | ||
* Pages | ||
* index.jade | ||
##How to test multiple languages | ||
I'm currently using Opera with a default language set to 'ja'. Also, [postman](https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en) with the 'Accept-Language' header set to 'ja' works as well. | ||
In theory, gengo will only translate to those that are not native to the default. So ie. lets say the default is 'en', if Chrome has 'en_US' and Opera | ||
has 'ja', only the user using Opera will see the translation and gengo will just return the english words for the Chrome user. | ||
##~~TODO~~ | ||
~~Currently, gengo can't translate for those who are using a 'en_US' set browser but want a 'ja' translation in that browser (I think ha!). | ||
I could have another setting that can be set from the browser to where it looks like so:~~ | ||
```jade | ||
//in index.jade | ||
script __.setLocale = 'ja' | ||
``` | ||
~~If I can update the webpage in realtime that would be great, otherwise I'll need to refresh the page.~~ | ||
###Update: Changing locale using cookies | ||
You can now change the locale using cookies. The sample app will contain an example but specifically your index page could look like so: | ||
```jade | ||
//In this case, jade will be used. | ||
block content | ||
h1= __(title) | ||
p #{__("エクスプレスへようこそ")} | ||
//some buttons to change the locale | ||
button(onclick="locale().set('ja');") Japanese | ||
button(onclick="locale().set('en_US');") English | ||
button(onclick="locale().reset();") Reset | ||
script. | ||
var locale = function(){ | ||
return{ | ||
set: function(locale){ | ||
//set the cookie | ||
document.cookie = "locale=" + locale; | ||
//refresh the page | ||
location.reload(); | ||
}, | ||
reset: function(){ | ||
//reset the cookie | ||
document.cookie = "locale="; | ||
//refresh the page | ||
location.reload(); | ||
} | ||
}; | ||
}; | ||
``` | ||
The result will be the translated page. If you can think of a better way to change locales without cookies, then feel free to fork away and improve library. | ||
Note: Setting the locale is not manditory and gengo should still be able to translate without it, but if you want users to change languages then this is how it could be done. | ||
For more info on features, check out the wiki. I will be writing on the wiki instead of populating this readme with too much stuff ha! | ||
###Can I contribute? | ||
Sure! See [Contribute](https://github.com/iwatakeshi/gengojs/wiki/Contribute) for more details. |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
285
13685
5
70
3
+ Addedmoment@^2.8.2
+ Addednumeral@^1.5.3
+ Addedmoment@2.30.1(transitive)
+ Addednumeral@1.5.6(transitive)