[英]Using two language codes for same locale in i18n
I am utilizing i18n to render translations English/Japanese -- My default language is English in en
.我正在使用 i18n 翻译英语/日语——我的默认语言是en
中的英语。 but recently realized my website is not using the correct ISO language code for Japanese.但最近意识到我的网站没有使用正确的日语 ISO 语言代码。 jp
instead of ja
. jp
而不是ja
。
I am wondering if it's possible to have two language codes in i18n reference the same locale files -- and perplexed how to do this.我想知道是否有可能在 i18n 中有两种语言代码引用相同的语言环境文件——并且困惑如何做到这一点。
For example, I'd like /whatever?lng=jp
and /whatever?lng=ja
to work for Japanese text.例如,我希望/whatever?lng=jp
和/whatever?lng=ja
适用于日文文本。
Here are my configs:这是我的配置:
i18nConfig.js i18nConfig.js
const path = require("path");
module.exports = {
fallbackLng: "en",
preload: ["en", "ja", "jp"],
ns: [
"localefiles"
],
backend: {
loadPath: path.join(__dirname, "locales", "{{lng}}", "{{ns}}.json"),
addPath: path.join(__dirname, "locales", "{{lng}}", "{{ns}}.missing.json"),
},
};
i18n.js i18n.js
const i18n = require("i18next");
const XHR = require("i18next-xhr-backend");
const LanguageDetector = require("i18next-browser-languagedetector");
const options = {
fallbackLng: "en",
load: "languageOnly",
ns: ["common"],
defaultNS: "common",
debug: process.env.NODE_ENV !== "production" && false,
saveMissing: true,
interpolation: {
escapeValue: false,
formatSeparator: ",",
format: (value, format) => {
if (format === "uppercase") {
return value.toUpperCase();
}
return value;
},
},
};
if (process.browser) {
i18n.use(XHR).use(LanguageDetector);
}
if (!i18n.isInitialized) {
i18n.init(options);
}
if (i18n.initialLanguage == "jp") {
console.log({i18n})
i18n.setLng("ja");
}
i18n.getInitialProps = (req, namespaces) => {
if (!namespaces) {
namespaces = i18n.options.defaultNS;
}
if (typeof namespaces === "string") {
namespaces = [namespaces];
}
req.i18n.toJSON = () => null;
const initialI18nStore = {};
req.i18n.languages.forEach((lan) => {
initialI18nStore[lan] = {};
namespaces.forEach((ns) => {
initialI18nStore[lan][ns] = req.i18n.services.resourceStore.data[lan]
? req.i18n.services.resourceStore.data[lan][ns] || {}
: {};
});
});
return {
i18n: req.i18n,
initialI18nStore,
initialLanguage: req.i18n.language,
};
};
module.exports = i18n;
Thanks so much for help on this basic question.非常感谢您对这个基本问题的帮助。
You can define backend.loadPath
as a function which gets langs
& namespace
and returns a string with the path to the file.您可以将backend.loadPath
定义为function ,它获取langs
和namespace
并返回带有文件路径的字符串。
// i18next.js
const i18n = require('i18next');
const XHR = require('i18next-xhr-backend');
const LanguageDetector = require('i18next-browser-languagedetector');
const options = {
fallbackLng: 'en',
load: 'languageOnly',
ns: ['common'],
defaultNS: 'common',
debug: process.env.NODE_ENV !== 'production' && false,
saveMissing: true,
backend: {
loadPath(lng, ns) {
if (lng === 'jp') { // <---
return '/locales/ja/{{ns}}.json';
}
return '/locales/{{lng}}/{{ns}}.json';
},
addPath: path.join(__dirname, 'locales', '{{lng}}', '{{ns}}.missing.json'),
},
interpolation: {
escapeValue: false,
formatSeparator: ',',
format: (value, format) => {
if (format === 'uppercase') {
return value.toUpperCase();
}
return value;
},
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.