[英]node.js i18n - Cannot read property 'toLowerCase' of undefined
調用i18n.__n('..')
時,總是收到以下錯誤:
TypeError: Cannot read property 'toLowerCase' of undefined
at Object.i18nTranslatePlural [as __n] (/home/runner/node_modules/i18n/i18n.js:367:31)
at evalmachine.<anonymous>:14:18
at Script.runInContext (vm.js:74:29)
at Object.runInContext (vm.js:182:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:180:13)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at ReadStream.Readable.push (_stream_readable.js:218:10)
順便說一句, i18n.__('..')
就像一個吊飾!
這是代碼:
index.js
var i18n = require("i18n");
var path = require('path');
var __dirname = path.resolve();
i18n.configure({
locales:['en', 'de'],
directory: __dirname + '/locales',
defaultLocale: 'de',
});
console.log(i18n.__('test'));
console.log(i18n.__n('%s horse', 3));
區域設置/ de.json
{
"test": "Das ist ein Test",
"%s horse" : {
"one": "%s Pferd",
"other": "%s Pferde"
}
}
區域設置/ en.json
{
"test": "This is a test",
"%s horse" : {
"one": "%s horse",
"other": "%s horses"
}
}
希望有人能給我一些建議,我做錯了什么或如何解決該問題。 我在Mac電腦書和https://repl.it/languages/nodejs上運行了代碼。 結果相同。
似乎在調用__n
時庫在查找正確的語言環境方面遇到了麻煩。 只需設置i18n.setLocale('de')
。 如下所示的代碼將輸出
Das ist ein Test
1 Pferd
3 Pferde
我期望defaultLocale就足夠了,但似乎還不夠。 希望這個答案有幫助!
var i18n = require("i18n");
var path = require('path');
var __dirname = path.resolve();
i18n.configure({
locales:['en', 'de'],
directory: __dirname + '/locales',
defaultLocale: 'de'
});
i18n.setLocale('de')
console.log(i18n.__('test'));
console.log(i18n.__n("%s horse", 1));
console.log(i18n.__n("%s horse", 3));
僅供參考,我通過在configure()
調用中注冊一些調試功能來調試它
logDebugFn: console.log,
logWarnFn: console.log,
logErrorFn: console.log
然后,在沒有setLocale()
調用的情況下,我得到了以下WARN: No locale found - check the context of the call to __(). Using de as current locale
WARN: No locale found - check the context of the call to __(). Using de as current locale
。 在i18n
對象上強制使用當前語言環境可以解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.