-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(locale): export Selectlang component (#200)
* feat(locale): export Selectlang component * feat(plugin-locake): support getAllLocales Co-authored-by: 依鹭 <jingcao.cjc@antfin.com> Co-authored-by: chenshuai2144 <qixian.cs@outlook.com>
- Loading branch information
1 parent
9f0df80
commit 16278a4
Showing
6 changed files
with
229 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
name: '你好,{name}', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import React from 'react'; | ||
{{#Antd}} | ||
import { GlobalOutlined } from '{{{ iconsPkgPath }}}'; | ||
import { Menu, Dropdown } from 'antd'; | ||
import { ClickParam } from 'antd/{{{antdFiles}}}/menu'; | ||
import { DropDownProps } from 'antd/{{{antdFiles}}}/dropdown'; | ||
{{/Antd}} | ||
import { getLocale, setLocale } from './localeExports'; | ||
|
||
{{#Antd}} | ||
export interface HeaderDropdownProps extends DropDownProps { | ||
overlayClassName?: string; | ||
placement?: | ||
| 'bottomLeft' | ||
| 'bottomRight' | ||
| 'topLeft' | ||
| 'topCenter' | ||
| 'topRight' | ||
| 'bottomCenter'; | ||
} | ||
|
||
const HeaderDropdown: React.FC<HeaderDropdownProps> = ({ | ||
overlayClassName: cls, | ||
...restProps | ||
}) => ( | ||
<Dropdown | ||
getPopupContainer={(trigger) =>( trigger.parentNode as HTMLElement)} | ||
overlayClassName={cls} | ||
{...restProps} | ||
/> | ||
); | ||
{{/Antd}} | ||
|
||
interface LocalData { | ||
lang: string, | ||
label?: string, | ||
icon?: string, | ||
title?: string, | ||
} | ||
|
||
interface SelectLangProps { | ||
globalIconClassName?: string, | ||
postLocalesData?: (locales: LocalData[]) => LocalData[], | ||
onItemClick?: (params:ClickParam) => void, | ||
} | ||
|
||
const transformArrayToObject = (allLangUIConfig:LocalData[])=>{ | ||
return allLangUIConfig.reduce((obj, item) => { | ||
if(!item.lang){ | ||
return obj; | ||
} | ||
|
||
return { | ||
...obj, | ||
[item.lang]: item, | ||
}; | ||
}, {}); | ||
} | ||
|
||
const defaultLangUConfigMap = { | ||
'zh-CN': { | ||
lang: 'zh-CN', | ||
label: '简体中文', | ||
icon: '🇨🇳', | ||
title: '语言' | ||
}, | ||
'zh-TW': { | ||
lang: 'zh-TW', | ||
label: '繁体中文', | ||
icon: '🇭🇰', | ||
title: '語言' | ||
}, | ||
'en-US': { | ||
lang: 'en-US', | ||
label: 'English', | ||
icon: '🇺🇸', | ||
title: 'Language' | ||
}, | ||
'pt-BR': { | ||
lang: 'pt-BR', | ||
label: 'Português', | ||
icon: '🇧🇷', | ||
title: 'Idiomas' | ||
} | ||
}; | ||
|
||
export const SelectLang: React.FC<SelectLangProps> = (props) => { | ||
{{#ShowSelectLang}} | ||
const { globalIconClassName, postLocalesData, onItemClick, ...restProps } = props; | ||
const selectedLang = getLocale(); | ||
|
||
const changeLang = ({ key }: ClickParam): void => setLocale(key); | ||
|
||
const defaultLangUConfig = getAllLocales().map( | ||
key => | ||
defaultLangUConfigMap[key] || { | ||
lang: key, | ||
label: key, | ||
icon: '🌐', | ||
title: key | ||
} | ||
); | ||
|
||
|
||
const allLangUIConfig = transformArrayToObject(postLocalesData ? postLocalesData(defaultLangUConfig): defaultLangUConfig); | ||
|
||
const handleClick = onItemClick ? (params)=> onItemClick(params): changeLang; | ||
|
||
const menuItemStyle = { minWidth: '160px' } | ||
const langMenu = ( | ||
<Menu | ||
selectedKeys={[selectedLang]} onClick={handleClick} | ||
> | ||
{{#LocaleList}} | ||
<Menu.Item key={'{{locale}}'} style={menuItemStyle}> | ||
<span role='img' aria-label={allLangUIConfig['{{locale}}']?.label || '{{locale}}'}> | ||
{allLangUIConfig['{{locale}}']?.icon || "🌐"} | ||
</span>{' '} | ||
{allLangUIConfig['{{locale}}']?.label || '{{locale}}'} | ||
</Menu.Item> | ||
{{/LocaleList}} | ||
</Menu> | ||
); | ||
|
||
const style = { | ||
verticalAlign: 'top', | ||
cursor: 'pointer', | ||
padding: '0 12px', | ||
} | ||
|
||
return ( | ||
<HeaderDropdown overlay={langMenu} placement='bottomRight' {...restProps}> | ||
<span | ||
className={globalIconClassName} | ||
style={style}> | ||
<GlobalOutlined title={allLangUIConfig[selectedLang]?.title} /> | ||
</span> | ||
</HeaderDropdown> | ||
); | ||
{{/ShowSelectLang}} | ||
return <></> | ||
}; |