Browse files

refactored writes to sync, started porting tests to vows

  • Loading branch information...
1 parent 24f0617 commit 36a39fe4c6d92a42c27a81a2a4a0637c3bbda09f @mashpie committed May 29, 2012
Showing with 55 additions and 12 deletions.
  1. +2 −1 .gitignore
  2. +2 −1 TODO.md
  3. +18 −9 i18n.js
  4. +2 −1 package.json
  5. +31 −0 vows/i18n.configure.vows.js
View
3 .gitignore
@@ -1,2 +1,3 @@
node_modules/
-testlocales/
+testlocales/
+.DS_Store
View
3 TODO.md
@@ -1,7 +1,8 @@
Roadmap
========
+* switch from expresso to vows for testing
* better parsing of defaults (and adding more reasonable default settings)
* lot's of more configure settings
-* better sanitation in all fs operations
+* better sanitation in all fs operations => plugin system for storage
* more examples, usage with template engines, usage as backend module
* named parameters in translation strings
View
27 i18n.js
@@ -40,7 +40,7 @@ i18n.configure = function (opt) {
// where to store json files
if (typeof opt.directory === 'string') {
directory = opt.directory;
- }else{
+ } else {
directory = './locales';
}
@@ -195,7 +195,7 @@ function guessLanguage(request) {
function translate(locale, singular, plural) {
if (locale === undefined) {
- if (debug) console.warn("WARN: No locale found - check the context of the call to $__?");
+ if (debug) console.warn("WARN: No locale found - check the context of the call to $__. Using " + defaultLocale + " (set by request) as current locale");
locale = defaultLocale;
}
@@ -246,25 +246,34 @@ function read(locale) {
// try writing a file in a created directory
function write(locale) {
+ // creating directory if necessary
try {
var stats = fs.lstatSync(directory);
} catch (e) {
if (debug) console.log('creating locales dir in: ' + directory);
fs.mkdirSync(directory, 0755);
}
- var target = locate(locale),
- tmp = target + ".tmp";
// first time init has an empty file
if (!locales[locale]) {
locales[locale] = {};
}
- fs.writeFileSync(tmp, JSON.stringify(locales[locale], null, "\t"), "utf8");
- fs.stat(tmp, function (err, stat) {
- if (err) throw err;
- fs.rename(tmp, target);
- });
+ // writing to tmp and rename on success
+ try {
+ var target = locate(locale),
+ tmp = target + ".tmp";
+
+ fs.writeFileSync(tmp, JSON.stringify(locales[locale], null, "\t"), "utf8");
+ var Stats = fs.statSync(tmp);
+ if (Stats.isFile()) {
+ fs.renameSync(tmp, target);
+ } else {
+ console.error('unable to write locales to file (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
+ }
+ } catch (e) {
+ console.error('unexpected error writing files (either ' + tmp + ' or ' + target + ' are not writeable?): ', e);
+ }
}
View
3 package.json
@@ -14,7 +14,8 @@
},
"dependencies": {
"sprintf": ">=0.1.1",
- "expresso": ">=0.9.2"
+ "expresso": ">=0.9.2",
+ "vows": ">=0.6.2"
},
"engines": {
"node": ">=0.4.0"
View
31 vows/i18n.configure.vows.js
@@ -0,0 +1,31 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ i18n = require('../i18n');
+
+i18n.configure({
+ locales: ['en', 'de'],
+ register: global,
+ directory: './testlocales',
+ extension: '.json',
+ debug: false,
+ verbose: false
+});
+
+vows.describe('Configuration settings').addBatch({
+
+ 'when setting to custom directory with custom extension': {
+
+ 'setLocale and getLocale should still be able to set current locale': function () {
+ assert.equal('en', i18n.getLocale(), 'should return default setting');
+ assert.equal('de', i18n.setLocale('de'), 'should return the new setting');
+ assert.equal('de', i18n.getLocale(), 'should return the new setting');
+ },
+
+ 'and some basic translations should still work': function () {
+ i18n.setLocale('en');
+ assert.equal(__('Hello'), 'Hello');
+ assert.equal(__('Hello %s, how are you today?', 'Marcus'), 'Hello Marcus, how are you today?');
+ assert.equal(__('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend')), 'Hello Marcus, how are you today? How was your weekend.');
+ }
+ }
+}).export(module);

0 comments on commit 36a39fe

Please sign in to comment.