多语言(i18n)支持 是企业项目走向国际化的必经之路,也是前端工程师最佳实践的内容之一
const contextValue = {
lang,
setLang,
}
switch (lang) {
case 'zh-CN':
return zhCN;
case 'en-US':
return enUS;
default:
return zhCN;
}
const i18n = {
'en-US': {
'menu.welcome': 'Welcome',
'menu.dashboard': 'Dashboard',
'menu.list': 'List',
},
'zh-CN': {
'menu.dashboard': '仪表盘',
'menu.list': '列表页',
'menu.result': '结果页',
},
};
export default i18n;
// An highlighted block
import { useContext } from 'react';
import { GlobalContext } from '../context';
import defaultLocale from '../locale';
function useLocale(locale = null) {
const { lang } = useContext(GlobalContext);
return (locale || defaultLocale)[lang] || {};
}
const t = useLocale();
<Menu.Item onClick={handleChangeRole} key="switch role">
<IconTag className={styles['dropdown-icon']} />
{t['menu.user.switchRoles']}
</Menu.Item>
const { setLang, lang } = useContext(GlobalContext);
setLang(value);
根据用户的喜好,选择不同的风格,成为产品设计必备的需求之一
const contextValue = {
theme,
setTheme,
};
body {
--red-1: 255,236,232;
--red-2: 253,205,197;
--red-3: 251,172,163;
}
body[arco-theme='dark'] {
--red-1: 77,0,10;
--red-2: 119,6,17;
--red-3: 161,22,31;
}
const { theme, setTheme } = useContext(GlobalContext);
<IconButton
icon={theme !== 'dark' ? <IconMoonFill /> : <IconSunFill />}
onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
/>
function changeTheme(theme) {
if (theme === 'dark') {
document.body.setAttribute('arco-theme', 'dark');
} else {
document.body.removeAttribute('arco-theme');
}
}