diff --git a/docs/dev/how-to-translate.md b/docs/dev/how-to-translate.md
index 77cbe3f4..b72b045a 100644
--- a/docs/dev/how-to-translate.md
+++ b/docs/dev/how-to-translate.md
@@ -1,107 +1,76 @@
# How to translate WorkAdventure
-## How the translation files work
-
-In the `front/translations` folder, all json files of the form `[namespace].[language code].json` are interpreted to create languages by the language code in the file name.
-
-The only mandatory file (the entry point) is the `index.[language code].json` which must contain the properties `language`, `country` and `default` so that the language can be taken into account.
-
-Example:
-```json
-{
- "language": "English", # Language that will be used
- "country": "United States", # Country specification (e.g. British English and American English are not the same thing)
- "default": true # In some cases WorkAdventure only knows the language, not the country language of the browser. In this case it takes the language with the default property at true.
-}
-```
-
-It does not matter where the file is placed in this folder. However, we have set up the following architecture in order to have a simple reading:
-
-- translations/
- - [language code]/
- - [namepace].[language code].json
-
-Example:
-- translations/
- - en-US/
- - index.en-US.json
- - main-menu.en-US.json
- - chat.en-US.json
- - fr-FR/
- - index.fr-FR.json
- - main-menu.fr-FR.json
- - chat.fr-FR.json
-
-If a key isn't found then it will be searched in the fallback language and if it isn't found then the key will be returned. By default the fallback language is `en-US` but you can set another one with the `FALLBACK_LANGUAGE` environment variable.
+We use the [typesafe-i18n](https://github.com/ivanhofer/typesafe-i18n) package to handle the translation.
## Add a new language
It is very easy to add a new language!
-First, in the translations folder create a new folder with the language code as name (the language code according to [RFC 5646](https://datatracker.ietf.org/doc/html/rfc5646)).
+First, in the `front/src/i18n` folder create a new folder with the language code as name (the language code according to [RFC 5646](https://datatracker.ietf.org/doc/html/rfc5646)).
-In the previously created folder, add a file named as index.[language code].json with the following content:
+In the previously created folder, add a file named index.ts with the following content containing your language information (french from France in this example):
-```json
-{
- "language": "Language Name",
- "country": "Country Name",
- "default": true
-}
+```ts
+import type { Translation } from "../i18n-types";
+
+const fr_FR: Translation = {
+ ...en_US,
+ language: "Français",
+ country: "France",
+};
+
+export default fr_FR;
```
-See the section above (*How the translation files work*) for more information on how this works.
-
-BE CAREFUL if your language has variants by country don't forget to give attention to the default language used. If several languages are default the first one in the list will be used.
-
## Add a new key
### Add a simple key
+
The keys are searched by a path through the properties of the sub-objects and it is therefore advisable to write your translation as a JavaScript object.
-Please use [kebab-case](https://en.wikipedia.org/wiki/Letter_case#Kebab_case) to name your keys!
+Please use kamelcase to name your keys!
Example:
-```json
+```ts
{
- "messages": {
- "coffe-machine":{
- "start": "Coffe machine has been started!"
- }
+ messages: {
+ coffeMachine: {
+ start: "Coffe machine has been started!";
}
+ }
}
```
-In the code you can translate using `translator.translate`, or the shortcut alias: `_`.
+In the code you can translate using `$LL`:
-```js
-import { _ } from "../../Translator/Translator";
+```ts
+import LL from "../../i18n/i18n-svelte";
-console.log(_('messages.coffe-machine.start'));
+console.log($LL.messages.coffeMachine.start());
```
### Add a key with parameters
You can also use parameters to make the translation dynamic.
-Use the tag {{ [parameter name] }} to apply your parameters in the translations
+Use the tag { [parameter name] } to apply your parameters in the translations
Example:
-```json
+```ts
{
- "messages": {
- "coffe-machine":{
- "player-start": "{{ playerName }} started the coffee machine!"
- }
+ messages: {
+ coffeMachine: {
+ playerStart: "{ playerName } started the coffee machine!";
}
+ }
}
```
In the code you can use it like this:
-```js
-_('messages.coffe-machine.player-start', {
- playerName: "John"
+```ts
+$LL.messages.coffeMachine.playerStart.start({
+ playerName: "John",
});
```
diff --git a/front/.typesafe-i18n.json b/front/.typesafe-i18n.json
new file mode 100644
index 00000000..0cecbe32
--- /dev/null
+++ b/front/.typesafe-i18n.json
@@ -0,0 +1,5 @@
+{
+ "$schema": "https://unpkg.com/typesafe-i18n@2.59.0/schema/typesafe-i18n.json",
+ "baseLocale": "en-US",
+ "adapter": "svelte"
+}
\ No newline at end of file
diff --git a/front/package.json b/front/package.json
index 81da765c..100ae59c 100644
--- a/front/package.json
+++ b/front/package.json
@@ -21,7 +21,6 @@
"html-webpack-plugin": "^5.3.1",
"jasmine": "^3.5.0",
"lint-staged": "^11.0.0",
- "merge-jsons-webpack-plugin": "^2.0.1",
"mini-css-extract-plugin": "^1.6.0",
"node-polyfill-webpack-plugin": "^1.1.2",
"npm-run-all": "^4.1.5",
@@ -65,11 +64,12 @@
"socket.io-client": "^2.3.0",
"standardized-audio-context": "^25.2.4",
"ts-proto": "^1.96.0",
+ "typesafe-i18n": "^2.59.0",
"uuidv4": "^6.2.10",
"zod": "^3.11.6"
},
"scripts": {
- "start": "run-p templater serve svelte-check-watch",
+ "start": "run-p templater serve svelte-check-watch typesafe-i18n",
"templater": "cross-env ./templater.sh",
"serve": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack.json\" webpack serve --open",
"build": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack.json\" NODE_ENV=production webpack",
@@ -81,7 +81,8 @@
"svelte-check-watch": "svelte-check --fail-on-warnings --fail-on-hints --compiler-warnings \"a11y-no-onchange:ignore,a11y-autofocus:ignore,a11y-media-has-caption:ignore\" --watch",
"svelte-check": "svelte-check --fail-on-warnings --fail-on-hints --compiler-warnings \"a11y-no-onchange:ignore,a11y-autofocus:ignore,a11y-media-has-caption:ignore\"",
"pretty": "yarn prettier --write 'src/**/*.{ts,svelte}'",
- "pretty-check": "yarn prettier --check 'src/**/*.{ts,svelte}'"
+ "pretty-check": "yarn prettier --check 'src/**/*.{ts,svelte}'",
+ "typesafe-i18n": "typesafe-i18n"
},
"lint-staged": {
"*.svelte": [
diff --git a/front/src/Components/AudioManager/AudioManager.svelte b/front/src/Components/AudioManager/AudioManager.svelte
index aab2747e..3385d6da 100644
--- a/front/src/Components/AudioManager/AudioManager.svelte
+++ b/front/src/Components/AudioManager/AudioManager.svelte
@@ -5,7 +5,7 @@
import { get } from "svelte/store";
import type { Unsubscriber } from "svelte/store";
import { onDestroy, onMount } from "svelte";
- import { _ } from "../../Translator/Translator";
+ import LL from "../../i18n/i18n-svelte";
let HTMLAudioPlayer: HTMLAudioElement;
let audioPlayerVolumeIcon: HTMLElement;
@@ -145,7 +145,7 @@
diff --git a/front/src/Components/Chat/Chat.svelte b/front/src/Components/Chat/Chat.svelte
index 5f66837b..c4756a36 100644
--- a/front/src/Components/Chat/Chat.svelte
+++ b/front/src/Components/Chat/Chat.svelte
@@ -5,7 +5,7 @@
import ChatElement from "./ChatElement.svelte";
import { afterUpdate, beforeUpdate, onMount } from "svelte";
import { HtmlUtils } from "../../WebRtc/HtmlUtils";
- import { _ } from "../../Translator/Translator";
+ import LL from "../../i18n/i18n-svelte";
let listDom: HTMLElement;
let chatWindowElement: HTMLElement;
@@ -46,7 +46,7 @@