i18n.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var path = require('path');
  2. var assert = require('assert');
  3. var I18n = require('../');
  4. describe('I18n', function() {
  5. var i18n = I18n();
  6. describe('.load', function() {
  7. it('should read from directory', function() {
  8. i18n.load(path.resolve(__dirname, 'locales'));
  9. assert.deepEqual(i18n.locales(), ['en', 'fr']);
  10. });
  11. });
  12. describe('.set', function() {
  13. it('should extend locales', function() {
  14. i18n.set({
  15. 'en-gb': {
  16. 'HELLO': 'Hello Sir {{name}}'
  17. }
  18. });
  19. assert.deepEqual(i18n.locales(), ['en', 'fr', 'en-gb']);
  20. });
  21. });
  22. describe('.resolve', function() {
  23. it('should resolve non-existing locales', function() {
  24. assert.equal(i18n.resolve('en-us'), 'en');
  25. assert.equal(i18n.resolve('fr-ca'), 'fr');
  26. });
  27. it('should resolve existing locales', function() {
  28. assert.equal(i18n.resolve('en-gb'), 'en-gb');
  29. assert.equal(i18n.resolve('en'), 'en');
  30. });
  31. it('should default to en', function() {
  32. assert.equal(i18n.resolve('zh'), 'en');
  33. });
  34. });
  35. describe('.t', function() {
  36. it('should translate without args', function() {
  37. assert.equal(i18n.t('en', 'WORLD'), 'World');
  38. });
  39. it('should translate with kwargs', function() {
  40. assert.equal(i18n.t('en', 'HELLO', { name: 'Samy' }), 'Hello Samy');
  41. });
  42. it('should translate with args', function() {
  43. assert.equal(i18n.t('en', 'CATS', 10), '10 cats');
  44. });
  45. it('should default non-existing locales to en', function() {
  46. assert.equal(i18n.t('zh', 'WORLD'), 'World');
  47. });
  48. it('should default non-existing translation to en', function() {
  49. assert.equal(i18n.t('fr', 'CATS', 10), '10 cats');
  50. });
  51. });
  52. });