javascript - 如何使用 react-native-i18n 翻译数组?

标签 javascript react-native

我的翻译是这样的:

zh.json

{
   "Welcome": "Welcome to react native",
   "days": ["Sun","Mon","Tue","Wed","Thur","Fri","Sat"]
}

我的页面是这样的:

import React, { PropTypes } from 'react';
import { View, Image } from 'react-native';
import i18n from 'react-native-i18n';
import ReactNativeCalendar from 'react-native-calendar';
import translations from '../../translations';
import styles from './styles';

const dayHeadings = i18n.t('days');

[...]

const Calendar = (props) => {
  console.log(dayHeadings);
  return (
    <View>
      <ReactNativeCalendar
        customStyle={styles}
        nextButtonText={nextButton}
        prevButtonText={prevButton}
        dayHeadings={dayHeadings}
        monthNames={monthNames}
        onDateSelect={props.onDateSelect}
        eventDates={props.eventDates}
        showEventIndicators
        showControls
        events={props.events}
        calendarFormat={props.calendarFormat}
      />
      <Legend items={legendItems} />
    </View>
  );
};

dayHeadings 应该是翻译后的字符串数组,但我却得到了

missing "en.days" translation

从控制台。奇怪的是,如果我保存翻译并触发热重载,翻译工作就好了 like here .文件中也有翻译 screenshot

最佳答案

最好的方法是声明:

{
   "Welcome": "Welcome to React Native",
   "days": {
    "sun": "Sun",
    "mon": "Mon",
    "tue": "Tue",
    "wed": "Wed",
    "thu": "Thur",
    "fri": "Fri",
    "sat": "Sat"
  }
}

然后引用它:

I18n.t(['days', 'mon']);

假设您想要显示所有日期(例如在日历中),您可以:

const dayKeys = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
...
render() {
  return (
    <View>
    {dayKeys.map((key) => <Text>{I18n.t(['days', key])}</Text>)}
    </View>
  )
}

关于javascript - 如何使用 react-native-i18n 翻译数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007617/

相关文章:

javascript - 如何禁用多个值以逗号分隔的选项?

javascript - 如何将音频片段从react-native上传到php后端?

android - 新鲜的 React-Native 项目显示白屏

ios - 在 react native 应用程序中使用提取时出错

javascript - 错误 : bundling failed: TypeError: Cannot read property 'bindings' of null

javascript - Controller 内的 AngularJS Controller

javascript - Adobe 空气 : draw vector graphics

javascript - Array.reduce 和递归

react-native - 使用 React Native Gesture Handler 和 Reanimated 检测滑动方向

javascript - 如何缓慢移动 DOM 元素?