-
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.
Showing
16 changed files
with
111 additions
and
2 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,5 @@ | ||
import users from './users' | ||
|
||
export const en = { | ||
...users, | ||
} |
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 { | ||
'intl.users.register': 'register', | ||
} |
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,2 @@ | ||
export { en } from './en-US' | ||
export { zh } from './zh-CN' |
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,5 @@ | ||
import users from './users' | ||
|
||
export const zh = { | ||
...users, | ||
} |
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 { | ||
'intl.users.register': '注册', | ||
} |
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,28 @@ | ||
import { IntlProvider, MessageFormatElement } from 'react-intl' | ||
import React from 'react' | ||
import { ThemeProvider, Theme } from '@mui/material/styles' | ||
|
||
import { BrowserLanguage } from '@b3/utils' | ||
|
||
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() | ||
|
||
return ( | ||
<IntlProvider messages={locales[lang] || locales.en} locale={lang} defaultLocale="en"> | ||
<ThemeProvider theme={theme(lang)}> | ||
{children} | ||
</ThemeProvider> | ||
</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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { LangProvider } from './LangProvider' | ||
export { useB3Lang } from './useB3Lang' |
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 @@ | ||
export const en = {} |
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 @@ | ||
export { en } from './en-US' |
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,9 @@ | ||
{ | ||
"name": "@b3/lang", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"react-intl": "^5.20.3" | ||
} | ||
} |
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,24 @@ | ||
import { createTheme } from '@mui/material/styles' | ||
import * as materialMultiLanguages from '@mui/material/locale' | ||
|
||
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 = { | ||
[index: string]: materialMultiLanguages.Localization | ||
} | ||
|
||
const theme = (lang: string) => createTheme({}, (materialMultiLanguages as MaterialMultiLanguagesType)[MUI_LANG_MAP[lang] || 'enUS']) | ||
|
||
export default theme |
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,10 @@ | ||
import { useIntl } from 'react-intl' | ||
|
||
export const useB3Lang = () => { | ||
const intl = useIntl() | ||
return (id: string, options = {}) => ( | ||
id ? intl.formatMessage({ | ||
id, | ||
defaultMessage: id, | ||
}, options) : '') | ||
} |
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,6 @@ | ||
const BrowserLanguage = () => { | ||
const lang = navigator.language | ||
return lang.substring(0, 2) | ||
} | ||
|
||
export default BrowserLanguage |
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 +1 @@ | ||
export { } | ||
export { default as BrowserLanguage } from './BrowserLanguage' |