Skip to content
This repository
Browse code

added extension config, fixed #17 and #18

  • Loading branch information...
commit a31fb439e73403312b0309a0ad46c8733ed56a2b 1 parent 595be4d
Marcus Spiegel authored May 26, 2012
1  README.md
Source Rendered
@@ -161,6 +161,7 @@ that file can be edited or just uploaded to [webtranslateit](http://docs.webtran
161 161
 	
162 162
 ## Changelog
163 163
 
  164
+* 0.3.5: fixed some issues, prepared refactoring, prepared publishing to npm finally
164 165
 * 0.3.4: merged pull request #13 from Fuitad/master and updated README
165 166
 * 0.3.3: merged pull request from codders/master and modified for backward compatibility. Usage and tests pending
166 167
 * 0.3.2: merged pull request #7 from carlptr/master and added tests, modified fswrite to do sync writes
7  TODO.md
Source Rendered
... ...
@@ -0,0 +1,7 @@
  1
+Roadmap
  2
+========
  3
+* better parsing of defaults (and adding more reasonable default settings)
  4
+* lot's of more configure settings
  5
+* better sanitation in all fs operations
  6
+* more examples, usage with template engines, usage as backend module
  7
+* named parameters in translation strings
36  i18n.js
@@ -3,7 +3,7 @@
3 3
  * @link        https://github.com/mashpie/i18n-node
4 4
  * @license		  http://creativecommons.org/licenses/by-sa/3.0/
5 5
  *
6  
- * @version     0.3.4
  6
+ * @version     0.3.5
7 7
  */
8 8
 
9 9
 // dependencies
@@ -16,12 +16,13 @@ var vsprintf = require('sprintf').vsprintf,
16 16
     cookiename = null,
17 17
     debug = false,
18 18
     verbose = false,
  19
+    extension = '.js',
19 20
     directory = './locales';
20 21
 
21 22
 // public exports
22 23
 var i18n = exports;
23 24
 
24  
-i18n.version = '0.3.4';
  25
+i18n.version = '0.3.5';
25 26
 
26 27
 i18n.configure = function (opt) {
27 28
   // you may register helpers in global scope, up to you
@@ -39,10 +40,15 @@ i18n.configure = function (opt) {
39 40
   // where to store json files
40 41
   if (typeof opt.directory === 'string') {
41 42
     directory = opt.directory;
42  
-  } else {
  43
+  }else{
43 44
     directory = './locales';
44 45
   }
45 46
 
  47
+  // where to store json files
  48
+  if (typeof opt.extension === 'string') {
  49
+    extension = opt.extension;
  50
+  }
  51
+
46 52
   // enabled some debug output
47 53
   if (opt.debug) {
48 54
     debug = opt.debug;
@@ -219,28 +225,29 @@ function translate(locale, singular, plural) {
219 225
 function read(locale) {
220 226
   var localeFile = {};
221 227
   var file = locate(locale);
222  
-  // try to read from FS
223 228
   try {
224  
-    localeFile = fs.readFileSync(file);
225 229
     if (verbose) console.log('read ' + file + ' for locale: ' + locale);
  230
+    localeFile = fs.readFileSync(file);
  231
+    try {
  232
+      // parsing filecontents to locales[locale]
  233
+      locales[locale] = JSON.parse(localeFile);
  234
+    } catch (e) {
  235
+      console.error('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
  236
+    }
226 237
   } catch (e) {
  238
+    // unable to read, so intialize that file
  239
+    // locales[locale] are already set in memory, so no extra read required
  240
+    // or locales[locale] are empty, which initializes an empty locale.json file
227 241
     if (verbose) console.log('initializing ' + file);
228 242
     write(locale);
229 243
   }
230  
-
231  
-  // try to parse to JSON
232  
-  try {
233  
-    locales[locale] = JSON.parse(localeFile);
234  
-  } catch (e) {
235  
-    console.error('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e);
236  
-  }
237 244
 }
238 245
 
239 246
 // try writing a file in a created directory
240 247
 
241 248
 function write(locale) {
242 249
   try {
243  
-    stats = fs.lstatSync(directory);
  250
+    var stats = fs.lstatSync(directory);
244 251
   } catch (e) {
245 252
     if (debug) console.log('creating locales dir in: ' + directory);
246 253
     fs.mkdirSync(directory, 0755);
@@ -264,5 +271,6 @@ function write(locale) {
264 271
 // basic normalization of filepath
265 272
 
266 273
 function locate(locale) {
267  
-  return path.normalize(directory + '/' + locale + '.js');
  274
+  var ext = extension || '.js';
  275
+  return path.normalize(directory + '/' + locale + ext);
268 276
 }
17  locales/de.json
... ...
@@ -0,0 +1,17 @@
  1
+{
  2
+  "Hello": "Hallo",
  3
+  "Hello %s, how are you today?": "Hallo %s, wie geht es dir heute?",
  4
+  "weekend": "Wochenende",
  5
+  "Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s.",
  6
+  "Hi": "Hi",
  7
+  "Howdy": "Hallöchen",
  8
+  "%s cat": {
  9
+    "one": "%s Katze",
  10
+    "other": "%s Katzen"
  11
+  },
  12
+  "There is one monkey in the %%s": {
  13
+    "one": "Im %%s sitzt ein Affe",
  14
+    "other": "Im Baum sitzen %d Affen"
  15
+  },
  16
+  "tree": "Baum"
  17
+}
17  locales/en.json
... ...
@@ -0,0 +1,17 @@
  1
+{
  2
+  "Hello": "Hello",
  3
+  "Hello %s, how are you today?": "Hello %s, how are you today?",
  4
+  "weekend": "weekend",
  5
+  "Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s.",
  6
+  "Hi": "Hi",
  7
+  "Howdy": "Howdy",
  8
+  "%s cat": {
  9
+    "one": "%s cat",
  10
+    "other": "%s cats"
  11
+  },
  12
+  "There is one monkey in the %%s": {
  13
+    "one": "There is one monkey in the %%s",
  14
+    "other": "There are %d monkeys in the %%s"
  15
+  },
  16
+  "tree": "tree"
  17
+}
1  test/i18n.configure.js
@@ -7,6 +7,7 @@ i18n.configure({
7 7
   locales: ['en', 'de'],
8 8
   register: global,
9 9
   directory: './testlocales',
  10
+  extension: '.json',
10 11
   debug: false
11 12
 });
12 13
 
2  test/i18n.test.js
@@ -13,7 +13,7 @@ i18n.configure({
13 13
 
14 14
 module.exports = {
15 15
   'check version': function () {
16  
-    assert.equal(i18n.version, '0.3.4');
  16
+    assert.equal(i18n.version, '0.3.5');
17 17
   },
18 18
 
19 19
   'check set/getLocale': function () {

0 notes on commit a31fb43

Please sign in to comment.
Something went wrong with that request. Please try again.