diff --git a/lib/hash/index.js b/lib/hash/index.js index 523c98e..3baed26 100644 --- a/lib/hash/index.js +++ b/lib/hash/index.js @@ -9,8 +9,9 @@ } }(this, function(exports) { - exports.sha1 = require('./sha1'); exports.crc32 = require('./crc32'); + exports.md5 = require('./md5'); + exports.sha1 = require('./sha1'); return exports; })); diff --git a/lib/hash/md5.js b/lib/hash/md5.js new file mode 100644 index 0000000..4b80762 --- /dev/null +++ b/lib/hash/md5.js @@ -0,0 +1 @@ +module.exports = require('MD5'); diff --git a/package.json b/package.json index bcc2545..1e44208 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "i18next-text", - "version": "0.3.1", + "version": "0.4.0", "description": "Using i18next translations without having the `key` as strings, you do not need to worry about i18n key naming.", "homepage": "http://cheton.github.io/i18next-text/", "author": "Cheton Wu ", @@ -29,9 +29,11 @@ "i18n", "hash", "sha1", - "crc32" + "crc32", + "md5" ], "dependencies": { + "MD5": "^1.2.1", "crc-32": "^0.2.2", "i18next": "^1.7.10", "sha1": "^1.1.0" diff --git a/test/fixtures/i18n/de/resource.json b/test/fixtures/i18n/de/resource.json index 54461c3..1399d4d 100644 --- a/test/fixtures/i18n/de/resource.json +++ b/test/fixtures/i18n/de/resource.json @@ -1,5 +1,6 @@ { "loading": "Wird geladen...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "Wird geladen...", - "cd643ef3": "Wird geladen..." + "cd643ef3": "Wird geladen...", + "8524de963f07201e5c086830d370797f": "Wird geladen...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "Wird geladen..." } diff --git a/test/fixtures/i18n/en/resource.json b/test/fixtures/i18n/en/resource.json index 2dd19ff..4faec94 100644 --- a/test/fixtures/i18n/en/resource.json +++ b/test/fixtures/i18n/en/resource.json @@ -1,5 +1,6 @@ { "loading": "Loading...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "Loading...", - "cd643ef3": "Loading..." + "cd643ef3": "Loading...", + "8524de963f07201e5c086830d370797f": "Loading...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "Loading..." } diff --git a/test/fixtures/i18n/es/resource.json b/test/fixtures/i18n/es/resource.json index 22e6a1f..c8af1a3 100644 --- a/test/fixtures/i18n/es/resource.json +++ b/test/fixtures/i18n/es/resource.json @@ -1,5 +1,6 @@ { "loading": "Cargando...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "Cargando...", - "cd643ef3": "Cargando..." + "cd643ef3": "Cargando...", + "8524de963f07201e5c086830d370797f": "Cargando...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "Cargando..." } diff --git a/test/fixtures/i18n/fr/resource.json b/test/fixtures/i18n/fr/resource.json index b12f5cd..d21f36b 100644 --- a/test/fixtures/i18n/fr/resource.json +++ b/test/fixtures/i18n/fr/resource.json @@ -1,5 +1,6 @@ { "loading": "Chargement...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "Chargement...", - "cd643ef3": "Chargement..." + "cd643ef3": "Chargement...", + "8524de963f07201e5c086830d370797f": "Chargement...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "Chargement..." } diff --git a/test/fixtures/i18n/it/resource.json b/test/fixtures/i18n/it/resource.json index 86229c9..76786d6 100644 --- a/test/fixtures/i18n/it/resource.json +++ b/test/fixtures/i18n/it/resource.json @@ -1,5 +1,6 @@ { "loading": "Caricamento in corso...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "Caricamento in corso...", - "cd643ef3": "Caricamento in corso..." + "cd643ef3": "Caricamento in corso...", + "8524de963f07201e5c086830d370797f": "Caricamento in corso...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "Caricamento in corso..." } diff --git a/test/fixtures/i18n/ja/resource.json b/test/fixtures/i18n/ja/resource.json index 9ac3720..50a9840 100644 --- a/test/fixtures/i18n/ja/resource.json +++ b/test/fixtures/i18n/ja/resource.json @@ -1,5 +1,6 @@ { "loading": "ロード中...", - "b04ba49f848624bb97ab094a2631d2ad74913498": "ロード中...", - "cd643ef3": "ロード中..." + "cd643ef3": "ロード中...", + "8524de963f07201e5c086830d370797f": "ロード中...", + "b04ba49f848624bb97ab094a2631d2ad74913498": "ロード中..." } diff --git a/test/main.js b/test/main.js index 5def933..9a9a3c6 100755 --- a/test/main.js +++ b/test/main.js @@ -58,112 +58,69 @@ test('i18next initialization', function(t) { }); }); -test('sha1', function(t) { +test('hash function', function(t) { i18n.init(i18nextOptions, function() { - var str = 'Loading...'; - var expectedKey = 'b04ba49f848624bb97ab094a2631d2ad74913498'; - - var sha1 = require('../lib/hash/sha1'); - t.equal(expectedKey, sha1(str), 'The value of sha1(' + JSON.stringify(str) + ') should be equal to ' + expectedKey); - - text.init({hash: 'sha1'}); - - // Test for existence of a key - t.ok(i18n.exists(expectedKey), 'This key should exist.'); - - // English - i18n.setLng('en'); - t.equal('Loading...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // German - i18n.setLng('de'); - t.equal('Wird geladen...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // French - i18n.setLng('fr'); - t.equal('Chargement...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Spanish - i18n.setLng('es'); - t.equal('Cargando...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Italian - i18n.setLng('it'); - t.equal('Caricamento in corso...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Japanese - i18n.setLng('ja'); - t.equal('ロード中...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Not exists - str = 'This value does not exist.'; - expectedKey = sha1(str); - t.notOk(i18n.exists(expectedKey)); - t.notOk(text.exists(str)); - t.assert(expectedKey === text.key(str)); - t.equal(str, i18n._(str)); - - t.end(); - }); -}); - -test('crc32', function(t) { - i18n.init(i18nextOptions, function() { - var str = 'Loading...'; - var expectedKey = 'cd643ef3'; - - var crc32 = require('../lib/hash/crc32'); - t.equal(expectedKey, crc32(str), 'The value of crc32(' + JSON.stringify(str) + ') should be equal to ' + expectedKey); - - text.init({hash: 'crc32'}); - - // Test for existence of a key - t.ok(i18n.exists(expectedKey), 'This key should exist.'); - - // English - i18n.setLng('en'); - t.equal('Loading...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // German - i18n.setLng('de'); - t.equal('Wird geladen...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // French - i18n.setLng('fr'); - t.equal('Chargement...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Spanish - i18n.setLng('es'); - t.equal('Cargando...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Italian - i18n.setLng('it'); - t.equal('Caricamento in corso...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Japanese - i18n.setLng('ja'); - t.equal('ロード中...', i18n._(str)); - t.equal(expectedKey, text.key(str)); - - // Not exists - str = 'This value does not exist.'; - expectedKey = crc32(str); - t.notOk(i18n.exists(expectedKey)); - t.notOk(text.exists(str)); - t.assert(expectedKey === text.key(str)); - t.equal(str, i18n._(str)); - - t.end(); + var runTests = function(t, hashMethod) { + var hash = require('../lib/hash'); + var str = 'Loading...'; + var expectedKey = hash[hashMethod](str); + + text.init({hash: hashMethod}); + + // Test for existence of a key + t.ok(i18n.exists(expectedKey), 'This key should exist.'); + + // English + i18n.setLng('en'); + t.equal('Loading...', i18n._(str), 'English translation should be \'Loading...\''); + t.equal(expectedKey, text.key(str)); + + // German + i18n.setLng('de'); + t.equal('Wird geladen...', i18n._(str), 'German translation should be \'Wird geladen...\''); + t.equal(expectedKey, text.key(str)); + + // French + i18n.setLng('fr'); + t.equal('Chargement...', i18n._(str), 'French translation should be \'Chargement...\''); + t.equal(expectedKey, text.key(str)); + + // Spanish + i18n.setLng('es'); + t.equal('Cargando...', i18n._(str), 'Spanish translation should be \'Cargando...\''); + t.equal(expectedKey, text.key(str)); + + // Italian + i18n.setLng('it'); + t.equal('Caricamento in corso...', i18n._(str), 'Italian translation should be \'Caricamento in corso...\''); + t.equal(expectedKey, text.key(str)); + + // Japanese + i18n.setLng('ja'); + t.equal('ロード中...', i18n._(str), 'Japanese translation should be \'ロード中...\''); + t.equal(expectedKey, text.key(str)); + + // Not exists + str = 'This value does not exist.'; + expectedKey = hash[hashMethod](str); + t.notOk(i18n.exists(expectedKey)); + t.notOk(text.exists(str)); + t.assert(expectedKey === text.key(str)); + t.equal(str, i18n._(str)); + + t.end(); + }; + + t.test('crc32', function(t) { + runTests(t, 'crc32'); + }); + + t.test('sha1', function(t) { + runTests(t, 'sha1'); + }); + + t.test('md5', function(t) { + runTests(t, 'md5'); + }); }); });