Permalink
Browse files

added extension config, fixed #17 and #18

  • Loading branch information...
1 parent 595be4d commit a31fb439e73403312b0309a0ad46c8733ed56a2b @mashpie committed May 26, 2012
Showing with 66 additions and 15 deletions.
  1. +1 −0 README.md
  2. +7 −0 TODO.md
  3. +22 −14 i18n.js
  4. +17 −0 locales/de.json
  5. +17 −0 locales/en.json
  6. +1 −0 test/i18n.configure.js
  7. +1 −1 test/i18n.test.js
View
@@ -161,6 +161,7 @@ that file can be edited or just uploaded to [webtranslateit](http://docs.webtran
## Changelog
+* 0.3.5: fixed some issues, prepared refactoring, prepared publishing to npm finally
* 0.3.4: merged pull request #13 from Fuitad/master and updated README
* 0.3.3: merged pull request from codders/master and modified for backward compatibility. Usage and tests pending
* 0.3.2: merged pull request #7 from carlptr/master and added tests, modified fswrite to do sync writes
View
@@ -0,0 +1,7 @@
+Roadmap
+========
+* better parsing of defaults (and adding more reasonable default settings)
+* lot's of more configure settings
+* better sanitation in all fs operations
+* more examples, usage with template engines, usage as backend module
+* named parameters in translation strings
View
@@ -3,7 +3,7 @@
* @link https://github.com/mashpie/i18n-node
* @license http://creativecommons.org/licenses/by-sa/3.0/
*
- * @version 0.3.4
+ * @version 0.3.5
*/
// dependencies
@@ -16,12 +16,13 @@ var vsprintf = require('sprintf').vsprintf,
cookiename = null,
debug = false,
verbose = false,
+ extension = '.js',
directory = './locales';
// public exports
var i18n = exports;
-i18n.version = '0.3.4';
+i18n.version = '0.3.5';
i18n.configure = function (opt) {
// you may register helpers in global scope, up to you
@@ -39,10 +40,15 @@ i18n.configure = function (opt) {
// where to store json files
if (typeof opt.directory === 'string') {
directory = opt.directory;
- } else {
+ }else{
directory = './locales';
}
+ // where to store json files
+ if (typeof opt.extension === 'string') {
+ extension = opt.extension;
+ }
+
// enabled some debug output
if (opt.debug) {
debug = opt.debug;
@@ -219,28 +225,29 @@ function translate(locale, singular, plural) {
function read(locale) {
var localeFile = {};
var file = locate(locale);
- // try to read from FS
try {
- localeFile = fs.readFileSync(file);
if (verbose) console.log('read ' + file + ' for locale: ' + locale);
+ localeFile = fs.readFileSync(file);
+ try {
+ // parsing filecontents to locales[locale]
+ locales[locale] = JSON.parse(localeFile);
+ } catch (e) {
+ console.error('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
+ }
} catch (e) {
+ // 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 (verbose) console.log('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);
- }
}
// try writing a file in a created directory
function write(locale) {
try {
- stats = fs.lstatSync(directory);
+ var stats = fs.lstatSync(directory);
} catch (e) {
if (debug) console.log('creating locales dir in: ' + directory);
fs.mkdirSync(directory, 0755);
@@ -264,5 +271,6 @@ function write(locale) {
// basic normalization of filepath
function locate(locale) {
- return path.normalize(directory + '/' + locale + '.js');
+ var ext = extension || '.js';
+ return path.normalize(directory + '/' + locale + ext);
}
View
@@ -0,0 +1,17 @@
+{
+ "Hello": "Hallo",
+ "Hello %s, how are you today?": "Hallo %s, wie geht es dir heute?",
+ "weekend": "Wochenende",
+ "Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s.",
+ "Hi": "Hi",
+ "Howdy": "Hallöchen",
+ "%s cat": {
+ "one": "%s Katze",
+ "other": "%s Katzen"
+ },
+ "There is one monkey in the %%s": {
+ "one": "Im %%s sitzt ein Affe",
+ "other": "Im Baum sitzen %d Affen"
+ },
+ "tree": "Baum"
+}
View
@@ -0,0 +1,17 @@
+{
+ "Hello": "Hello",
+ "Hello %s, how are you today?": "Hello %s, how are you today?",
+ "weekend": "weekend",
+ "Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s.",
+ "Hi": "Hi",
+ "Howdy": "Howdy",
+ "%s cat": {
+ "one": "%s cat",
+ "other": "%s cats"
+ },
+ "There is one monkey in the %%s": {
+ "one": "There is one monkey in the %%s",
+ "other": "There are %d monkeys in the %%s"
+ },
+ "tree": "tree"
+}
@@ -7,6 +7,7 @@ i18n.configure({
locales: ['en', 'de'],
register: global,
directory: './testlocales',
+ extension: '.json',
debug: false
});
View
@@ -13,7 +13,7 @@ i18n.configure({
module.exports = {
'check version': function () {
- assert.equal(i18n.version, '0.3.4');
+ assert.equal(i18n.version, '0.3.5');
},
'check set/getLocale': function () {

0 comments on commit a31fb43

Please sign in to comment.