Skip to content

Commit

Permalink
feat: complete multilanguage
Browse files Browse the repository at this point in the history
BUN-88
  • Loading branch information
kity-bundle authored and b3aton committed Jul 12, 2022
1 parent 6d75d21 commit 32a4946
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HashRouter, Route, Routes } from 'react-router-dom'
import { useB3AppOpen } from '@b3/hooks'
import { useB3Lang } from '@b3/lang'

import { ThemeFrame } from './ThemeFrame'
import { Home, Form } from './pages'
Expand All @@ -15,10 +16,12 @@ body {

export default function App() {
const [isOpen, setIsOpen] = useB3AppOpen(false)
const b3Lang = useB3Lang()

return (
<HashRouter>
<div className="bundle-app">
<div>{b3Lang('intl.users.register')}</div>
<ThemeFrame
className={isOpen ? 'active-frame' : undefined}
fontUrl={FONT_URL}
Expand Down
5 changes: 5 additions & 0 deletions apps/storefront/src/locales/en-US/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import users from './users'

export const en = {
...users,
}
3 changes: 3 additions & 0 deletions apps/storefront/src/locales/en-US/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
'intl.users.register': 'register',
}
2 changes: 2 additions & 0 deletions apps/storefront/src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { en } from './en-US'
export { zh } from './zh-CN'
5 changes: 5 additions & 0 deletions apps/storefront/src/locales/zh-CN/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import users from './users'

export const zh = {
...users,
}
3 changes: 3 additions & 0 deletions apps/storefront/src/locales/zh-CN/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
'intl.users.register': '注册',
}
9 changes: 8 additions & 1 deletion apps/storefront/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import ReactDOM from 'react-dom/client'
import { LangProvider } from '@b3/lang'

import App from './App'

import './main.css'
import * as locales from './locales'

const CONTAINER_ID = 'bundle-container'

Expand All @@ -14,4 +17,8 @@ if (!container) {

container.className = 'bundle-namespace'

ReactDOM.createRoot(container).render(<App />)
ReactDOM.createRoot(container).render(
<LangProvider locales={locales}>
<App />
</LangProvider>,
)
28 changes: 28 additions & 0 deletions packages/lang/LangProvider.tsx
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>
)
}
2 changes: 2 additions & 0 deletions packages/lang/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { LangProvider } from './LangProvider'
export { useB3Lang } from './useB3Lang'
1 change: 1 addition & 0 deletions packages/lang/locales/en-US/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const en = {}
1 change: 1 addition & 0 deletions packages/lang/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { en } from './en-US'
9 changes: 9 additions & 0 deletions packages/lang/package.json
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"
}
}
24 changes: 24 additions & 0 deletions packages/lang/theme.ts
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
10 changes: 10 additions & 0 deletions packages/lang/useB3Lang.ts
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) : '')
}
6 changes: 6 additions & 0 deletions packages/utils/BrowserLanguage.ts
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
2 changes: 1 addition & 1 deletion packages/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { }
export { default as BrowserLanguage } from './BrowserLanguage'

0 comments on commit 32a4946

Please sign in to comment.