-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add support for multiple languages * feat: persist language preference * feat: add browser language detection and set current page language * remove localStorage.clear() used for testing
- Loading branch information
1 parent
96d16cf
commit 076b9bb
Showing
15 changed files
with
581 additions
and
17 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,81 @@ | ||
'use client'; | ||
import React, { useEffect, useState } from 'react'; | ||
import clsx from 'clsx'; | ||
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; | ||
import { useLang } from '@/app/context/LangContext'; | ||
|
||
const LanguageMenu = () => { | ||
const { lang, setLang } = useLang(); | ||
const [mounted, setMounted] = useState(false); | ||
|
||
const languageNames = { | ||
en: 'English', | ||
zh: '简体中文', | ||
hant: '繁體中文', | ||
ja: '日本語', | ||
fr: 'Français', | ||
de: 'Deutsch', | ||
}; | ||
|
||
const handleLangChange = (newLang) => { | ||
setLang(newLang); | ||
}; | ||
|
||
const currentLanguage = languageNames[lang] || 'Unknown'; | ||
|
||
useEffect(() => { | ||
setMounted(true); | ||
}, []); | ||
|
||
if (!mounted) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<DropdownMenu.Root> | ||
<DropdownMenu.Trigger> | ||
<button | ||
className={clsx( | ||
'bg-transparent', | ||
'cursor-pointer', | ||
'flex items-center', | ||
'transition-colors duration-500', | ||
'rounded', | ||
'px-2 py-1', | ||
'border border-[#453d7d]', | ||
'text-[#453d7a]', | ||
'bg-[#efefef]', | ||
'hover:bg-[#453d7d] hover:text-white', | ||
)} | ||
> | ||
<img | ||
src="LanguageSwitching.svg" | ||
alt="Language" | ||
className="w-6 h-6 mr-1" | ||
style={{ | ||
transition: 'filter 0.5s', | ||
}} | ||
/> | ||
{currentLanguage} | ||
</button> | ||
</DropdownMenu.Trigger> | ||
<DropdownMenu.Content sideOffset={5} className="bg-white rounded-md p-2 z-10"> | ||
{Object.keys(languageNames).map((code) => { | ||
return ( | ||
<DropdownMenu.Item | ||
key={code} | ||
onSelect={() => { | ||
handleLangChange(code); | ||
}} | ||
className="p-2 cursor-pointer" | ||
> | ||
{languageNames[code]} | ||
</DropdownMenu.Item> | ||
); | ||
})} | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Root> | ||
); | ||
}; | ||
|
||
export default LanguageMenu; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use client'; | ||
import { createContext, useContext, useState, useEffect, ReactNode } from 'react'; | ||
|
||
const translations = { | ||
en: require('../../messages/en.json'), | ||
zh: require('../../messages/zh.json'), | ||
zhHant: require('../../messages/zh-Hant.json'), | ||
ja: require('../../messages/ja.json'), | ||
fr: require('../../messages/fr.json'), | ||
de: require('../../messages/de.json'), | ||
}; | ||
|
||
type LangContextType = { | ||
lang: string; | ||
setLang: (lang: string) => void; | ||
t: (key: string) => string; | ||
}; | ||
|
||
const LangContext = createContext<LangContextType | undefined>(undefined); | ||
|
||
const langMapping = { | ||
'zh-Hant': 'zhHant', | ||
}; | ||
|
||
const getTranslationKey = (lang) => { | ||
return langMapping[lang] || lang; | ||
}; | ||
|
||
export const LangProvider = ({ children }: { children: ReactNode }) => { | ||
const [lang, setLangState] = useState('en'); | ||
const [isLoading, setIsLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
const savedLang = localStorage.getItem('lang'); | ||
|
||
if (savedLang) { | ||
setLangState(savedLang); | ||
} else { | ||
const browserLang = navigator.language.split('-')[0]; | ||
const supportedLangs = ['en', 'zh', 'zh-Hant', 'ja', 'fr', 'de']; | ||
const defaultLang = supportedLangs.includes(browserLang) ? browserLang : 'en'; | ||
setLangState(defaultLang); | ||
localStorage.setItem('lang', defaultLang); | ||
} | ||
setIsLoading(false); | ||
}, []); | ||
|
||
const setLang = (newLang: string) => { | ||
setLangState(newLang); | ||
localStorage.setItem('lang', newLang); | ||
}; | ||
|
||
const t = (key: string) => { | ||
const langKey = getTranslationKey(lang); | ||
const value = translations[langKey][key]; | ||
return value || key; | ||
}; | ||
|
||
if (isLoading) { | ||
return null; | ||
} | ||
|
||
return <LangContext.Provider value={{ lang, setLang, t }}>{children}</LangContext.Provider>; | ||
}; | ||
|
||
export const useLang = () => { | ||
const context = useContext(LangContext); | ||
if (!context) { | ||
throw new Error('useLang must be used within a LangProvider'); | ||
} | ||
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
button:hover img { | ||
filter: brightness(0) invert(1); | ||
} |
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,14 @@ | ||
{ | ||
"Custom config": "Benutzerdefinierte Konfiguration", | ||
"Model": "Modell", | ||
"Select your model": "Wählen Sie Ihr Modell", | ||
"RESET": "ZURÜCKSETZEN", | ||
"Policy": "Strategie", | ||
"Request": "Anfrage", | ||
"Enforcement Result": "Durchsetzungsergebnis", | ||
"Why this result": "Warum dieses Ergebnis?", | ||
"SYNTAX VALIDATE": "SYNTAX VALIDIEREN", | ||
"RUN THE TEST": "DEN TEST AUSFÜHREN", | ||
"SHARE": "TEILEN", | ||
"COPY": "KOPIEREN" | ||
} |
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,14 @@ | ||
{ | ||
"Custom config": "Custom config", | ||
"Model": "Model", | ||
"Select your model": "Select your model", | ||
"RESET": "RESET", | ||
"Policy": "Policy", | ||
"Request": "Request", | ||
"Enforcement Result": "Enforcement Result", | ||
"Why this result": "Why this result?", | ||
"SYNTAX VALIDATE": "SYNTAX VALIDATE", | ||
"RUN THE TEST": "RUN THE TEST", | ||
"SHARE": "SHARE", | ||
"COPY": "COPY" | ||
} |
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,14 @@ | ||
{ | ||
"Custom config": "Configuration personnalisée", | ||
"Model": "Modèle", | ||
"Select your model": "Sélectionnez votre modèle", | ||
"RESET": "RÉINITIALISER", | ||
"Policy": "Stratégie", | ||
"Request": "Demande", | ||
"Enforcement Result": "Résultat de l'exécution", | ||
"Why this result": "Pourquoi ce résultat?", | ||
"SYNTAX VALIDATE": "VALIDER LA SYNTAXE", | ||
"RUN THE TEST": "EXÉCUTER LE TEST", | ||
"SHARE": "PARTAGER", | ||
"COPY": "COPIER" | ||
} |
Oops, something went wrong.