-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add aria label to controller buttons
- Loading branch information
Showing
14 changed files
with
61 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,12 @@ | ||
import React, {ReactElement} from 'react' | ||
import {LocaleConfig, LocaleConfigKey} from '../constants/locales' | ||
import LocaleContext from '../contexts/LocaleContext' | ||
import {useLocaleText} from '../contexts/LocaleContext' | ||
|
||
const TranslatedText: React.FC<{ | ||
name: LocaleConfigKey | ||
style?: 'narrow' | 'text' // narrow 简短的文本展示,text 正常文本展示,两种展示格式满足长短文案需求 | ||
name: Parameters<typeof useLocaleText>[0] | ||
/** narrow 简短的文本展示,text 正常文本展示,两种展示格式满足长短文案需求 */ | ||
style?: Parameters<typeof useLocaleText>[1] | ||
}> = ({name, style = 'text'}): ReactElement => { | ||
return ( | ||
<LocaleContext.Consumer> | ||
{(locale) => { | ||
if (typeof locale[name] === 'object') | ||
return ( | ||
locale[name] as Exclude<LocaleConfig[keyof LocaleConfig], string> | ||
)[style] | ||
return locale[name] | ||
}} | ||
</LocaleContext.Consumer> | ||
) | ||
return <>{useLocaleText(name, style)}</> | ||
} | ||
|
||
export default TranslatedText |
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
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
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
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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
import React from 'react' | ||
import React, {useContext} from 'react' | ||
import locales, {defaultLocale} from '../constants/locales' | ||
import type {LocaleConfig, LocaleConfigKey} from '../constants/locales' | ||
|
||
const LocaleContext = React.createContext(locales[defaultLocale]) | ||
LocaleContext.displayName = 'LocaleContext' | ||
|
||
/** Get i18n locale text */ | ||
export const useLocaleText = ( | ||
name: LocaleConfigKey, | ||
style: 'narrow' | 'text' = 'text' | ||
): string => { | ||
const locale = useContext(LocaleContext) | ||
if (typeof locale[name] === 'object') { | ||
const value = locale[name] as Exclude< | ||
LocaleConfig[keyof LocaleConfig], | ||
string | ||
> | ||
return value[style] | ||
} | ||
return locale[name] as string | ||
} | ||
|
||
export default LocaleContext |