@arcblock/ux 2.4.26 → 2.4.28
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/lib/Locale/context.js +93 -97
- package/lib/Locale/selector.js +5 -4
- package/package.json +4 -4
- package/src/Locale/context.js +54 -64
- package/src/Locale/selector.js +3 -2
package/lib/Locale/context.js
CHANGED
@@ -3,7 +3,10 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
6
|
+
exports.LocaleContext = exports.LocaleConsumer = void 0;
|
7
|
+
exports.LocaleProvider = LocaleProvider;
|
8
|
+
exports.setLocale = exports.getLocale = void 0;
|
9
|
+
exports.useLocaleContext = useLocaleContext;
|
7
10
|
|
8
11
|
var _react = require("react");
|
9
12
|
|
@@ -17,7 +20,7 @@ var _Util = require("../Util");
|
|
17
20
|
|
18
21
|
var _jsxRuntime = require("react/jsx-runtime");
|
19
22
|
|
20
|
-
const _excluded = ["children", "locale", "translations"];
|
23
|
+
const _excluded = ["children", "locale", "translations", "languages"];
|
21
24
|
|
22
25
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23
26
|
|
@@ -31,22 +34,10 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return
|
|
31
34
|
|
32
35
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
33
36
|
|
34
|
-
const cookieName = 'nf_lang';
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
}, {
|
39
|
-
value: 'zh',
|
40
|
-
text: '简体中文'
|
41
|
-
}];
|
42
|
-
exports.languages = languages;
|
43
|
-
const langParams = {
|
44
|
-
languages: ['zh', 'en'],
|
45
|
-
fallback: 'en'
|
46
|
-
}; // 跨应用传递多语言选择的方式是在 query string 中添加 locale 参数,LocaleSelector 要高优先级遵守这个参数
|
47
|
-
|
48
|
-
const getLocaleFromSearchParams = function getLocaleFromSearchParams() {
|
49
|
-
let url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.location.href;
|
37
|
+
const cookieName = 'nf_lang'; // 跨应用传递多语言选择的方式是在 query string 中添加 locale 参数,LocaleSelector 要高优先级遵守这个参数
|
38
|
+
|
39
|
+
const getLocaleFromSearchParams = function getLocaleFromSearchParams(languages) {
|
40
|
+
let url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href;
|
50
41
|
const locale = new URL(url).searchParams.get('locale');
|
51
42
|
|
52
43
|
if (languages.find(x => x.value === locale)) {
|
@@ -67,7 +58,17 @@ const setLocaleParam = function setLocaleParam(locale) {
|
|
67
58
|
}
|
68
59
|
};
|
69
60
|
|
70
|
-
const getLocale =
|
61
|
+
const getLocale = function getLocale() {
|
62
|
+
var _languages$;
|
63
|
+
|
64
|
+
let languages = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
65
|
+
const langParams = {
|
66
|
+
languages: languages.map(item => item.value),
|
67
|
+
// 取 languages 首个元素的 value 值, 如果不存在则取 'en'
|
68
|
+
fallback: (languages === null || languages === void 0 ? void 0 : (_languages$ = languages[0]) === null || _languages$ === void 0 ? void 0 : _languages$.value) || 'en'
|
69
|
+
};
|
70
|
+
return getLocaleFromSearchParams(languages) || _jsCookie.default.get(cookieName) || (0, _browserLang.default)(langParams);
|
71
|
+
};
|
71
72
|
|
72
73
|
exports.getLocale = getLocale;
|
73
74
|
|
@@ -81,85 +82,80 @@ exports.setLocale = setLocale;
|
|
81
82
|
|
82
83
|
const replace = (template, data) => template.replace(/{(\w*)}/g, (m, key) => data.hasOwnProperty(key) ? data[key] : '');
|
83
84
|
|
84
|
-
const
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
const tmpLocale = getLocale(locale);
|
108
|
-
|
109
|
-
if (tmpLocale !== currentLocale) {
|
110
|
-
changeLocale(locale);
|
111
|
-
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
112
|
-
|
113
|
-
}, [locale]);
|
114
|
-
|
115
|
-
const translate = (key, data) => {
|
116
|
-
if (!translations[currentLocale] || !translations[currentLocale][key]) {
|
117
|
-
console.warn("Warning: no ".concat(key, " translation of ").concat(currentLocale));
|
118
|
-
return key;
|
119
|
-
}
|
120
|
-
|
121
|
-
return replace(translations[currentLocale][key], data);
|
122
|
-
};
|
123
|
-
|
124
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
|
125
|
-
value: _objectSpread({
|
126
|
-
locale: currentLocale,
|
127
|
-
changeLocale,
|
128
|
-
t: translate
|
129
|
-
}, rest),
|
130
|
-
children: children
|
131
|
-
});
|
132
|
-
}
|
85
|
+
const LocaleContext = /*#__PURE__*/(0, _react.createContext)();
|
86
|
+
exports.LocaleContext = LocaleContext;
|
87
|
+
const {
|
88
|
+
Provider,
|
89
|
+
Consumer
|
90
|
+
} = LocaleContext;
|
91
|
+
exports.LocaleConsumer = Consumer;
|
92
|
+
|
93
|
+
function LocaleProvider(_ref) {
|
94
|
+
let {
|
95
|
+
children,
|
96
|
+
locale,
|
97
|
+
translations,
|
98
|
+
languages
|
99
|
+
} = _ref,
|
100
|
+
rest = _objectWithoutProperties(_ref, _excluded);
|
101
|
+
|
102
|
+
const [currentLocale, setCurrentLocale] = (0, _react.useState)(locale || getLocale(languages));
|
103
|
+
|
104
|
+
const changeLocale = newLocale => {
|
105
|
+
setCurrentLocale(newLocale);
|
106
|
+
setLocale(newLocale);
|
107
|
+
};
|
133
108
|
|
134
|
-
|
135
|
-
const
|
136
|
-
return context;
|
137
|
-
}
|
109
|
+
(0, _react.useEffect)(() => {
|
110
|
+
const tmpLocale = locale || getLocale(languages);
|
138
111
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
};
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
112
|
+
if (tmpLocale !== currentLocale) {
|
113
|
+
changeLocale(locale);
|
114
|
+
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
115
|
+
|
116
|
+
}, [locale]);
|
117
|
+
|
118
|
+
const translate = (key, data) => {
|
119
|
+
if (!translations[currentLocale] || !translations[currentLocale][key]) {
|
120
|
+
console.warn("Warning: no ".concat(key, " translation of ").concat(currentLocale));
|
121
|
+
return key;
|
122
|
+
}
|
123
|
+
|
124
|
+
return replace(translations[currentLocale][key], data);
|
152
125
|
};
|
153
|
-
};
|
154
126
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
127
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
|
128
|
+
value: _objectSpread({
|
129
|
+
locale: currentLocale,
|
130
|
+
changeLocale,
|
131
|
+
t: translate,
|
132
|
+
languages
|
133
|
+
}, rest),
|
134
|
+
children: children
|
135
|
+
});
|
136
|
+
}
|
137
|
+
|
138
|
+
function useLocaleContext() {
|
139
|
+
const context = (0, _react.useContext)(LocaleContext);
|
140
|
+
return context;
|
141
|
+
}
|
142
|
+
|
143
|
+
LocaleProvider.propTypes = {
|
144
|
+
children: _propTypes.default.any.isRequired,
|
145
|
+
translations: _propTypes.default.object.isRequired,
|
146
|
+
locale: _propTypes.default.string,
|
147
|
+
languages: _propTypes.default.arrayOf(_propTypes.default.shape({
|
148
|
+
value: _propTypes.default.string,
|
149
|
+
text: _propTypes.default.string
|
150
|
+
}))
|
151
|
+
};
|
152
|
+
LocaleProvider.defaultProps = {
|
153
|
+
locale: '',
|
154
|
+
languages: [{
|
155
|
+
value: 'en',
|
156
|
+
text: 'English'
|
157
|
+
}, {
|
158
|
+
value: 'zh',
|
159
|
+
text: '简体中文'
|
160
|
+
}]
|
161
|
+
};
|
package/lib/Locale/selector.js
CHANGED
@@ -69,7 +69,8 @@ function LocaleSelector(props) {
|
|
69
69
|
|
70
70
|
const {
|
71
71
|
locale,
|
72
|
-
changeLocale
|
72
|
+
changeLocale,
|
73
|
+
languages
|
73
74
|
} = (0, _react.useContext)(_context.LocaleContext);
|
74
75
|
const anchorEl = (0, _react.useRef)(null);
|
75
76
|
const [open, setOpen] = (0, _react.useState)(false);
|
@@ -132,7 +133,7 @@ function LocaleSelector(props) {
|
|
132
133
|
children: [renderIcon, showText ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography.default, {
|
133
134
|
component: "strong",
|
134
135
|
className: "trigger-text",
|
135
|
-
children:
|
136
|
+
children: languages.find(x => x.value === locale).text
|
136
137
|
}) : '']
|
137
138
|
})
|
138
139
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Popper.default, _objectSpread(_objectSpread({
|
@@ -145,7 +146,7 @@ function LocaleSelector(props) {
|
|
145
146
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ClickAwayListener.default, {
|
146
147
|
onClickAway: onClose,
|
147
148
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_MenuList.default, {
|
148
|
-
children:
|
149
|
+
children: languages.map(_ref => {
|
149
150
|
let {
|
150
151
|
value,
|
151
152
|
text
|
@@ -186,4 +187,4 @@ var _default = LocaleSelector;
|
|
186
187
|
exports.default = _default;
|
187
188
|
const Div = (0, _Theme.styled)('div', {
|
188
189
|
shouldForwardProp: prop => prop !== 'dark'
|
189
|
-
})(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n display: inline-block;\n\n .trigger {\n display: flex;\n flex-direction: column;\n justify-content: center;\n font-size: 14px;\n\n .trigger-text {\n margin-left: 5px;\n font-size: 14px;\n color: ", ";\n }\n }\n\n .locales {\n background: ", ";\n box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);\n }\n\n .locale-item {\n font-size: 16px;\n font-style: normal;\n font-stretch: normal;\n line-height: normal;\n letter-spacing: 2px;\n text-align: center;\n color: ", ";\n cursor: pointer;\n display: flex;\n padding: 16px;\n align-items: center;\n .check-icon {\n visibility: hidden;\n margin-right: 4px;\n }\n .check-icon-visible {\n visibility: visible;\n }\n }\n"])), props => (0, _Util.getColor)(props), props => (0, _Util.getBackground)(props), props => (0, _Util.getColor)(props));
|
190
|
+
})(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n display: inline-block;\n\n .trigger {\n display: flex;\n flex-direction: column;\n justify-content: center;\n font-size: 14px;\n white-space: nowrap;\n\n .trigger-text {\n margin-left: 5px;\n font-size: 14px;\n color: ", ";\n }\n }\n\n .locales {\n background: ", ";\n box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);\n }\n\n .locale-item {\n font-size: 16px;\n font-style: normal;\n font-stretch: normal;\n line-height: normal;\n letter-spacing: 2px;\n text-align: center;\n color: ", ";\n cursor: pointer;\n display: flex;\n padding: 16px;\n align-items: center;\n .check-icon {\n visibility: hidden;\n margin-right: 4px;\n }\n .check-icon-visible {\n visibility: visible;\n }\n }\n"])), props => (0, _Util.getColor)(props), props => (0, _Util.getBackground)(props), props => (0, _Util.getColor)(props));
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ux",
|
3
|
-
"version": "2.4.
|
3
|
+
"version": "2.4.28",
|
4
4
|
"description": "Common used react components for arcblock products",
|
5
5
|
"keywords": [
|
6
6
|
"react",
|
@@ -47,10 +47,10 @@
|
|
47
47
|
"react": ">=18.1.0",
|
48
48
|
"react-ga": "^2.7.0"
|
49
49
|
},
|
50
|
-
"gitHead": "
|
50
|
+
"gitHead": "3133ed4e76d70eff05b08130fa7340dcc07611c4",
|
51
51
|
"dependencies": {
|
52
|
-
"@arcblock/icons": "^2.4.
|
53
|
-
"@arcblock/react-hooks": "^2.4.
|
52
|
+
"@arcblock/icons": "^2.4.28",
|
53
|
+
"@arcblock/react-hooks": "^2.4.28",
|
54
54
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
55
55
|
"@emotion/react": "^11.10.0",
|
56
56
|
"@emotion/styled": "^11.10.0",
|
package/src/Locale/context.js
CHANGED
@@ -7,17 +7,9 @@ import browserLang from './browser-lang';
|
|
7
7
|
import { getCookieOptions } from '../Util';
|
8
8
|
|
9
9
|
const cookieName = 'nf_lang';
|
10
|
-
const languages = [
|
11
|
-
{ value: 'en', text: 'English' },
|
12
|
-
{ value: 'zh', text: '简体中文' },
|
13
|
-
];
|
14
|
-
const langParams = {
|
15
|
-
languages: ['zh', 'en'],
|
16
|
-
fallback: 'en',
|
17
|
-
};
|
18
10
|
|
19
11
|
// 跨应用传递多语言选择的方式是在 query string 中添加 locale 参数,LocaleSelector 要高优先级遵守这个参数
|
20
|
-
const getLocaleFromSearchParams = (url = window.location.href) => {
|
12
|
+
const getLocaleFromSearchParams = (languages, url = window.location.href) => {
|
21
13
|
const locale = new URL(url).searchParams.get('locale');
|
22
14
|
if (languages.find((x) => x.value === locale)) {
|
23
15
|
return locale;
|
@@ -34,8 +26,14 @@ const setLocaleParam = (locale, url = window.location.href) => {
|
|
34
26
|
}
|
35
27
|
};
|
36
28
|
|
37
|
-
const getLocale = (
|
38
|
-
|
29
|
+
const getLocale = (languages = []) => {
|
30
|
+
const langParams = {
|
31
|
+
languages: languages.map((item) => item.value),
|
32
|
+
// 取 languages 首个元素的 value 值, 如果不存在则取 'en'
|
33
|
+
fallback: languages?.[0]?.value || 'en',
|
34
|
+
};
|
35
|
+
return getLocaleFromSearchParams(languages) || Cookie.get(cookieName) || browserLang(langParams);
|
36
|
+
};
|
39
37
|
const setLocale = (locale) => {
|
40
38
|
Cookie.set(cookieName, locale, getCookieOptions());
|
41
39
|
setLocaleParam(locale);
|
@@ -44,64 +42,56 @@ const setLocale = (locale) => {
|
|
44
42
|
const replace = (template, data) =>
|
45
43
|
template.replace(/{(\w*)}/g, (m, key) => (data.hasOwnProperty(key) ? data[key] : ''));
|
46
44
|
|
47
|
-
const
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
const [currentLocale, setCurrentLocale] = useState(getLocale(locale));
|
52
|
-
|
53
|
-
const changeLocale = (newLocale) => {
|
54
|
-
setCurrentLocale(newLocale);
|
55
|
-
setLocale(newLocale);
|
56
|
-
};
|
57
|
-
|
58
|
-
useEffect(() => {
|
59
|
-
const tmpLocale = getLocale(locale);
|
60
|
-
if (tmpLocale !== currentLocale) {
|
61
|
-
changeLocale(locale);
|
62
|
-
}
|
63
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
64
|
-
}, [locale]);
|
65
|
-
|
66
|
-
const translate = (key, data) => {
|
67
|
-
if (!translations[currentLocale] || !translations[currentLocale][key]) {
|
68
|
-
console.warn(`Warning: no ${key} translation of ${currentLocale}`);
|
69
|
-
return key;
|
70
|
-
}
|
71
|
-
|
72
|
-
return replace(translations[currentLocale][key], data);
|
73
|
-
};
|
74
|
-
|
75
|
-
return <Provider value={{ locale: currentLocale, changeLocale, t: translate, ...rest }}>{children}</Provider>;
|
76
|
-
}
|
45
|
+
const LocaleContext = createContext();
|
46
|
+
const { Provider, Consumer } = LocaleContext;
|
47
|
+
function LocaleProvider({ children, locale, translations, languages, ...rest }) {
|
48
|
+
const [currentLocale, setCurrentLocale] = useState(locale || getLocale(languages));
|
77
49
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
LocaleProvider.propTypes = {
|
84
|
-
children: PropTypes.any.isRequired,
|
85
|
-
translations: PropTypes.object.isRequired,
|
86
|
-
locale: PropTypes.string,
|
50
|
+
const changeLocale = (newLocale) => {
|
51
|
+
setCurrentLocale(newLocale);
|
52
|
+
setLocale(newLocale);
|
87
53
|
};
|
88
54
|
|
89
|
-
|
90
|
-
locale
|
55
|
+
useEffect(() => {
|
56
|
+
const tmpLocale = locale || getLocale(languages);
|
57
|
+
if (tmpLocale !== currentLocale) {
|
58
|
+
changeLocale(locale);
|
59
|
+
}
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
61
|
+
}, [locale]);
|
62
|
+
|
63
|
+
const translate = (key, data) => {
|
64
|
+
if (!translations[currentLocale] || !translations[currentLocale][key]) {
|
65
|
+
console.warn(`Warning: no ${key} translation of ${currentLocale}`);
|
66
|
+
return key;
|
67
|
+
}
|
68
|
+
|
69
|
+
return replace(translations[currentLocale][key], data);
|
91
70
|
};
|
92
71
|
|
93
|
-
return
|
72
|
+
return (
|
73
|
+
<Provider value={{ locale: currentLocale, changeLocale, t: translate, languages, ...rest }}>{children}</Provider>
|
74
|
+
);
|
75
|
+
}
|
76
|
+
|
77
|
+
function useLocaleContext() {
|
78
|
+
const context = useContext(LocaleContext);
|
79
|
+
return context;
|
80
|
+
}
|
81
|
+
|
82
|
+
LocaleProvider.propTypes = {
|
83
|
+
children: PropTypes.any.isRequired,
|
84
|
+
translations: PropTypes.object.isRequired,
|
85
|
+
locale: PropTypes.string,
|
86
|
+
languages: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.string, text: PropTypes.string })),
|
94
87
|
};
|
95
88
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
useLocaleContext,
|
103
|
-
setLocale,
|
104
|
-
getLocale,
|
105
|
-
languages,
|
106
|
-
create,
|
89
|
+
LocaleProvider.defaultProps = {
|
90
|
+
locale: '',
|
91
|
+
languages: [
|
92
|
+
{ value: 'en', text: 'English' },
|
93
|
+
{ value: 'zh', text: '简体中文' },
|
94
|
+
],
|
107
95
|
};
|
96
|
+
|
97
|
+
export { LocaleProvider, Consumer as LocaleConsumer, LocaleContext, useLocaleContext, setLocale, getLocale };
|
package/src/Locale/selector.js
CHANGED
@@ -14,12 +14,12 @@ import GlobeIcon from '@arcblock/icons/lib/Globe';
|
|
14
14
|
|
15
15
|
import { getColor, getBackground } from '../Util';
|
16
16
|
|
17
|
-
import { LocaleContext
|
17
|
+
import { LocaleContext } from './context';
|
18
18
|
import { styled, useTheme } from '../Theme';
|
19
19
|
|
20
20
|
function LocaleSelector(props) {
|
21
21
|
const { showText, popperProps, popperType, icon: Icon, size, ...rest } = props;
|
22
|
-
const { locale, changeLocale } = useContext(LocaleContext);
|
22
|
+
const { locale, changeLocale, languages } = useContext(LocaleContext);
|
23
23
|
const anchorEl = useRef(null);
|
24
24
|
const [open, setOpen] = useState(false);
|
25
25
|
const theme = useTheme();
|
@@ -131,6 +131,7 @@ const Div = styled('div', {
|
|
131
131
|
flex-direction: column;
|
132
132
|
justify-content: center;
|
133
133
|
font-size: 14px;
|
134
|
+
white-space: nowrap;
|
134
135
|
|
135
136
|
.trigger-text {
|
136
137
|
margin-left: 5px;
|