-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
104 additions
and
36 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
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,41 @@ | ||
import { createTheme, ThemeProvider } from '@mui/material/styles' | ||
import * as materialMultiLanguages from '@mui/material/locale' | ||
import React from 'react' | ||
|
||
import { useB3CurrentLang } from '@b3/lang' | ||
|
||
type LangMapType = { | ||
[index: string]: string | ||
} | ||
|
||
const MUI_LANG_MAP: LangMapType = { | ||
en: 'enUS', | ||
zh: 'zhCN', | ||
fr: 'frFR', | ||
nl: 'nlNL', | ||
de: 'deDE', | ||
it: 'itIT', | ||
es: 'esES', | ||
} | ||
|
||
type MaterialMultiLanguagesType = { | ||
[K: string]: materialMultiLanguages.Localization | ||
} | ||
|
||
type Props = { | ||
children?: React.ReactNode; | ||
} | ||
|
||
const theme = (lang: string) => createTheme({}, (materialMultiLanguages as MaterialMultiLanguagesType)[MUI_LANG_MAP[lang] || 'enUS']) | ||
|
||
function B3ThemeProvider({ children }: Props) { | ||
const [lang] = useB3CurrentLang() | ||
|
||
return ( | ||
<ThemeProvider theme={theme(lang)}> | ||
{ children } | ||
</ThemeProvider> | ||
) | ||
} | ||
|
||
export default B3ThemeProvider |
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,28 +1,22 @@ | ||
import { IntlProvider, MessageFormatElement } from 'react-intl' | ||
import React from 'react' | ||
import { ThemeProvider, Theme } from '@mui/material/styles' | ||
|
||
import { BrowserLanguage } from '@b3/utils' | ||
|
||
import { useB3CurrentLang } from './useB3CurrentLang' | ||
import * as defaultLocales from './locales' | ||
import defaultTheme from './theme' | ||
|
||
type Props = { | ||
children?: React.ReactNode; | ||
theme?: (lang: string) => Theme; | ||
locales?: { | ||
[k: string]: Record<string, string> | Record<string, MessageFormatElement[]> | ||
}; | ||
}; | ||
|
||
export function LangProvider({ children, theme = defaultTheme, locales = defaultLocales }: Props) { | ||
const lang = BrowserLanguage() | ||
export function LangProvider({ children, locales = defaultLocales }: Props) { | ||
const [lang] = useB3CurrentLang() | ||
|
||
return ( | ||
<IntlProvider messages={locales[lang] || locales.en} locale={lang} defaultLocale="en"> | ||
<ThemeProvider theme={theme(lang)}> | ||
{children} | ||
</ThemeProvider> | ||
{children} | ||
</IntlProvider> | ||
) | ||
} |
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,2 +1,4 @@ | ||
export { LangProvider } from './LangProvider' | ||
export { useB3Lang } from './useB3Lang' | ||
export { useB3CurrentLang } from './useB3CurrentLang' | ||
export { withB3Lang } from './withB3Lang' |
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
import { | ||
useState, | ||
useEffect, | ||
Dispatch, | ||
SetStateAction, | ||
} from 'react' | ||
|
||
import { BrowserLanguage } from '@b3/utils' | ||
|
||
const defaultLang = (() => { | ||
const lang = BrowserLanguage() | ||
const safeValue = ['en', 'zh', 'fr', 'nl', 'de', 'it', 'es'] | ||
|
||
return safeValue.indexOf(lang) !== -1 ? lang : 'en' | ||
})() | ||
|
||
const subscriptions: Dispatch<SetStateAction<string>>[] = [] | ||
let lang = defaultLang | ||
|
||
const setLang = (newLang: string) => { | ||
lang = newLang | ||
subscriptions.forEach((subscription) => { | ||
subscription(lang) | ||
}) | ||
} | ||
|
||
type UseB3CurrentLang = { | ||
(): [string, (newLang: string) => void] | ||
} | ||
|
||
export const useB3CurrentLang: UseB3CurrentLang = () => { | ||
const [_, newSubscription] = useState(defaultLang) | ||
useEffect(() => { | ||
subscriptions.push(newSubscription) | ||
}, []) | ||
return [lang, setLang] | ||
} |
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,13 @@ | ||
import React from 'react' | ||
import { useB3Lang } from './useB3Lang' | ||
|
||
export const withB3Lang = (WrappedComponent: typeof React.Component) => function (props: any) { | ||
const b3lang = useB3Lang() | ||
|
||
return ( | ||
<WrappedComponent | ||
b3lang={b3lang} | ||
{...props} | ||
/> | ||
) | ||
} |