Skip to content
This repository
Browse code

changed default extension to .json with .js fallback, closed #16

  • Loading branch information...
commit 18b0cd70fa208351529c4f04833d273681d041ce 1 parent fb060c2
Marcus Spiegel authored
4 .gitignore
@@ -4,4 +4,6 @@ testlocales/
4 4 /npm-debug.log
5 5
6 6 /i18n-cov.js
7   -/coverage.html
  7 +/coverage.html
  8 +/locales/en.json
  9 +/locales/de.json
23 i18n.js
@@ -40,7 +40,7 @@ i18n.configure = function i18nConfigure(opt) {
40 40 updateFiles = (typeof opt.updateFiles === 'boolean') ? opt.updateFiles : true;
41 41
42 42 // where to store json files
43   - extension = (typeof opt.extension === 'string') ? opt.extension : '.js';
  43 + extension = (typeof opt.extension === 'string') ? opt.extension : '.json';
44 44
45 45 // setting defaultLocale
46 46 defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';
@@ -308,6 +308,23 @@ function write(locale) {
308 308
309 309 // basic normalization of filepath
310 310 function getStorageFilePath(locale) {
311   - var ext = extension || '.js';
312   - return path.normalize(directory + '/' + locale + ext);
  311 + // changed API to use .json as default, #16
  312 + var ext = extension || '.json',
  313 + filepath = path.normalize(directory + '/' + locale + ext),
  314 + filepathJS = path.normalize(directory + '/' + locale + '.js');
  315 + // use .js as fallback if already existing
  316 + try {
  317 + if (fs.statSync(filepathJS)) {
  318 + if (debug) {
  319 + console.log('using existing file ' + filepathJS);
  320 + }
  321 + extension = '.js';
  322 + return filepathJS;
  323 + }
  324 + } catch (e) {
  325 + if (debug) {
  326 + console.log('will write to ' + filepath);
  327 + }
  328 + }
  329 + return filepath;
313 330 }
21 locales/de.json
... ... @@ -1,21 +0,0 @@
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   - "There is one monkey in the %s": {
18   - "one": "There is one monkey in the %s",
19   - "other": "There are %d monkeys in the %s"
20   - }
21   -}
21 locales/en.json
... ... @@ -1,21 +0,0 @@
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   - "There is one monkey in the %s": {
18   - "one": "There is one monkey in the %s",
19   - "other": "There are %d monkeys in the %s"
20   - }
21   -}
44 test/i18n.configure.js
@@ -12,52 +12,32 @@ describe('Module Config', function () {
12 12 i18n.configure({
13 13 locales: ['en', 'de'],
14 14 register: testScope,
15   - directory: './testlocales',
16   - extension: '.json',
  15 + directory: './customlocales',
  16 + extension: '.customextension',
17 17 });
  18 + testScope.__('Hello');
18 19 });
19 20
20 21 afterEach(function () {
21   - var stats = fs.lstatSync('./testlocales');
  22 + var stats = fs.lstatSync('./customlocales');
22 23 should.exist(stats);
23 24 if (stats) {
24   - fs.unlinkSync('./testlocales/de.json');
25   - fs.unlinkSync('./testlocales/en.json');
26   - fs.rmdirSync('./testlocales');
  25 + fs.unlinkSync('./customlocales/de.customextension');
  26 + fs.unlinkSync('./customlocales/en.customextension');
  27 + fs.rmdirSync('./customlocales');
27 28 }
28 29 });
29 30
30   - it('setting locales in scope should return appropiate locale', function () {
31   - should.equal('de', i18n.setLocale(testScope, 'de'));
32   - });
33   -
34   - it('getting locales in scope should return appropiate locale', function () {
35   - i18n.setLocale(testScope, 'de')
36   - should.equal('de', testScope.getLocale(testScope));
37   - });
38   -
39   - it('getting defaultLocale should return appropiate locale', function () {
40   - should.equal('en', i18n.getLocale());
41   - });
42   -
43   - it('translations should work', function () {
44   - i18n.setLocale(testScope, 'de');
45   - should.equal(testScope.__('Hello'), 'Hallo');
46   - should.equal(testScope.__('Hello %s, how are you today?', 'Marcus'), 'Hallo Marcus, wie geht es dir heute?');
47   - should.equal(testScope.__('Hello %s, how are you today? How was your %s.', 'Marcus', testScope.__('weekend')), 'Hallo Marcus, wie geht es dir heute? Wie war dein Wochenende.');
48   - });
49   -
50   - it('the custom directory should exist', function () {
51   - var stats = fs.lstatSync('./testlocales');
  31 + it('should be possible to setup a custom directory', function () {
  32 + var stats = fs.lstatSync('./customlocales');
52 33 should.exist(stats);
53 34 });
54 35
55   - it('the custom files should exist with custom extensions', function () {
56   - var statsde = fs.lstatSync('./testlocales/de.json'),
57   - statsen = fs.lstatSync('./testlocales/en.json');
  36 + it('should be possible to read custom files with custom extensions', function () {
  37 + var statsde = fs.lstatSync('./customlocales/de.customextension'),
  38 + statsen = fs.lstatSync('./customlocales/en.customextension');
58 39 should.exist(statsde);
59 40 should.exist(statsen);
60 41 });
61 42
62   -
63 43 });
42 test/i18n.defaults.js
... ... @@ -0,0 +1,42 @@
  1 +/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */
  2 +
  3 +var i18n = process.env.EXPRESS_COV ? require('../i18n-cov') : require('../i18n'),
  4 + should = require("should"),
  5 + fs = require('fs');
  6 +
  7 +describe('Module Defaults', function () {
  8 +
  9 + var testScope = {};
  10 +
  11 + beforeEach(function () {
  12 + i18n.configure({
  13 + locales: ['en', 'de'],
  14 + register: testScope,
  15 + directory: './defaultlocales'
  16 + });
  17 + testScope.__('Hello');
  18 + });
  19 +
  20 + afterEach(function () {
  21 + var stats = fs.lstatSync('./defaultlocales');
  22 + should.exist(stats);
  23 + if (stats) {
  24 + fs.unlinkSync('./defaultlocales/de.json');
  25 + fs.unlinkSync('./defaultlocales/en.json');
  26 + fs.rmdirSync('./defaultlocales');
  27 + }
  28 + });
  29 +
  30 + it('should be possible to setup a custom directory', function () {
  31 + var stats = fs.lstatSync('./defaultlocales');
  32 + should.exist(stats);
  33 + });
  34 +
  35 + it('should be possible to read custom files with default a extension of .json (issue #16)', function () {
  36 + var statsde = fs.lstatSync('./defaultlocales/de.json'),
  37 + statsen = fs.lstatSync('./defaultlocales/en.json');
  38 + should.exist(statsde);
  39 + should.exist(statsen);
  40 + });
  41 +
  42 +});

0 comments on commit 18b0cd7

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