generated from oluqom/typescript-react-tailwind-vite
-
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): Widget pra trocar de línguagem 🌐
- Loading branch information
Showing
5 changed files
with
63 additions
and
1 deletion.
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
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,30 @@ | ||
// LanguageSelect.tsx | ||
import React from "react"; | ||
import { useToggleLanguage } from "./useToggleLanguage"; | ||
|
||
const LanguageSelect: React.FC = () => { | ||
const { currentLanguage, toggleLanguage, languages } = useToggleLanguage(); | ||
|
||
return ( | ||
<div className="fixed bottom-0 right-0 p-4 flex flex-col print:hidden"> | ||
{languages.map((language) => ( | ||
<button | ||
key={language.code} | ||
onClick={() => toggleLanguage(language.code)} | ||
className={`px-2 py-1 bg-gray-200 rounded cursor-pointer hover:bg-gray-300 ${ | ||
currentLanguage === language.code && "font-bold" | ||
}`} | ||
> | ||
<span className="flex items-center"> | ||
<span role="img" aria-label="Flag" className="text-2xl mr-1"> | ||
{language.emoji} | ||
</span> | ||
{language.label} | ||
</span> | ||
</button> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LanguageSelect; |
20 changes: 20 additions & 0 deletions
20
src/components/molecules/LanguageSelect/useToggleLanguage.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,20 @@ | ||
// useLanguage.ts | ||
import { languages } from "@/utils/i18n/languages"; | ||
import { useEffect, useState } from "react"; | ||
import { useT } from "talkr"; | ||
|
||
export const useToggleLanguage = () => { | ||
const { setLocale, locale } = useT(); | ||
|
||
const [currentLanguage, setCurrentLanguage] = useState<string>(locale); | ||
|
||
useEffect(() => { | ||
setCurrentLanguage(locale); | ||
}, [locale]); | ||
|
||
const toggleLanguage = (languageCode: string) => { | ||
setLocale(languageCode); | ||
}; | ||
|
||
return { currentLanguage, toggleLanguage, languages }; | ||
}; |
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,10 @@ | ||
interface Language { | ||
code: string; | ||
emoji: string; | ||
label: string; | ||
} | ||
|
||
export const languages: Language[] = [ | ||
{ code: "en", emoji: "🇺🇸", label: "EN" }, | ||
{ code: "pt", emoji: "🇧🇷", label: "PT" }, | ||
]; |