LocaleProvider国际化 - 图1

LocaleProvider 国际化

基本用法

  1. import { Cell, LocaleProvider, Button, SearchBar, Modal, Select } from 'zarm';
  2. const locales = {
  3. 'en_US': {
  4. locale: 'en',
  5. SearchBar: {
  6. placeholder: 'Search',
  7. cancelText: 'Cancel',
  8. },
  9. Alert: {
  10. cancelText: 'Close',
  11. },
  12. Confirm: {
  13. cancelText: 'Cancel',
  14. okText: 'OK',
  15. },
  16. Select: {
  17. placeholder: 'please select',
  18. },
  19. Picker: {
  20. cancelText: 'Cancel',
  21. okText: 'OK',
  22. title: 'please select',
  23. }
  24. },
  25. 'zh_CN': {
  26. locale: 'zh_cn',
  27. SearchBar: {
  28. placeholder: '搜索',
  29. cancelText: '取消',
  30. },
  31. Alert: {
  32. cancelText: '关闭',
  33. },
  34. Confirm: {
  35. cancelText: '取消',
  36. okText: '确定',
  37. },
  38. Select: {
  39. placeholder: '请选择',
  40. },
  41. Picker: {
  42. cancelText: '取消',
  43. okText: '确定',
  44. title: '请选择',
  45. }
  46. },
  47. };
  48. class Demo extends React.Component {
  49. state = {
  50. locale: 'zh_CN',
  51. };
  52. show = (key) => {
  53. if (key === 'alert') {
  54. Modal.alert({
  55. title: '警告',
  56. content: '这里是警告信息',
  57. shape: 'radius'
  58. });
  59. } else {
  60. Modal.confirm({
  61. title: '确认信息',
  62. content: '你确定要这样做吗?',
  63. shape: 'radius'
  64. })
  65. }
  66. }
  67. onOk = (selected) => {
  68. this.setState({
  69. locale: selected[0].value,
  70. });
  71. }
  72. render() {
  73. return (
  74. <LocaleProvider locale={locales[this.state.locale]}>
  75. <div>
  76. <Cell title="切换语言包">
  77. <Select
  78. value={this.state.locale}
  79. dataSource={[
  80. { value: 'zh_CN', label: '中文' },
  81. { value: 'en_US', label: 'English' },
  82. ]}
  83. onOk={this.onOk}
  84. />
  85. </Cell>
  86. <SearchBar />
  87. <Cell
  88. description={
  89. <Button size="xs" onClick={() => this.show('alert')}>开启</Button>
  90. }
  91. >
  92. 警告框 Alert
  93. </Cell>
  94. <Cell
  95. description={
  96. <Button size="xs" onClick={() => this.show('confirm')}>开启</Button>
  97. }
  98. >
  99. 确认框 Confirm
  100. </Cell>
  101. </div>
  102. </LocaleProvider>
  103. )
  104. }
  105. }
  106. ReactDOM.render(<Demo />, mountNode);

API

属性类型默认值说明
localeObject-语言包配置,默认为中文,语言包可到 zarm/lib/locale-provider/locale 目录下寻找