Skip to content

Commit

Permalink
i18n: Proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhjacobs committed Nov 13, 2024
1 parent 38eb696 commit f43531c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 3 deletions.
14 changes: 14 additions & 0 deletions i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const en = require("./translations.en.json");
const es = require("./translations.es.json");

const i18n = {
translations: {
en,
es,
},
defaultLang: "en",
useBrowserDefault: true,
languageDataStore: "localStorage",
};

module.exports = i18n;
3 changes: 3 additions & 0 deletions i18n/translations.en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tagline": "An open source Flash Player emulator"
}
3 changes: 3 additions & 0 deletions i18n/translations.es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tagline": "Un emulador de Flash Player de código abierto"
}
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"feed": "^4.2.2",
"gray-matter": "^4.0.3",
"jsdom": "^25.0.1",
"next-export-i18n": "^3.0.0",
"octokit": "^4.0.2",
"postcss": "^8.4.49",
"postcss-preset-mantine": "^1.17.0",
Expand Down
3 changes: 3 additions & 0 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ a:link,
a:visited {
color: var(--ruffle-orange-3);
}
span[data-language-switcher] {
display: none;
}
6 changes: 4 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import dynamic from "next/dynamic";
import { useTranslation } from "next-export-i18n";
import classes from "./index.module.css";
import {
Container,
Expand All @@ -26,6 +27,7 @@ const Installers = dynamic(() => import("./installers"), {
});

export default function Home() {
const { t } = useTranslation();
const [latest, setLatest] = React.useState<GithubRelease | null>(null);

React.useEffect(() => {
Expand All @@ -45,8 +47,8 @@ export default function Home() {
<InteractiveLogo className={classes.logo} />

<Container size="md">
<Title className={classes.title}>
An open source Flash Player emulator
<Title className={classes.title} suppressHydrationWarning>
{t("tagline")}
</Title>
<div className={classes.hero}>
<Image
Expand Down
6 changes: 6 additions & 0 deletions src/components/header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
}
}

.hiddenOnDesktop {
@media (min-width: $mantine-breakpoint-md) {
display: none;
}
}

.burger {
--burger-color: var(--ruffle-orange);
}
Expand Down
61 changes: 60 additions & 1 deletion src/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import classes from "./header.module.css";
import Image from "next/image";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { LanguageSwitcher } from "next-export-i18n";
import { useState, useEffect } from "react";

const links = [
{ link: "/", label: "About Ruffle" },
Expand All @@ -21,8 +23,42 @@ const links = [
target: "_blank",
},
];
const languages = {
en: "English",
es: "Español",
};

export function Header() {
const [selectedLang, setSelectedLang] = useState("en");
useEffect(() => {
if (typeof window !== "undefined") {
const browserLang =
window.navigator.language || window.navigator.languages
? (window.navigator.language || window.navigator.languages[0])
.split("-")[0]
.toLowerCase()
: "en";
setSelectedLang(browserLang);
const selectedLang = window.localStorage
? window.localStorage.getItem("next-export-i18n-lang")
: null;
if (selectedLang) {
setSelectedLang(selectedLang);
}
}
}, []);

const handleLanguageChange = (event) => {
const newLang = event.target.value;
setSelectedLang(newLang);
// Trigger the LanguageSwitcher programmatically
document
.querySelector(
`[data-language-switcher][aria-label='set language to ${newLang}']`,
)
.click();
};

const [opened, { toggle, close }] = useDisclosure(false);
const pathname = usePathname();

Expand All @@ -43,7 +79,7 @@ export function Header() {

return (
<header className={classes.header}>
<Container size="md" className={classes.inner}>
<Container size="lg" className={classes.inner}>
<Link href="/">
<Image
src="/logo.svg"
Expand All @@ -55,7 +91,19 @@ export function Header() {
</Link>
<Group gap={5} visibleFrom="md">
{items}
<select value={selectedLang} onChange={handleLanguageChange}>
{Object.entries(languages).map(([langCode, langName]) => (
<option key={langCode} value={langCode}>
{langName}
</option>
))}
</select>
</Group>{" "}
{Object.keys(languages).map((langCode) => (
<LanguageSwitcher key={langCode} lang={langCode}>
{languages[langCode]}
</LanguageSwitcher>
))}
<Drawer
opened={opened}
onClose={close}
Expand All @@ -69,6 +117,17 @@ export function Header() {
>
{items}
</Drawer>
<select
className={classes.hiddenOnDesktop}
value={selectedLang}
onChange={handleLanguageChange}
>
{Object.entries(languages).map(([langCode, langName]) => (
<option key={langCode} value={langCode}>
{langName}
</option>
))}
</select>
<Burger
opened={opened}
onClick={toggle}
Expand Down

0 comments on commit f43531c

Please sign in to comment.