-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(localizationprovider): added localizationWrapper
- Loading branch information
1 parent
a0ad778
commit 2b7fe38
Showing
1 changed file
with
44 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* istanbul ignore file */ | ||
import React, { useContext } from 'react'; | ||
import { ILocalizedStrings } from './ILocalizedStrings'; | ||
import { defaultLocale } from './defaultLocalization'; | ||
import { LocaleType } from './ILocalizationContext'; | ||
import { LocalizationContext, LocalizationProvider } from './localizationProvider'; | ||
|
||
interface IProps { | ||
customLocales?: Record<LocaleType, Partial<ILocalizedStrings>>; | ||
} | ||
|
||
export const LocalizationWrapper: React.FC<IProps> = ({ customLocales, children }) => { | ||
const Wrapper: React.FC<IProps> = ({ customLocales, children }) => { | ||
const { currentlocalization, switchTo } = useContext(LocalizationContext); | ||
const availableThemes = [...Object.keys(customLocales ?? []), defaultLocale]; | ||
|
||
return ( | ||
<> | ||
<div style={{ marginBottom: '1rem' }}> | ||
<p> | ||
Locale is <b>{currentlocalization.locale}</b> | ||
<br /> | ||
<small> | ||
It's saved into your browser's localStorage, so this locale will persist if | ||
you refresh the page. | ||
</small> | ||
</p> | ||
{availableThemes.map((key, i) => ( | ||
<button style={{ margin: '0 0.2rem' }} key={i.toString()} onClick={() => switchTo(key)}> | ||
Go {key} | ||
</button> | ||
))} | ||
</div> | ||
|
||
{children} | ||
</> | ||
); | ||
}; | ||
return ( | ||
<LocalizationProvider customLocales={customLocales}> | ||
<Wrapper customLocales={customLocales}>{children}</Wrapper> | ||
</LocalizationProvider> | ||
); | ||
}; |