Skip to content

Commit

Permalink
feat: pt-BR json create
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed May 28, 2024
1 parent 4d9396c commit 451b7cb
Show file tree
Hide file tree
Showing 5 changed files with 456 additions and 9 deletions.
6 changes: 5 additions & 1 deletion docs/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Image from "next/image";
import Link from "next/link";

import translation from "@/components/Internationalization";
import translation, { getBrowserLang } from "@/components/Internationalization";
import MainBg from "@/components/MainBg";
import { merriweather, oswald, playfair, roboto100, sofiaPro } from "@/fonts";

export default function Home() {
const browserLang = getBrowserLang();

console.log("Browser language:", browserLang);

const t = (text: string) => translation({ text, subject: "HomePage" });

return (
Expand Down
7 changes: 5 additions & 2 deletions docs/src/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { FaYoutube, FaInstagram, FaGithub, FaLinkedin } from "react-icons/fa";

import { playfair, sofiaPro } from "@/fonts";

import translation from "../Internationalization";
import translation, { getBrowserLang } from "../Internationalization";

export default function Footer() {
const t = (text: string) => translation({ text, subject: "Footer" });
const browserLang = getBrowserLang();

const t = (text: string) =>
translation({ text, subject: "Footer", language: browserLang });

const path = usePathname();

Expand Down
7 changes: 5 additions & 2 deletions docs/src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import { FaCaretDown } from "react-icons/fa";

import { roboto700 } from "@/fonts";

import translation from "../Internationalization";
import translation, { getBrowserLang } from "../Internationalization";

export default function Header() {
const t = (text: string) => translation({ text, subject: "Header" });
const browserLang = getBrowserLang();

const t = (text: string) =>
translation({ text, subject: "Header", language: browserLang });

const [isSubMenuOpen, setIsSubMenuOpen] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);
Expand Down
48 changes: 44 additions & 4 deletions docs/src/components/Internationalization/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import enJson from "@/locales/en/en.json";
import ptJson from "@/locales/pt/pt.json";

type Langs = "en" | "pt";
// type Langs = "en" | "pt";

interface TranslationProps {
text: string;
subject?: keyof typeof enJson | "";
language?: Langs;
language?: readonly string[] | null;
}

interface Locale {
Expand All @@ -14,9 +15,27 @@ interface Locale {

function translation({
text,
language = "en",
language = null,
subject = "",
}: TranslationProps): string {
if (language) {
for (const lang of language) {
if (lang.includes("pt")) {
return portugueseTreatment({ text, subject });
}
if (lang.includes("en")) {
return englishTreatment({ text, subject });
}
}
}

// Default to English if no matching language is found
return englishTreatment({ text, subject });
}

export default translation;

function englishTreatment({ text, subject }: TranslationProps): string {
const english: Locale = enJson;

let enText: string | object = english[text];
Expand All @@ -32,4 +51,25 @@ function translation({
return enText;
}

export default translation;
function portugueseTreatment({ text, subject }: TranslationProps): string {
const portuguese: Locale = ptJson;

let ptText: string | object = portuguese[text];

if (subject && text in portuguese[subject]) {
ptText = portuguese[subject][text];
}

if (typeof ptText === "object" || !ptText) {
return text;
}

return ptText;
}

export function getBrowserLang() {
if (typeof window !== "undefined") {
return navigator.languages;
}
return null;
}
Loading

0 comments on commit 451b7cb

Please sign in to comment.