如果翻译在 Lingui、React 中不可用,请使用默认/后备值

问题描述 投票:0回答:1

我有以下代码:

  const message = msg({
    context: "test",
    id: "username",
    message: t`Your name is: ${name}`,
  });

问题是,当我运行

npm run messages:extract
时,如果翻译不适用于所选语言,它只会返回
id
,而不是使用
message
作为某种默认值。

另外,我应该如何呈现消息?

<>{i18n._(tesztuzenet)}</>

<>{message.message}</>

有什么不同吗?

reactjs internationalization translation linguijs
1个回答
0
投票

当 Lingui 中没有翻译时,要处理后备值,您可以使用 msg 函数提供的默认选项。这允许您在翻译丢失时指定后备值。像这样的东西,

const message = msg({
  id: "username",
  message: t`Your name is: ${name}`,
  defaults: "Your name is: {name}", // Specify a fallback value here
});

如果给定上下文和 ID 的翻译不可用,它将回退到提供的默认消息。
关于如何呈现消息,您提供的两个选项都是有效的,但它们的用途略有不同。

// This renders the message using the i18n object directly. It's useful if you need to access translations directly from the i18n instance.
<>{i18n._(tesztuzenet)}</> 

// This renders the message using the message object obtained from msg function. 
// It's useful if you already have the message object and want to directly render it.
<>{message.message}</>
© www.soinside.com 2019 - 2024. All rights reserved.