Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base setup for easy translations in frontend #145

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions frontend/package-lock.json

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

4 changes: 4 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"eslint-plugin-tsdoc": "^0.2.17",
"i18next": "^23.10.1",
"i18next-browser-languagedetector": "^7.2.0",
"i18next-http-backend": "^2.5.0",
"react-i18next": "^14.1.0",
"typescript": "^5.2.2",
"vite": "^5.1.0"
}
Expand Down
7 changes: 7 additions & 0 deletions frontend/public/locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"homepage": "Homepage",
"myProjects": "My Projects",
"myCourses": "My Courses",
"login": "Login",
"home": "Home"
}
7 changes: 7 additions & 0 deletions frontend/public/locales/nl/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"homepage": "Homepage",
"myProjects": "Mijn Projecten",
"myCourses": "Mijn Vakken",
"login": "Login",
"home": "Home"
}
4 changes: 3 additions & 1 deletion frontend/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
Typography,
} from "@mui/material";
import MenuIcon from "@mui/icons-material/Menu";
import { useTranslation } from "react-i18next";

/**
* The header component for the application that will be rendered at the top of the page.
* @returns - The header component
*/
export function Header(): JSX.Element {
const { t } = useTranslation();
return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="sticky">
Expand All @@ -21,7 +23,7 @@ export function Header(): JSX.Element {
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
Home
{t('home')}
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en',
debug: true,

interpolation: {
escapeValue: false,
}
});

export default i18n;
1 change: 1 addition & 0 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import './i18n'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useTranslation } from "react-i18next";

/**
* This component is the home page component that will be rendered when on the index route.
* @returns - The home page component
*/
export default function Home() {
const { t } = useTranslation();
return (
<div>
<h1>HomePage</h1>
<h1>{t('homepage')}</h1>
</div>
);
}