Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored tests, added setLocale / getLocale to register

  • Loading branch information...
commit 3d4df717695fce60fa805f047407aef1cdd5bf5c 1 parent 40bd4f4
@mashpie authored
Showing with 78 additions and 34 deletions.
  1. +19 −10 i18n.js
  2. +59 −24 test/i18n.test.js
View
29 i18n.js
@@ -33,6 +33,7 @@ i18n.configure = function i18nConfigure(opt) {
opt.register.__ = i18n.__;
opt.register.__n = i18n.__n;
opt.register.getLocale = i18n.getLocale;
+ opt.register.setLocale = i18n.setLocale;
}
// sets a custom cookie name to parse locale settings from
@@ -118,19 +119,21 @@ i18n.__n = function i18nTranslatePlural(singular, plural, count) {
return msg;
};
-// either gets called like
-// setLocale('en') or like
-// setLocale(req, 'en')
-i18n.setLocale = function i18nSetLocale(arg1, arg2) {
- var target_locale = arg1,
+i18n.setLocale = function i18nSetLocale(locale_or_request, locale) {
+ var target_locale = locale_or_request,
request;
-
- if (arg2 && locales[arg2]) {
- request = arg1;
- target_locale = arg2;
+ // called like setLocale(req, 'en')
+ if (locale_or_request && typeof locale === 'string' && locales[locale]) {
+ request = locale_or_request;
+ target_locale = locale;
+ }
+ // called like req.setLocale('en')
+ if (locale === undefined && typeof this.locale === 'string' && typeof locale_or_request === 'string') {
+ request = this;
+ target_locale = locale_or_request;
}
-
if (locales[target_locale]) {
+ // called like setLocale('en')
if (request === undefined) {
defaultLocale = target_locale;
}
@@ -142,9 +145,15 @@ i18n.setLocale = function i18nSetLocale(arg1, arg2) {
};
i18n.getLocale = function i18nGetLocale(request) {
+ // called like getLocale(req)
if (request && request.locale) {
return request.locale;
}
+ // called like req.getLocale()
+ if (request === undefined && typeof this.locale === 'string') {
+ return this.locale;
+ }
+ // called like getLocale()
return defaultLocale;
};
View
83 test/i18n.test.js
@@ -146,41 +146,76 @@ describe('Module API', function () {
"request": "GET /test",
__: i18n.__,
__n: i18n.__n,
+ "locale": {}
};
- 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');
- });
+ i18n.configure({
+ locales: ['en', 'de'],
+ register: req,
+ defaultLocale: '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');
+ describe('Object as parameter', function () {
+ 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');
+ });
});
-
- it('while the global translation remains untouched', function () {
- should.equal(__('Hello'), 'Hallo');
+ 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');
+ });
});
+ });
- it('and has to use local translation in de', function () {
- i18n.setLocale(req, 'de').should.equal('de');
- req.__('Hello').should.equal('Hallo');
+ describe('Attached to object', function () {
+ describe('i18nSetLocale and i18nGetLocale', function () {
+ it('should return the current local setting, when used with 1 arg', function () {
+ req.setLocale('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', function () {
+ req.getLocale().should.equal('en');
+ });
});
-
- it('still the global translation remains untouched', function () {
- should.equal(__('Hello'), 'Hallo');
+ describe('i18nTranslate', function () {
+ it('has to use local translation in en', function () {
+ req.setLocale('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 () {
+ req.setLocale('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');
Please sign in to comment.
Something went wrong with that request. Please try again.