@arcblock/ux 2.4.24 → 2.4.27
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 +92 -97
- package/lib/Locale/selector.js +7 -4
- package/package.json +4 -4
- package/src/Locale/context.js +54 -64
- package/src/Locale/selector.js +6 -3
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,16 @@ const setLocaleParam = function setLocaleParam(locale) {
|
|
67
58
|
}
|
68
59
|
};
|
69
60
|
|
70
|
-
const getLocale =
|
61
|
+
const getLocale = languages => {
|
62
|
+
var _languages$;
|
63
|
+
|
64
|
+
const langParams = {
|
65
|
+
languages: (languages || []).map(item => item.value),
|
66
|
+
// 取 languages 首个元素的 value 值, 如果不存在则取 'en'
|
67
|
+
fallback: (languages === null || languages === void 0 ? void 0 : (_languages$ = languages[0]) === null || _languages$ === void 0 ? void 0 : _languages$.value) || 'en'
|
68
|
+
};
|
69
|
+
return getLocaleFromSearchParams(languages) || _jsCookie.default.get(cookieName) || (0, _browserLang.default)(langParams);
|
70
|
+
};
|
71
71
|
|
72
72
|
exports.getLocale = getLocale;
|
73
73
|
|
@@ -81,85 +81,80 @@ exports.setLocale = setLocale;
|
|
81
81
|
|
82
82
|
const replace = (template, data) => template.replace(/{(\w*)}/g, (m, key) => data.hasOwnProperty(key) ? data[key] : '');
|
83
83
|
|
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
|
-
}
|
84
|
+
const LocaleContext = /*#__PURE__*/(0, _react.createContext)();
|
85
|
+
exports.LocaleContext = LocaleContext;
|
86
|
+
const {
|
87
|
+
Provider,
|
88
|
+
Consumer
|
89
|
+
} = LocaleContext;
|
90
|
+
exports.LocaleConsumer = Consumer;
|
91
|
+
|
92
|
+
function LocaleProvider(_ref) {
|
93
|
+
let {
|
94
|
+
children,
|
95
|
+
locale,
|
96
|
+
translations,
|
97
|
+
languages
|
98
|
+
} = _ref,
|
99
|
+
rest = _objectWithoutProperties(_ref, _excluded);
|
100
|
+
|
101
|
+
const [currentLocale, setCurrentLocale] = (0, _react.useState)(locale || getLocale(languages));
|
102
|
+
|
103
|
+
const changeLocale = newLocale => {
|
104
|
+
setCurrentLocale(newLocale);
|
105
|
+
setLocale(newLocale);
|
106
|
+
};
|
133
107
|
|
134
|
-
|
135
|
-
const
|
136
|
-
return context;
|
137
|
-
}
|
108
|
+
(0, _react.useEffect)(() => {
|
109
|
+
const tmpLocale = locale || getLocale(languages);
|
138
110
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
};
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
111
|
+
if (tmpLocale !== currentLocale) {
|
112
|
+
changeLocale(locale);
|
113
|
+
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
114
|
+
|
115
|
+
}, [locale]);
|
116
|
+
|
117
|
+
const translate = (key, data) => {
|
118
|
+
if (!translations[currentLocale] || !translations[currentLocale][key]) {
|
119
|
+
console.warn("Warning: no ".concat(key, " translation of ").concat(currentLocale));
|
120
|
+
return key;
|
121
|
+
}
|
122
|
+
|
123
|
+
return replace(translations[currentLocale][key], data);
|
152
124
|
};
|
153
|
-
};
|
154
125
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
126
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
|
127
|
+
value: _objectSpread({
|
128
|
+
locale: currentLocale,
|
129
|
+
changeLocale,
|
130
|
+
t: translate,
|
131
|
+
languages
|
132
|
+
}, rest),
|
133
|
+
children: children
|
134
|
+
});
|
135
|
+
}
|
136
|
+
|
137
|
+
function useLocaleContext() {
|
138
|
+
const context = (0, _react.useContext)(LocaleContext);
|
139
|
+
return context;
|
140
|
+
}
|
141
|
+
|
142
|
+
LocaleProvider.propTypes = {
|
143
|
+
children: _propTypes.default.any.isRequired,
|
144
|
+
translations: _propTypes.default.object.isRequired,
|
145
|
+
locale: _propTypes.default.string,
|
146
|
+
languages: _propTypes.default.arrayOf(_propTypes.default.shape({
|
147
|
+
value: _propTypes.default.string,
|
148
|
+
text: _propTypes.default.string
|
149
|
+
}))
|
150
|
+
};
|
151
|
+
LocaleProvider.defaultProps = {
|
152
|
+
locale: '',
|
153
|
+
languages: [{
|
154
|
+
value: 'en',
|
155
|
+
text: 'English'
|
156
|
+
}, {
|
157
|
+
value: 'zh',
|
158
|
+
text: '简体中文'
|
159
|
+
}]
|
160
|
+
};
|
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
|
@@ -184,4 +185,6 @@ LocaleSelector.defaultProps = {
|
|
184
185
|
};
|
185
186
|
var _default = LocaleSelector;
|
186
187
|
exports.default = _default;
|
187
|
-
const Div = (0, _Theme.styled)('div'
|
188
|
+
const Div = (0, _Theme.styled)('div', {
|
189
|
+
shouldForwardProp: prop => prop !== 'dark'
|
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.27",
|
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": "89ac06d01dc874b1248e553f3563d57c36ea6647",
|
51
51
|
"dependencies": {
|
52
|
-
"@arcblock/icons": "^2.4.
|
53
|
-
"@arcblock/react-hooks": "^2.4.
|
52
|
+
"@arcblock/icons": "^2.4.27",
|
53
|
+
"@arcblock/react-hooks": "^2.4.27",
|
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();
|
@@ -121,7 +121,9 @@ LocaleSelector.defaultProps = {
|
|
121
121
|
|
122
122
|
export default LocaleSelector;
|
123
123
|
|
124
|
-
const Div = styled('div'
|
124
|
+
const Div = styled('div', {
|
125
|
+
shouldForwardProp: (prop) => prop !== 'dark',
|
126
|
+
})`
|
125
127
|
display: inline-block;
|
126
128
|
|
127
129
|
.trigger {
|
@@ -129,6 +131,7 @@ const Div = styled('div')`
|
|
129
131
|
flex-direction: column;
|
130
132
|
justify-content: center;
|
131
133
|
font-size: 14px;
|
134
|
+
white-space: nowrap;
|
132
135
|
|
133
136
|
.trigger-text {
|
134
137
|
margin-left: 5px;
|