i18n 因缺少翻译而回退到 key

发布于 2025-01-17 01:05:58 字数 907 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2025-01-24 01:05:59

对于不存在的翻译,我想要做的是显示密钥,而不是空字符串。

如果您确实有一个以空字符串作为值的翻译条目,则不会将其视为缺失,除非您 returnEmptyString 设置为 false

What I want to do, for translations that do not exist, is to show the key, instead of an empty string.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文