-
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): create button for toggle language
- Loading branch information
Showing
15 changed files
with
1,362 additions
and
438 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
Large diffs are not rendered by default.
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
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
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 @@ | ||
import ButtonCustom from "../ButtonCustom"; | ||
import LanguageSvg from "../../assets/icons/language.svg"; | ||
import React from "react"; | ||
import styles from "./styles.module.css"; | ||
import { changeLanguage } from "i18next"; | ||
import { useTranslation } from "react-i18next"; | ||
import Typography from "../Typography"; | ||
import i18next from "i18next"; | ||
|
||
export default function LanguageSwitch() { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
const { t } = useTranslation(); | ||
const currentLanguage = i18next.language; | ||
|
||
function handleClick() { | ||
setIsOpen(!isOpen); | ||
} | ||
|
||
function handleToggleLanguage(lng: "en" | "pt") { | ||
changeLanguage(lng); | ||
} | ||
|
||
return ( | ||
<div> | ||
<ButtonCustom icon={<LanguageSvg />} onClick={handleClick} /> | ||
{isOpen && ( | ||
<ul className={styles["language-switch__list"]}> | ||
<li | ||
className={`${styles["language-switch__item"]} ${currentLanguage === "en" && styles["language-switch__item--active"]}`} | ||
onClick={() => handleToggleLanguage("en")} | ||
> | ||
<Typography variant="body2">{t("languages.en.label")}</Typography> | ||
</li> | ||
<li | ||
className={`${styles["language-switch__item"]} ${currentLanguage === "pt" && styles["language-switch__item--active"]}`} | ||
onClick={() => handleToggleLanguage("pt")} | ||
> | ||
<Typography variant="body2">{t("languages.pt.label")}</Typography> | ||
</li> | ||
</ul> | ||
)} | ||
</div> | ||
); | ||
} |
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,27 @@ | ||
.language-switch__list { | ||
list-style-type: none; | ||
display: flex; | ||
flex-direction: column; | ||
position: absolute; | ||
} | ||
|
||
.language-switch__item { | ||
padding: 0.25rem 0.5rem; | ||
border-top: 0.1rem solid var(--background); | ||
cursor: pointer; | ||
background-color: var(--primary); | ||
color: var(--background); | ||
transition: background-color 0.2s ease-in-out; | ||
} | ||
|
||
.language-switch__item:hover { | ||
background-color: var(--secondary); | ||
} | ||
|
||
.language-switch__item:first-child { | ||
border-top: none; | ||
} | ||
|
||
.language-switch__item--active { | ||
background-color: var(--tertiary); | ||
} |
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,31 @@ | ||
import styles from "./styles.module.css"; | ||
|
||
interface TypographyProps { | ||
variant: | ||
| "h1" | ||
| "h2" | ||
| "h3" | ||
| "h4" | ||
| "body1" | ||
| "body2" | ||
| "body3" | ||
| "caption1" | ||
| "caption2"; | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function Typography({ variant, children }: TypographyProps) { | ||
return ( | ||
<> | ||
{variant === "h1" && <h1 className={styles.headline1}>{children}</h1>} | ||
{variant === "h2" && <h2 className={styles.headline2}>{children}</h2>} | ||
{variant === "h3" && <h3 className={styles.headline3}>{children}</h3>} | ||
{variant === "h4" && <h4 className={styles.headline4}>{children}</h4>} | ||
{variant === "body1" && <p className={styles.body1}>{children}</p>} | ||
{variant === "body2" && <p className={styles.body2}>{children}</p>} | ||
{variant === "body3" && <p className={styles.body3}>{children}</p>} | ||
{variant === "caption1" && <p className={styles.caption1}>{children}</p>} | ||
{variant === "caption2" && <p className={styles.caption2}>{children}</p>} | ||
</> | ||
); | ||
} |
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,53 @@ | ||
.headline1 { | ||
font-size: 3rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.headline2 { | ||
font-size: 2.5rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.headline3 { | ||
font-size: 2rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.headline4 { | ||
font-size: 1.5rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.body1 { | ||
font-size: 1rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.body2 { | ||
font-size: 1rem; | ||
font-weight: 400; | ||
line-height: 120%; | ||
} | ||
|
||
.body3 { | ||
font-size: 0.875rem; | ||
font-weight: 400; | ||
line-height: 120%; | ||
} | ||
|
||
.caption1 { | ||
font-size: 0.75rem; | ||
font-weight: 700; | ||
line-height: 120%; | ||
} | ||
|
||
.caption2 { | ||
font-size: 0.75rem; | ||
font-weight: 400; | ||
line-height: 120%; | ||
} |
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,60 @@ | ||
import i18n from "i18next"; | ||
import { initReactI18next } from "react-i18next"; | ||
import LanguageDetector from "i18next-browser-languagedetector"; | ||
import moment from "moment"; | ||
import "moment/dist/locale/pt"; | ||
|
||
import pt from "./locales/pt.json"; | ||
import en from "./locales/en.json"; | ||
|
||
const options = { | ||
// Order and from where user language should be detected. | ||
order: ["querystring", "localStorage", "navigator", "path"], | ||
|
||
// Keys or params to lookup language from: | ||
lookupQuerystring: "lng", | ||
lookupLocalStorage: "i18nextLng", | ||
lookupFromPathIndex: 0, | ||
|
||
// Cache user language on: | ||
caches: ["localStorage"], | ||
|
||
// Only detect languages that are in the whitelist. | ||
checkWhitelist: true, | ||
}; | ||
|
||
i18n | ||
.use(LanguageDetector) | ||
.use(initReactI18next) | ||
.init({ | ||
resources: { | ||
pt: { translation: pt }, | ||
en: { translation: en }, | ||
}, | ||
fallbackLng: ["en"], | ||
supportedLngs: ["pt", "en"], | ||
detection: options, | ||
|
||
interpolation: { | ||
escapeValue: false, | ||
// Format items to each language format. | ||
format(value, format, lng) { | ||
if (format === "number" && value) return value.toLocaleString(lng); | ||
|
||
if (format === "roundNumber" && value) { | ||
value = Math.round(value); | ||
return value.toLocaleString(lng); | ||
} | ||
|
||
if (value instanceof Date) { | ||
return moment(value) | ||
.locale(lng || "") | ||
.format(format); | ||
} | ||
|
||
return value; | ||
}, | ||
}, | ||
}); | ||
|
||
export default i18n; |
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 @@ | ||
{ | ||
"languages": { | ||
"en": { | ||
"label": "English" | ||
}, | ||
"pt": { | ||
"label": "Portuguese" | ||
} | ||
} | ||
} |
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 @@ | ||
{ | ||
"languages": { | ||
"en": { | ||
"label": "Inglês" | ||
}, | ||
"pt": { | ||
"label": "Português" | ||
} | ||
} | ||
} |
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