Skip to content

Commit

Permalink
setup switch between locales
Browse files Browse the repository at this point in the history
  • Loading branch information
zenoxs committed Jul 20, 2022
1 parent e6341a9 commit 628ceb1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/main/utils/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export type I18nEvents = {
}

export class I18n extends (EventEmitter as new () => TypedEmitter<I18nEvents>) {
private _locale: Locales = 'en'
private _locale: Locales

constructor(private readonly _rootStore: RootStore) {
super()
this._locale = _rootStore.appStore.language as Locales
observe(this._rootStore.appStore, 'language', () => {
this._locale = this._rootStore.appStore.language as Locales
this.emit('localeChanged', this._locale)
Expand Down
2 changes: 1 addition & 1 deletion packages/main/windows/game-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class GameWindow extends (EventEmitter as new () => TypedEmitter<GameWind

this._win.loadURL(url)
if (process.env.NODE_ENV === 'development') {
// this._win.webContents.openDevTools({ mode: 'detach' })
this._win.webContents.openDevTools({ mode: 'detach' })
}
}
// Make all links open with the browser, not with the application
Expand Down
20 changes: 11 additions & 9 deletions packages/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ThemeProvider } from '@mui/material/styles'
import React, { useEffect, useMemo, useRef, useState } from 'react'
import { setupRootStore, RootStore, RootStoreProvider } from './store'
import { Navigator } from './navigation'
import { GameContextProvider } from './providers'
import { GameContextProvider, LocalProvider } from './providers'
import { GameContext } from '@lindo/shared'
import { TypesafeI18n } from '@lindo/i18n'
import { Locales, TypesafeI18n } from '@lindo/i18n'
import { CssBaseline } from '@mui/material'
import { darkTheme, lightTheme } from './themes'

Expand Down Expand Up @@ -45,14 +45,16 @@ export const App = () => {
if (!rootStore || !gameContext) return null

return (
<TypesafeI18n locale='en'>
<TypesafeI18n locale={rootStore.appStore.language as Locales}>
<RootStoreProvider value={rootStore}>
<GameContextProvider value={gameContext}>
<ThemeProvider theme={theme}>
<CssBaseline />
<Navigator />
</ThemeProvider>
</GameContextProvider>
<LocalProvider>
<GameContextProvider value={gameContext}>
<ThemeProvider theme={theme}>
<CssBaseline />
<Navigator />
</ThemeProvider>
</GameContextProvider>
</LocalProvider>
</RootStoreProvider>
</TypesafeI18n>
)
Expand Down
1 change: 1 addition & 0 deletions packages/renderer/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './game-context'
export * from './local'
27 changes: 27 additions & 0 deletions packages/renderer/src/providers/local.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useStores } from '@/store'
import { Locales, useI18nContext } from '@lindo/i18n'
import { reaction } from 'mobx'
import React, { ReactNode, useEffect } from 'react'

export interface LocalProviderProps {
children: ReactNode
}

export const LocalProvider = ({ children }: LocalProviderProps) => {
const { locale, setLocale } = useI18nContext()
const { appStore } = useStores()

useEffect(() => {
return reaction(
() => appStore.language,
(language) => {
if (language !== locale) {
setLocale(language as Locales)
}
},
{ fireImmediately: true }
)
})

return <>{children}</>
}

0 comments on commit 628ceb1

Please sign in to comment.