Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created October 20, 2016 06:05
Show Gist options
  • Save nobeans/df4c260708e6f60eff01bde22187158b to your computer and use it in GitHub Desktop.
Save nobeans/df4c260708e6f60eff01bde22187158b to your computer and use it in GitHub Desktop.
var Backend = require('i18next-node-fs-backend');
//var Backend = require('i18next-sync-fs-backend');
var i18next = require('i18next');
var assert = require('power-assert');
var config = {
debug: true,
lng: 'en',
resources: {
en: {
translation: {
"key": "hello world",
"a.b.c": "Resolvable"
}
},
ja: {
translation: {
"key": "こんにちは",
"onlyJa": "日本語だけ",
"a.b.c": "解決可能"
}
}
},
nsSeparator: ':',
keySeparator: '.',
fallbackLng: "dev"
}
console.log(i18next);
console.log("---------------------------------------");
i18next.init(config);
console.log(i18next);
assert.equal(i18next.t('key'), "hello world");
assert.equal(i18next.t('not_found'), "not_found");
assert.equal(i18next.t('Key is not found.'), "Key is not found.");
assert.equal(i18next.t('onlyJa'), 'onlyJa');
assert.equal(i18next.t('a.b.c'), 'a.b.c');
console.log("---------------------------------------");
i18next.init(Object.assign({}, config, {
nsSeparator: false,
keySeparator: false
}));
console.log(i18next);
assert.equal(i18next.t('key'), "hello world");
assert.equal(i18next.t('not_found'), "not_found");
assert.equal(i18next.t('Key is not found.'), "Key is not found.");
assert.equal(i18next.t('onlyJa'), 'onlyJa');
assert.equal(i18next.t('a.b.c'), 'Resolvable');
console.log("---------------------------------------");
i18next.init(Object.assign({}, config, {
fallbackLng: "ja",
}));
console.log(i18next);
assert.equal(i18next.t('key'), "hello world");
assert.equal(i18next.t('not_found'), "not_found");
assert.equal(i18next.t('Key is not found.'), "Key is not found.");
assert.equal(i18next.t('onlyJa'), '日本語だけ');
assert.equal(i18next.t('a.b.c'), 'a.b.c');
console.log("---------------------------------------");
i18next.init(Object.assign({}, config, {
resources: {
en: {
translation: {
"key": "hello world",
"a.b.c": "Resolvable",
"array": ["aaa", "bbb", 111, 222],
"using_array_params": "hello world {{0}} {{1}} {{2}}",
"using_object_params": "hello world {{a}} {{b}} {{c}}",
"using_other_message": '>>$t(key)<<',
"using_other_message_with_params": "==$t(using_object_params, {\"a\": \"hoge\", \"b\": {{b+2}}, \"c\": \"CCC\")==",
}
},
ja: {
translation: {
"key": "こんにちは",
"onlyJa": "日本語だけ",
"a.b.c": "解決可能"
}
}
},
joinArrays: "\n" // これを定義すると配列をjoinした文字列を返してくれる。複数行のメッセージに良いかも
}));
console.log(i18next);
assert.equal(i18next.t('key'), "hello world");
assert.equal(i18next.t('not_found'), "not_found");
assert.equal(i18next.t('Key is not found.'), "Key is not found.");
assert.equal(i18next.t('onlyJa'), 'onlyJa');
assert.equal(i18next.t('a.b.c'), 'a.b.c');
assert.equal(i18next.t('array'), "aaa\nbbb\n111\n222");
assert.equal(i18next.t('using_array_params', ['and', 'good-bye', 'world']), 'hello world and good-bye world');
assert.equal(i18next.t('using_object_params', {a: 'and', b: 'good-bye', c: 'world'}), 'hello world and good-bye world');
assert.equal(i18next.t('using_object_params', {a: 'and', c: 'world'}), 'hello world and world');
assert.equal(i18next.t('using_other_message'), ">>hello world<<");
assert.equal(i18next.t('using_other_message_with_params', {a: "AAA", b: "BBB"}), "==hello world AAA BBB =="); // 複雑なことはしない方が良さそう
return
console.log("---------------------------------------");
// electronに組み込むとロードできるのに、なぜかここではbackendがスルーされてる
i18next
.use(Backend)
.init(Object.assign({}, config, {
resources: {},
fallbackLng: "ja",
backend: {
loadPath: "/____src/locales/{{lng}}/{{ns}}.json"
}
}), function(err, t) {
console.log("Initialized i18n", err, t);
console.log(i18next.t('key'));
console.log(i18next.t('keyEscaped'));
});
console.log('i18next is initialized????');
console.log(i18next.t("key"))
// TODO なぜロードエラーにならない!!!!!!
return;
console.log("---------------------------------------");
i18next.use(Backend).init(Object.assign({}, config, {
//debug: (process.env.NODE_ENV === 'development'),
resources: {},
debug: true,
lng: "en",
fallbackLng: "en",
backend: {
loadPath: __dirname + "/locales/{{lng}}/{{ns}}.json"
}
}), (err) => {
console.log('i18next is initialized:', err);
console.log(i18next.t("key"));
assert.equal(i18next.t("key"), "hello world");
});
console.log('i18next is initialized????');
console.log(i18next.t("key"))
assert.equal(i18next.t("key"), "hello world");
console.log("------------------------------------------");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment