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 May 3, 2024
1 parent 8267990 commit 2ea38db
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 10 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.11.3",
"i18next-browser-languagedetector": "7.2.1",
"i18next-http-backend": "2.5.1",
"immer": "9.0.16",
"is-hotkey": "0.2.0",
"jotai": "2.6.0",
Expand All @@ -64,6 +67,7 @@
"react-dom": "18.2.0",
"react-error-boundary": "4.0.10",
"react-google-recaptcha": "2.1.0",
"react-i18next": "14.1.1",
"react-modal": "3.16.1",
"react-range": "1.8.14",
"react-router-dom": "6.20.0",
Expand Down
7 changes: 7 additions & 0 deletions public/locales/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Organisms": {
"RoomCommon": {
"changed_room_name": " hat den Raum Name geändert"
}
}
}
7 changes: 7 additions & 0 deletions public/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Organisms": {
"RoomCommon": {
"changed_room_name": " changed room name"
}
}
}
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;
5 changes: 4 additions & 1 deletion src/app/organisms/room/RoomTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
toRem,
} from 'folds';
import { isKeyHotkey } from 'is-hotkey';
import { useTranslation } from 'react-i18next';
import {
decryptFile,
eventWithShortcode,
Expand Down Expand Up @@ -1254,6 +1255,8 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
},
});

const { t } = useTranslation();

const renderMatrixEvent = useMatrixEventRenderer<[number, EventTimelineSet, boolean]>({
renderRoomMessage: (mEventId, mEvent, item, timelineSet, collapse) => {
const reactionRelations = getEventReactions(timelineSet, mEventId);
Expand Down Expand Up @@ -1494,7 +1497,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
<Box grow="Yes" direction="Column">
<Text size="T300" priority="300">
<b>{senderName}</b>
{' changed room name'}
{t('Organisms.RoomCommon.changed_room_name')}
</Text>
</Box>
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
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 @@ -34,6 +34,10 @@ const copyFiles = {
src: 'public/res/android',
dest: 'public/',
},
{
src: 'public/locales',
dest: 'public/',
},
],
}

Expand Down

0 comments on commit 2ea38db

Please sign in to comment.