i18n 因缺少翻译而回退到 key
我的 vue3 项目中有这个文件:
// i18n.js
import { createI18n } from "vue3-i18n";
import en from "./assets/lang/en";
import ar from "./assets/lang/ar";
import tr from "./assets/lang/tr";
const messages = {
en,
ar,
tr
};
const i18n = createI18n({
locale: localStorage.getItem('lang')===null?'en':localStorage.getItem('lang'),
messages: messages,
});
export default i18n;
对于不存在的翻译,我想要做的是显示密钥,而不是空字符串。
所以我首先从 我在 SO 上发现的一个问题尝试了这个:
const i18n = createI18n({
locale: localStorage.getItem('lang')===null?'en':localStorage.getItem('lang'),
messages: messages,
parseMissingKeyHandler: (key: string) => {
return `No translation found for "${key}"`;
}
});
代码甚至没有编译,我尝试改变周围的东西但即使它符合要求,我也没有看到预期的消息来代替丢失的钥匙。
我如何在 vue3 中使用 i18n 回退到它们缺少翻译的密钥。
I have this file in my vue3 project:
// i18n.js
import { createI18n } from "vue3-i18n";
import en from "./assets/lang/en";
import ar from "./assets/lang/ar";
import tr from "./assets/lang/tr";
const messages = {
en,
ar,
tr
};
const i18n = createI18n({
locale: localStorage.getItem('lang')===null?'en':localStorage.getItem('lang'),
messages: messages,
});
export default i18n;
What I want to do, for translations that do not exist, is to show the key, instead of an empty string.
So I tried this at first from a question I found on SO:
const i18n = createI18n({
locale: localStorage.getItem('lang')===null?'en':localStorage.getItem('lang'),
messages: messages,
parseMissingKeyHandler: (key: string) => {
return `No translation found for "${key}"`;
}
});
The code did not even compile, I tried changing things around but even though it complied I did not see the intended message in place of the missing key.
How do I get i18n in vue3 fallback to they key for missing translations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确实有一个以空字符串作为值的翻译条目,则不会将其视为缺失,除非您 将
returnEmptyString
设置为 false。If you do have a translation entry with an empty string as the value, it won't be considered as missing unless you set
returnEmptyString
to false.