Skip to content

Commit

Permalink
Adds md5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheton Wu (RD-TW) committed Mar 16, 2015
1 parent ce627bb commit fa30ab4
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 121 deletions.
3 changes: 2 additions & 1 deletion lib/hash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));
1 change: 1 addition & 0 deletions lib/hash/md5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('MD5');
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <cheton@gmail.com>",
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/i18n/de/resource.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loading": "Wird geladen...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Wird geladen...",
"cd643ef3": "Wird geladen..."
"cd643ef3": "Wird geladen...",
"8524de963f07201e5c086830d370797f": "Wird geladen...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Wird geladen..."
}
5 changes: 3 additions & 2 deletions test/fixtures/i18n/en/resource.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loading": "Loading...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Loading...",
"cd643ef3": "Loading..."
"cd643ef3": "Loading...",
"8524de963f07201e5c086830d370797f": "Loading...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Loading..."
}
5 changes: 3 additions & 2 deletions test/fixtures/i18n/es/resource.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loading": "Cargando...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Cargando...",
"cd643ef3": "Cargando..."
"cd643ef3": "Cargando...",
"8524de963f07201e5c086830d370797f": "Cargando...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Cargando..."
}
5 changes: 3 additions & 2 deletions test/fixtures/i18n/fr/resource.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loading": "Chargement...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Chargement...",
"cd643ef3": "Chargement..."
"cd643ef3": "Chargement...",
"8524de963f07201e5c086830d370797f": "Chargement...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "Chargement..."
}
5 changes: 3 additions & 2 deletions test/fixtures/i18n/it/resource.json
Original file line number Diff line number Diff line change
@@ -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..."
}
5 changes: 3 additions & 2 deletions test/fixtures/i18n/ja/resource.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"loading": "ロード中...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "ロード中...",
"cd643ef3": "ロード中..."
"cd643ef3": "ロード中...",
"8524de963f07201e5c086830d370797f": "ロード中...",
"b04ba49f848624bb97ab094a2631d2ad74913498": "ロード中..."
}
169 changes: 63 additions & 106 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit fa30ab4

Please sign in to comment.