Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

module api is now named, added more tests

  • Loading branch information...
commit b230ef47f4671a95ccfa68d43a652d81ac644667 1 parent 1e89558
@mashpie authored
Showing with 229 additions and 155 deletions.
  1. +5 −5 i18n.js
  2. +27 −29 test/i18n.configure.js
  3. +197 −121 test/i18n.test.js
View
10 i18n.js
@@ -26,7 +26,7 @@ var i18n = exports;
i18n.version = '0.3.8';
-i18n.configure = function (opt) {
+i18n.configure = function i18nConfigure (opt) {
// you may register helpers in global scope, up to you
if (typeof opt.register === 'object') {
@@ -84,7 +84,7 @@ i18n.init = function i18nInit(request, response, next) {
}
};
-i18n.__ = function (phrase) {
+i18n.__ = function i18nTranslate(phrase) {
var locale, msg;
if (this && this.scope) {
locale = this.scope.locale;
@@ -99,7 +99,7 @@ i18n.__ = function (phrase) {
return msg;
};
-i18n.__n = function (singular, plural, count) {
+i18n.__n = function i18nTranslatePlural(singular, plural, count) {
var locale, msg;
// get locale from scope (deprecated) or object
@@ -134,7 +134,7 @@ i18n.__n = function (singular, plural, count) {
// either gets called like
// setLocale('en') or like
// setLocale(req, 'en')
-i18n.setLocale = function (arg1, arg2) {
+i18n.setLocale = function i18nSetLocale(arg1, arg2) {
var target_locale = arg1,
request;
@@ -154,7 +154,7 @@ i18n.setLocale = function (arg1, arg2) {
return i18n.getLocale(request);
};
-i18n.getLocale = function (request) {
+i18n.getLocale = function i18nGetLocale(request) {
if (request && request.locale) {
return request.locale;
}
View
56 test/i18n.configure.js
@@ -4,7 +4,7 @@ var should = require("should"),
i18n = require('../i18n'),
fs = require('fs');
-describe('Configuration settings:', function () {
+describe('Module Config', function () {
var testScope = {};
@@ -30,39 +30,37 @@ describe('Configuration settings:', function () {
}
});
- describe('when using a custom directory with a custom extension', function () {
+ it('setting locales in scope should return appropiate locale', function () {
+ should.equal('de', i18n.setLocale(testScope, 'de'));
+ });
- it('setting locales in scope should return appropiate locale', function () {
- should.equal('de', i18n.setLocale(testScope, 'de'));
- });
+ it('getting locales in scope should return appropiate locale', function () {
+ i18n.setLocale(testScope, 'de')
+ should.equal('de', testScope.getLocale(testScope));
+ });
- it('getting locales in scope should return appropiate locale', function () {
- i18n.setLocale(testScope, 'de')
- should.equal('de', testScope.getLocale(testScope));
- });
+ it('getting defaultLocale should return appropiate locale', function () {
+ should.equal('en', i18n.getLocale());
+ });
- it('getting defaultLocale should return appropiate locale', function () {
- should.equal('en', i18n.getLocale());
- });
+ it('translations should work', function () {
+ i18n.setLocale(testScope, 'de');
+ should.equal(testScope.__('Hello'), 'Hallo');
+ should.equal(testScope.__('Hello %s, how are you today?', 'Marcus'), 'Hallo Marcus, wie geht es dir heute?');
+ 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.');
+ });
- it('basic translations should work', function () {
- i18n.setLocale(testScope, 'en');
- should.equal(testScope.__('Hello'), 'Hello');
- should.equal(testScope.__('Hello %s, how are you today?', 'Marcus'), 'Hello Marcus, how are you today?');
- should.equal(testScope.__('Hello %s, how are you today? How was your %s.', 'Marcus', testScope.__('weekend')), 'Hello Marcus, how are you today? How was your weekend.');
- });
+ it('the custom directory should exist', function () {
+ var stats = fs.lstatSync('./testlocales');
+ should.exist(stats);
+ });
- it('the directory should exist', function () {
- var stats = fs.lstatSync('./testlocales');
- should.exist(stats);
- });
+ it('the custom files should exist with custom extensions', function () {
+ var statsde = fs.lstatSync('./testlocales/de.json'),
+ statsen = fs.lstatSync('./testlocales/en.json');
+ should.exist(statsde);
+ should.exist(statsen);
+ });
- it('the files should exist', function () {
- var statsde = fs.lstatSync('./testlocales/de.json'),
- statsen = fs.lstatSync('./testlocales/en.json');
- should.exist(statsde);
- should.exist(statsen);
- });
- });
});
View
318 test/i18n.test.js
@@ -10,143 +10,219 @@ i18n.configure({
});
-describe('module setup', function () {
- it('the version should be set', function () {
+describe('Module Setup', function () {
+ it('should export a valid version', function () {
should.equal(i18n.version, '0.3.8');
});
-});
-
-describe('module api', function () {
-
- var req = {
- "request": "GET /test",
- __: i18n.__
- };
-
- describe('setLocal and getLocale', function () {
- it('getLocale should return default setting', function () {
- i18n.getLocale().should.equal('en');
- });
-
- it('setLocale should return the new setting', function () {
- i18n.setLocale('de').should.equal('de');
- });
- it('and getLocale should return the new setting', function () {
- i18n.getLocale().should.equal('de');
- });
-
- it('should return the current local setting, when used with 2 args', function () {
- i18n.setLocale(req, 'en').should.equal('en');
- });
-
- it('while getLocale should still return the previous global setting', function () {
- i18n.getLocale().should.equal('de');
- });
-
- it('now getLocale should return local locale when used with local object as 1st arg', function () {
- i18n.getLocale(req).should.equal('en');
- });
+ it('should export configure as i18nConfigure', function () {
+ should.equal(typeof i18n.configure, 'function');
+ should.equal(i18n.configure.name, 'i18nConfigure');
});
- describe('translating singulars', function () {
- it('should return en translations as expected', function () {
- i18n.setLocale('en');
- should.equal(__('Hello'), 'Hello');
- should.equal(__('Hello %s, how are you today?', 'Marcus'), 'Hello Marcus, how are you today?');
- should.equal(__('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend')), 'Hello Marcus, how are you today? How was your weekend.');
- });
-
- it('and should return de translations as expected', function () {
- i18n.setLocale('de');
- should.equal(__('Hello'), 'Hallo');
- should.equal(__('Hello %s, how are you today?', 'Marcus'), 'Hallo Marcus, wie geht es dir heute?');
- should.equal(__('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend')), 'Hallo Marcus, wie geht es dir heute? Wie war dein Wochenende.');
- });
+ it('should export init as i18nInit', function () {
+ should.equal(typeof i18n.init, 'function');
+ should.equal(i18n.init.name, 'i18nInit');
});
- describe('translating in scopes (ie.: request)', function(){
- it('has to use local translation in en', function(){
- i18n.setLocale(req, 'en').should.equal('en');
- req.__('Hello').should.equal('Hello');
- });
-
- it('while the global translation remains untouched', function(){
- should.equal(__('Hello'), 'Hallo');
- });
-
- it('and has to use local translation in de', function(){
- i18n.setLocale(req, 'de').should.equal('de');
- req.__('Hello').should.equal('Hallo');
- });
+ it('should export __ as i18nTranslate', function () {
+ should.equal(typeof i18n.__, 'function');
+ should.equal(i18n.__.name, 'i18nTranslate');
+ });
- it('still the global translation remains untouched', function(){
- should.equal(__('Hello'), 'Hallo');
- });
+ it('should export __n as i18nTranslatePlural', function () {
+ should.equal(typeof i18n.__n, 'function');
+ should.equal(i18n.__n.name, 'i18nTranslatePlural');
});
- describe('translating basic plurals', function(){
- it('should work as expected (to lazy to change from expresso)', function(){
- i18n.setLocale('en');
- var singular = __n('%s cat', '%s cats', 1),
- plural = __n('%s cat', '%s cats', 3);
- should.equal(singular, '1 cat');
- should.equal(plural, '3 cats');
-
- i18n.setLocale('de');
- singular = __n('%s cat', '%s cats', 1);
- plural = __n('%s cat', '%s cats', 3);
- should.equal(singular, '1 Katze');
- should.equal(plural, '3 Katzen');
- });
+ it('should export setLocale as i18nSetLocale', function () {
+ should.equal(typeof i18n.setLocale, 'function');
+ should.equal(i18n.setLocale.name, 'i18nSetLocale');
});
- describe('translating nested plurals', function(){
- it('should work as expected (to lazy to change from expresso)', function(){
- i18n.setLocale('en');
- var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree')),
- plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
- should.equal(singular, 'There is one monkey in the tree');
- should.equal(plural, 'There are 3 monkeys in the tree');
-
- i18n.setLocale('de');
- singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree'));
- plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
- should.equal(singular, 'Im Baum sitzt ein Affe');
- should.equal(plural, 'Im Baum sitzen 3 Affen');
- });
+ it('should export getLocale as i18nGetLocale', function () {
+ should.equal(typeof i18n.getLocale, 'function');
+ should.equal(i18n.getLocale.name, 'i18nGetLocale');
+ });
+});
- it('output of extra strings will break when not masked by an extra % (%% issue #49)', function(){
- i18n.setLocale('en');
- var singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, __('tree')),
- plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, __('tree'));
- should.equal(singular, 'There is one monkey in the 1');
- should.equal(plural, 'There are 3 monkeys in the undefined');
-
- i18n.setLocale('de');
- singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, __('tree'));
- plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, __('tree'));
- should.equal(singular, 'There is one monkey in the 1');
- should.equal(plural, 'There are 3 monkeys in the undefined');
+describe('Module API', function () {
+ describe('Global Scope', function () {
+ describe('i18nSetLocale and i18nGetLocale', function () {
+ it('getLocale should return default setting', function () {
+ i18n.getLocale().should.equal('en');
+ });
+
+ it('setLocale should return the new setting', function () {
+ i18n.setLocale('de').should.equal('de');
+ });
+
+ it('and getLocale should return the new setting', function () {
+ i18n.getLocale().should.equal('de');
+ });
+ });
+
+ describe('i18nTranslate', function () {
+ it('should return en translations as expected', function () {
+ i18n.setLocale('en');
+ should.equal(__('Hello'), 'Hello');
+ should.equal(__('Hello %s, how are you today?', 'Marcus'), 'Hello Marcus, how are you today?');
+ should.equal(__('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend')), 'Hello Marcus, how are you today? How was your weekend.');
+ });
+
+ it('should return de translations as expected', function () {
+ i18n.setLocale('de');
+ should.equal(__('Hello'), 'Hallo');
+ should.equal(__('Hello %s, how are you today?', 'Marcus'), 'Hallo Marcus, wie geht es dir heute?');
+ should.equal(__('Hello %s, how are you today? How was your %s.', 'Marcus', __('weekend')), 'Hallo Marcus, wie geht es dir heute? Wie war dein Wochenende.');
+ });
+
+ it('should also return translations when iterating thru variables values', function () {
+ var i = 0,
+ greetings = ['Hi', 'Hello', 'Howdy'],
+ greetingsDE = ['Hi', 'Hallo', 'Hallöchen'];
+
+ i18n.setLocale('en');
+ for (i = 0; i < greetings.length; i++) {
+ should.equal(greetings[i], __(greetings[i]));
+ }
+
+ i18n.setLocale('de');
+ for (i = 0; i < greetings.length; i++) {
+ should.equal(greetingsDE[i], __(greetings[i]));
+ }
+ });
+
+ });
+
+ describe('i18nTranslatePlural', function () {
+ it('should return singular or plural form based on last parameter', function () {
+ i18n.setLocale('en');
+ var singular = __n('%s cat', '%s cats', 1),
+ plural = __n('%s cat', '%s cats', 3);
+ should.equal(singular, '1 cat');
+ should.equal(plural, '3 cats');
+
+ i18n.setLocale('de');
+ singular = __n('%s cat', '%s cats', 1);
+ plural = __n('%s cat', '%s cats', 3);
+ should.equal(singular, '1 Katze');
+ should.equal(plural, '3 Katzen');
+ });
+
+ it('should return substituted phrases when used nested', function () {
+ i18n.setLocale('en');
+ var singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree')),
+ plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
+ should.equal(singular, 'There is one monkey in the tree');
+ should.equal(plural, 'There are 3 monkeys in the tree');
+
+ i18n.setLocale('de');
+ singular = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, __('tree'));
+ plural = __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
+ should.equal(singular, 'Im Baum sitzt ein Affe');
+ should.equal(plural, 'Im Baum sitzen 3 Affen');
+ });
+
+ it('won\'t return substitutions when not masked by an extra % (%% issue #49)', function () {
+ i18n.setLocale('en');
+ var singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, __('tree')),
+ plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, __('tree'));
+ should.equal(singular, 'There is one monkey in the 1');
+ should.equal(plural, 'There are 3 monkeys in the undefined');
+
+ i18n.setLocale('de');
+ singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, __('tree'));
+ plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, __('tree'));
+ should.equal(singular, 'There is one monkey in the 1');
+ should.equal(plural, 'There are 3 monkeys in the undefined');
+ });
});
-
});
- describe('translating variables values', function(){
- it('should work as expected (to lazy to change from expresso)', function(){
- var i = 0,
- greetings = ['Hi', 'Hello', 'Howdy'],
- greetingsDE = ['Hi', 'Hallo', 'Hallöchen'];
-
- i18n.setLocale('en');
- for (i = 0; i < greetings.length; i++) {
- should.equal(greetings[i], __(greetings[i]));
- }
-
- i18n.setLocale('de');
- for (i = 0; i < greetings.length; i++) {
- should.equal(greetingsDE[i], __(greetings[i]));
- }
+
+ describe('Local Scope', function () {
+ var req = {
+ "request": "GET /test",
+ __: i18n.__,
+ __n: i18n.__n,
+ };
+ describe('i18nSetLocale and i18nGetLocale', function () {
+ it('should return the current local setting, when used with 2 args', function () {
+ i18n.setLocale(req, 'en').should.equal('en');
+ });
+
+ it('while getLocale should still return the previous global setting', function () {
+ i18n.getLocale().should.equal('de');
+ });
+
+ it('now getLocale should return local locale when used with local object as 1st arg', function () {
+ i18n.getLocale(req).should.equal('en');
+ });
+ });
+
+ describe('i18nTranslate', function () {
+ it('has to use local translation in en', function () {
+ i18n.setLocale(req, 'en').should.equal('en');
+ req.__('Hello').should.equal('Hello');
+ });
+
+ it('while the global translation remains untouched', function () {
+ should.equal(__('Hello'), 'Hallo');
+ });
+
+ it('and has to use local translation in de', function () {
+ i18n.setLocale(req, 'de').should.equal('de');
+ req.__('Hello').should.equal('Hallo');
+ });
+
+ it('still the global translation remains untouched', function () {
+ should.equal(__('Hello'), 'Hallo');
+ });
+ });
+
+ describe('i18nTranslatePlural', function () {
+ it('should return singular or plural form based on last parameter', function () {
+ i18n.setLocale(req, 'en');
+ var singular = req.__n('%s cat', '%s cats', 1),
+ plural = req.__n('%s cat', '%s cats', 3);
+ should.equal(singular, '1 cat');
+ should.equal(plural, '3 cats');
+
+ i18n.setLocale(req, 'de');
+ singular = req.__n('%s cat', '%s cats', 1);
+ plural = req.__n('%s cat', '%s cats', 3);
+ should.equal(singular, '1 Katze');
+ should.equal(plural, '3 Katzen');
+ });
+
+ it('should return substituted phrases when used nested', function () {
+ i18n.setLocale(req, 'en');
+ var singular = req.__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, req.__('tree')),
+ plural = req.__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, req.__('tree'));
+ should.equal(singular, 'There is one monkey in the tree');
+ should.equal(plural, 'There are 3 monkeys in the tree');
+
+ i18n.setLocale(req, 'de');
+ singular = req.__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 1, req.__('tree'));
+ plural = req.__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, req.__('tree'));
+ should.equal(singular, 'Im Baum sitzt ein Affe');
+ should.equal(plural, 'Im Baum sitzen 3 Affen');
+ });
+
+ it('won\'t return substitutions when not masked by an extra % (%% issue #49)', function () {
+ i18n.setLocale(req, 'en');
+ var singular = req.__n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, req.__('tree')),
+ plural = req.__n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, req.__('tree'));
+ should.equal(singular, 'There is one monkey in the 1');
+ should.equal(plural, 'There are 3 monkeys in the undefined');
+
+ i18n.setLocale(req, 'de');
+ singular = req.__n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, req.__('tree'));
+ plural = req.__n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, req.__('tree'));
+ should.equal(singular, 'There is one monkey in the 1');
+ should.equal(plural, 'There are 3 monkeys in the undefined');
+ });
});
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.