-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multiple language support (#37)
* feat: multiple language support * feat: multiple languagee support * fix: language setting typo * fix: remove third language (malay) * chore: run code formatting --------- Co-authored-by: Teo Wen Long <[email protected]> Co-authored-by: satnaing <[email protected]>
- Loading branch information
1 parent
ee55c19
commit c5d163d
Showing
18 changed files
with
410 additions
and
76 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,61 @@ | ||
import { createContext, useContext, useState } from 'react' | ||
import { IntlProvider } from 'use-intl' | ||
import translations from '../translations' | ||
|
||
export type Language = 'en' | 'zh' | ||
|
||
type LanguageProviderProps = { | ||
children: React.ReactNode | ||
defaultLanguage?: Language | ||
storageKey?: string | ||
} | ||
|
||
type LanguageProviderState = { | ||
language: Language | ||
setLanguage: (lang: Language) => void | ||
} | ||
|
||
const initialState: LanguageProviderState = { | ||
language: 'en', | ||
setLanguage: () => null, | ||
} | ||
|
||
const LanguageProviderContext = | ||
createContext<LanguageProviderState>(initialState) | ||
|
||
export function LanguageProvider({ | ||
children, | ||
defaultLanguage = 'en', | ||
storageKey = 'vite-ui-language', | ||
...props | ||
}: LanguageProviderProps) { | ||
const [language, setLanguage] = useState<Language>( | ||
() => (localStorage.getItem(storageKey) as Language) || defaultLanguage | ||
) | ||
|
||
const value = { | ||
language, | ||
setLanguage: (lang: Language) => { | ||
localStorage.setItem(storageKey, lang) | ||
setLanguage(lang) | ||
}, | ||
} | ||
|
||
return ( | ||
<LanguageProviderContext.Provider {...props} value={value}> | ||
<IntlProvider locale={language} messages={translations[language]}> | ||
{children} | ||
</IntlProvider> | ||
</LanguageProviderContext.Provider> | ||
) | ||
} | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export const useLanguage = () => { | ||
const context = useContext(LanguageProviderContext) | ||
|
||
if (context === undefined) | ||
throw new Error('useLanguage must be used within a LanguageProvider') | ||
|
||
return context | ||
} |
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,49 @@ | ||
import { Language, useLanguage } from './language-provider' | ||
import { IconCheck } from '@tabler/icons-react' | ||
import { cn } from '@/lib/utils' | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from '@/components/ui/dropdown-menu' | ||
import { Button } from './custom/button' | ||
|
||
export default function LanguageSwitch() { | ||
const { language, setLanguage } = useLanguage() | ||
|
||
const languageText = new Map<string, string>([ | ||
['en', 'English'], | ||
['zh', '简体中文'], | ||
]) | ||
|
||
const renderDropdownItem = () => { | ||
return Array.from(languageText).map(([key, value]) => ( | ||
<DropdownMenuItem key={key} onClick={() => setLanguage(key as Language)}> | ||
{value}{' '} | ||
<IconCheck | ||
size={14} | ||
className={cn('ml-auto', language !== key && 'hidden')} | ||
/> | ||
</DropdownMenuItem> | ||
)) | ||
} | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
variant='outline' | ||
size='default' | ||
className='scale-95 rounded-full' | ||
> | ||
{languageText.get(language)} | ||
<span className='sr-only'>Toggle language</span> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align='end'> | ||
{renderDropdownItem()} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
) | ||
} |
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
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
Oops, something went wrong.