如何通过I18Next传递参数?

发布于 2025-01-22 17:04:21 字数 1388 浏览 3 评论 0原文

我想翻译我的颜色,但是我是如何做的。

const Item = memo(({ color, index, lastIndex, translateName, style, activeColor, onPress }: IColorItem) => {
  return (
      <Text>{translateName}</Text>
  )
  // @ts-ignore
}, isEq);

const Colors = ({ colors, style, activeColor, onPress }: IColors) => {
  const { t } = useTranslation();
  const renderItem: ListRenderItem<ColorsType> = ({ item, index }) => (
    <Item
      color={item}
      translateName={t('colors.color', { color: item })}
      index={index}
      style={style}
      activeColor={activeColor}
      lastIndex={colors.length - 1}
      onPress={onPress}
    />
  )
  return (
    <View style={s.container}>
      <FlatList
        data={colors as ColorsType[]}
        renderItem={renderItem}
        style={s.flatList}
        keyExtractor={(item, i) => i.toString()}
        horizontal
        showsHorizontalScrollIndicator={false}
      />
    </View>
  )
}

transl.json

  "colors": {
     "color": "{{color, COLORS}}"
  },

没什么可用。我怎么说“红色” = ...,“蓝色” = ...?

................................................................................. ................................................................................. ....................................................................................

I want to translate my colors but idk how I do it.

const Item = memo(({ color, index, lastIndex, translateName, style, activeColor, onPress }: IColorItem) => {
  return (
      <Text>{translateName}</Text>
  )
  // @ts-ignore
}, isEq);

const Colors = ({ colors, style, activeColor, onPress }: IColors) => {
  const { t } = useTranslation();
  const renderItem: ListRenderItem<ColorsType> = ({ item, index }) => (
    <Item
      color={item}
      translateName={t('colors.color', { color: item })}
      index={index}
      style={style}
      activeColor={activeColor}
      lastIndex={colors.length - 1}
      onPress={onPress}
    />
  )
  return (
    <View style={s.container}>
      <FlatList
        data={colors as ColorsType[]}
        renderItem={renderItem}
        style={s.flatList}
        keyExtractor={(item, i) => i.toString()}
        horizontal
        showsHorizontalScrollIndicator={false}
      />
    </View>
  )
}

transl.json

  "colors": {
     "color": "{{color, COLORS}}"
  },

Nothing works. How can I say "red" = ..., "blue" = ... ?

................................................................................................................................................

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ι不睡觉的鱼゛ 2025-01-29 17:04:21

您可以在I18N文档中看到插值:“ noreferrer”> https://www.i18next.com/translation-function.com/translation-function.com/translation-function.com/translation-function. /插值

{
"key": "{{what}} is {{how}}"
}


i18next.t('key', { what: 'i18next', how: 'great' });

You can see in the i18n documentation the interpolation: https://www.i18next.com/translation-function/interpolation

{
"key": "{{what}} is {{how}}"
}


i18next.t('key', { what: 'i18next', how: 'great' });
怕倦 2025-01-29 17:04:21

您可以使用这种方式:

<Item
      color={item}
      translateName={t('colors.color', { color: 'blue' })}
      index={index}
      style={style}
      activeColor={activeColor}
      lastIndex={colors.length - 1}
      onPress={onPress}
    />

在您的JSON中,

  "colors": {
     "color": "my color is %{color}"
  },

输出应为:我的颜色是蓝色

you can use this way:

<Item
      color={item}
      translateName={t('colors.color', { color: 'blue' })}
      index={index}
      style={style}
      activeColor={activeColor}
      lastIndex={colors.length - 1}
      onPress={onPress}
    />

and in your json

  "colors": {
     "color": "my color is %{color}"
  },

the output should be: my color is blue

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