@fe-free/core 4.0.2 → 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @fe-free/core
2
2
 
3
+ ## 4.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - feat: i18n
8
+ - @fe-free/icons@4.0.4
9
+ - @fe-free/tool@4.0.4
10
+
11
+ ## 4.0.3
12
+
13
+ ### Patch Changes
14
+
15
+ - @fe-free/icons@4.0.3
16
+ - @fe-free/tool@4.0.3
17
+
3
18
  ## 4.0.2
4
19
 
5
20
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fe-free/core",
3
- "version": "4.0.2",
3
+ "version": "4.0.4",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
@@ -45,9 +45,13 @@
45
45
  "@ant-design/pro-components": "2.8.9",
46
46
  "antd": "^5.27.1",
47
47
  "dayjs": "~1.11.10",
48
+ "i18next": "^25.7.2",
49
+ "i18next-browser-languagedetector": "^8.2.0",
50
+ "i18next-icu": "^2.4.1",
48
51
  "react": "^19.2.0",
49
- "@fe-free/icons": "4.0.2",
50
- "@fe-free/tool": "4.0.2"
52
+ "react-i18next": "^16.4.0",
53
+ "@fe-free/tool": "4.0.4",
54
+ "@fe-free/icons": "4.0.4"
51
55
  },
52
56
  "scripts": {
53
57
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,11 +1,14 @@
1
1
  import { ProConfigProvider } from '@ant-design/pro-components';
2
2
  import { useTitle } from 'ahooks';
3
3
  import { App, ConfigProvider } from 'antd';
4
+ import enUS from 'antd/locale/en_US';
4
5
  import zhCN from 'antd/locale/zh_CN';
5
6
  import classNames from 'classnames';
6
7
  import { merge } from 'lodash-es';
7
8
  import { useEffect, useMemo } from 'react';
9
+ import { useTranslation } from 'react-i18next';
8
10
  import { BrowserRouter as Router, useNavigate } from 'react-router-dom';
11
+ import { EnumLanguage, I18nProvider } from '../i18n';
9
12
  import { routeTool } from '../route';
10
13
  import { customValueTypeMap } from '../value_type_map';
11
14
  import { themeConfig } from './config';
@@ -94,8 +97,7 @@ function SetRouteTool({ basename }: { basename: string }) {
94
97
  return null;
95
98
  }
96
99
 
97
- /** 提供一些基础的组 APP 功能 */
98
- function CoreApp(props: {
100
+ interface CoreAppProps {
99
101
  basename: string;
100
102
  name?: string;
101
103
  enableCheckUpdate?: boolean;
@@ -107,7 +109,9 @@ function CoreApp(props: {
107
109
  customConfig?: {
108
110
  hiddenFormItemLabelColon?: boolean;
109
111
  };
110
- }) {
112
+ }
113
+ /** 提供一些基础的组 APP 功能 */
114
+ function CoreAppBase(props: CoreAppProps) {
111
115
  const {
112
116
  basename,
113
117
  name,
@@ -120,6 +124,8 @@ function CoreApp(props: {
120
124
  customConfig,
121
125
  } = props;
122
126
 
127
+ const { i18n } = useTranslation();
128
+
123
129
  useTitle(name || '');
124
130
 
125
131
  useEffect(() => {
@@ -133,6 +139,14 @@ function CoreApp(props: {
133
139
  return merge(themeConfig, configProviderProps?.theme);
134
140
  }, [configProviderProps?.theme]);
135
141
 
142
+ const locale = useMemo(() => {
143
+ if (configProviderProps?.locale) {
144
+ return configProviderProps.locale;
145
+ }
146
+
147
+ return i18n.language === EnumLanguage.EN_US ? enUS : zhCN;
148
+ }, [configProviderProps?.locale, i18n.language]);
149
+
136
150
  return (
137
151
  <ProConfigProvider
138
152
  {...proConfigProviderProps}
@@ -140,11 +154,7 @@ function CoreApp(props: {
140
154
  valueTypeMap={{ ...customValueTypeMap, ...proConfigProviderProps?.valueTypeMap }}
141
155
  >
142
156
  {/* 集成好 locale */}
143
- <ConfigProvider
144
- {...configProviderProps}
145
- locale={configProviderProps?.locale || zhCN}
146
- theme={theme}
147
- >
157
+ <ConfigProvider {...configProviderProps} locale={locale} theme={theme}>
148
158
  <App
149
159
  {...appProps}
150
160
  className={classNames('fec-app', appProps?.className, {
@@ -162,4 +172,12 @@ function CoreApp(props: {
162
172
  );
163
173
  }
164
174
 
175
+ function CoreApp(props: CoreAppProps) {
176
+ return (
177
+ <I18nProvider>
178
+ <CoreAppBase {...props} />
179
+ </I18nProvider>
180
+ );
181
+ }
182
+
165
183
  export { CoreApp };
@@ -32,6 +32,9 @@ export const Basic: Story = {
32
32
  title: 'id',
33
33
  dataIndex: 'id',
34
34
  search: true,
35
+ formItemProps: {
36
+ label: '这是label',
37
+ },
35
38
  },
36
39
  {
37
40
  title: '名字(省略)',
package/src/i18n.tsx ADDED
@@ -0,0 +1,63 @@
1
+ import i18n from 'i18next';
2
+ import LanguageDetector from 'i18next-browser-languagedetector';
3
+ import ICU from 'i18next-icu';
4
+ import { I18nextProvider, initReactI18next } from 'react-i18next';
5
+
6
+ enum EnumLanguage {
7
+ ZH_CN = 'zh-CN',
8
+ EN_US = 'en-US',
9
+ }
10
+
11
+ const valueEnumLanguage = {
12
+ [EnumLanguage.ZH_CN]: {
13
+ text: '中文',
14
+ value: EnumLanguage.ZH_CN,
15
+ },
16
+ [EnumLanguage.EN_US]: {
17
+ text: '英文',
18
+ value: EnumLanguage.EN_US,
19
+ },
20
+ };
21
+
22
+ const listLanguage = Object.keys(valueEnumLanguage).map((key) => {
23
+ const item = valueEnumLanguage[key];
24
+
25
+ return {
26
+ value: item.value !== undefined ? item.value : key,
27
+ label: item.text,
28
+ originData: item.data,
29
+ };
30
+ });
31
+
32
+ function initI18n({ enTranslation }) {
33
+ const cacheLng = localStorage.getItem('i18nextLng') || EnumLanguage.ZH_CN;
34
+ const lng = listLanguage.find((item) => item.value === cacheLng) ? cacheLng : EnumLanguage.ZH_CN;
35
+
36
+ i18n
37
+ .use(LanguageDetector)
38
+ .use(initReactI18next)
39
+ .use(ICU)
40
+ .init({
41
+ resources: {
42
+ [EnumLanguage.ZH_CN]: {
43
+ translation: {},
44
+ },
45
+ [EnumLanguage.EN_US]: {
46
+ translation: {
47
+ ...enTranslation,
48
+ },
49
+ },
50
+ },
51
+ lng,
52
+ fallbackLng: EnumLanguage.ZH_CN,
53
+ interpolation: {
54
+ escapeValue: false,
55
+ },
56
+ });
57
+ }
58
+
59
+ function I18nProvider({ children }: { children: React.ReactNode }) {
60
+ return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
61
+ }
62
+
63
+ export { EnumLanguage, I18nProvider, initI18n, listLanguage, valueEnumLanguage };