Skip to content

Commit

Permalink
Add translation support using i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
aceArt-GmbH committed Dec 11, 2023
1 parent 2a1bf4a commit 66fc90b
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 11 deletions.
124 changes: 115 additions & 9 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"formik": "2.2.9",
"html-dom-parser": "4.0.0",
"html-react-parser": "4.2.0",
"i18next": "23.7.8",
"i18next-browser-languagedetector": "7.2.0",
"i18next-http-backend": "2.4.2",
"immer": "9.0.16",
"is-hotkey": "0.2.0",
"jotai": "1.12.0",
Expand All @@ -64,6 +67,7 @@
"react-dom": "17.0.2",
"react-error-boundary": "4.0.10",
"react-google-recaptcha": "2.1.0",
"react-i18next": "13.5.0",
"react-modal": "3.16.1",
"react-range": "1.8.14",
"sanitize-html": "2.8.0",
Expand Down
12 changes: 12 additions & 0 deletions public/locales/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Organisms": {
"Settings": {
"theme": {
"follow_system": {
"title": "System-Theme verwenden",
"description": "Verwende den hellen oder dunklen Modus basierend auf den Systemeinstellungen."
}
}
}
}
}
12 changes: 12 additions & 0 deletions public/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Organisms": {
"Settings": {
"theme": {
"follow_system": {
"title": "Follow system theme",
"description": "Use light or dark mode based on the system settings."
}
}
}
}
}
30 changes: 30 additions & 0 deletions src/app/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend, { HttpBackendOptions } from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

i18n
// i18next-http-backend
// loads translations from your server
// https://github.com/i18next/i18next-http-backend
.use(Backend)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init<HttpBackendOptions>({
debug: false,
fallbackLng: 'en',
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
load: 'languageOnly',
backend: {
loadPath: 'public/locales/{{lng}}.json',
},
});

export default i18n;
7 changes: 5 additions & 2 deletions src/app/organisms/settings/Settings.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect } from 'react';
import './Settings.scss';
import { useTranslation } from 'react-i18next';

import initMatrix from '../../../client/initMatrix';
import cons from '../../../client/state/cons';
Expand Down Expand Up @@ -64,19 +65,21 @@ function AppearanceSection() {
const [showHiddenEvents, setShowHiddenEvents] = useSetting(settingsAtom, 'showHiddenEvents');
const spacings = ['0', '100', '200', '300', '400', '500']

const { t } = useTranslation();

return (
<div className="settings-appearance">
<div className="settings-appearance__card">
<MenuHeader>Theme</MenuHeader>
<SettingTile
title="Follow system theme"
title={t('Organisms.Settings.theme.follow_system.title')}
options={(
<Toggle
isActive={settings.useSystemTheme}
onToggle={() => { toggleSystemTheme(); updateState({}); }}
/>
)}
content={<Text variant="b3">Use light or dark mode based on the system settings.</Text>}
content={<Text variant="b3">{t('Organisms.Settings.theme.follow_system.description')}</Text>}
/>
<SettingTile
title="Theme"
Expand Down
3 changes: 3 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import settings from './client/state/settings';

import App from './app/pages/App';

// import i18n (needs to be bundled ;))
import './app/i18n';

document.body.classList.add(configClass, varsClass);

settings.applyTheme();
Expand Down
4 changes: 4 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const copyFiles = {
src: 'public/res/android',
dest: 'public/',
},
{
src: 'public/locales',
dest: 'public/',
},
],
}

Expand Down

0 comments on commit 66fc90b

Please sign in to comment.