-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): language support of German and English
- Loading branch information
Showing
36 changed files
with
362 additions
and
138 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 @@ | ||
export type Language = 'en' | 'de' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import useLanguage from '../../hooks/useLanguage' | ||
import { Language } from '../../@types/language' | ||
|
||
const LanguageSelector = () => { | ||
const { currentLanguage, changeLanguage } = useLanguage() | ||
|
||
return ( | ||
<select | ||
className='select select-bordered select-sm w-min' | ||
defaultValue={currentLanguage} | ||
onChange={(e) => changeLanguage(e.target.value as Language)} | ||
> | ||
<option value='en'>EN</option> | ||
<option value='de'>DE</option> | ||
</select> | ||
) | ||
} | ||
|
||
export default LanguageSelector |
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
12 changes: 12 additions & 0 deletions
12
src/components/services/providers/feedback/FeedbackContext.ts
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,12 @@ | ||
import { createContext } from 'react' | ||
import { FeedbackState } from './FeedbackReducer' | ||
|
||
type FeedbackContextType = { | ||
state: FeedbackState | ||
setError: (message: string) => void | ||
removeFeedback: () => void | ||
} | ||
|
||
const FeedbackContext = createContext<FeedbackContextType | null>(null) | ||
|
||
export default FeedbackContext |
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
2 changes: 1 addition & 1 deletion
2
...nents/services/context/FeedbackReducer.ts → ...ces/providers/feedback/FeedbackReducer.ts
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,13 @@ | ||
import { PropsWithChildren } from 'react' | ||
import { LanguageProvider } from './language/LanguageProvider' | ||
import { FeedbackProvider } from './feedback/FeedbackProvider' | ||
|
||
const Providers = ({ children }: PropsWithChildren) => { | ||
return ( | ||
<LanguageProvider language='en'> | ||
<FeedbackProvider>{children} </FeedbackProvider> | ||
</LanguageProvider> | ||
) | ||
} | ||
|
||
export default Providers |
14 changes: 14 additions & 0 deletions
14
src/components/services/providers/language/LanguageContext.ts
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 @@ | ||
import { createContext } from 'react' | ||
import { Language } from '../../../../@types/language' | ||
|
||
type LanguageContextType = { | ||
currentLanguage: Language | ||
changeLanguage: (language: Language) => void | ||
getMessage: (labelId: string) => string | Array<string> | ||
} | ||
|
||
const LanguageContext = createContext<LanguageContextType>( | ||
{} as LanguageContextType | ||
) | ||
|
||
export default LanguageContext |
40 changes: 40 additions & 0 deletions
40
src/components/services/providers/language/LanguageProvider.tsx
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,40 @@ | ||
import React, { ReactNode, useState } from 'react' | ||
|
||
import LanguageContext from './LanguageContext' | ||
import en from './data/en.json' | ||
import de from './data/de.json' | ||
import { Language } from '../../../../@types/language' | ||
|
||
export type LanguageProviderProps = { | ||
language: Language | ||
children: ReactNode | ||
} | ||
|
||
export const LanguageProvider: React.FC<LanguageProviderProps> = ({ | ||
language, | ||
children, | ||
}) => { | ||
const [currentLanguage, setCurrentLanguage] = useState<Language>(language) | ||
const changeLanguage = (language: Language) => setCurrentLanguage(language) | ||
|
||
const dictionary: { | ||
[languageKey: string]: { [messageKey: string]: string } | ||
} = { en, de } | ||
Check failure on line 22 in src/components/services/providers/language/LanguageProvider.tsx
|
||
|
||
const getMessage = (messageKey: string) => { | ||
const message = dictionary[currentLanguage][messageKey] | ||
if (!message) | ||
throw new Error( | ||
`MessageKey ${messageKey} not found in ${currentLanguage}.json` | ||
) | ||
return message | ||
} | ||
|
||
return ( | ||
<LanguageContext.Provider | ||
value={{ currentLanguage, changeLanguage, getMessage }} | ||
> | ||
{children} | ||
</LanguageContext.Provider> | ||
) | ||
} |
Oops, something went wrong.