workadventure/docs/dev/how-to-translate.md

77 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2021-12-08 20:12:18 +01:00
# How to translate WorkAdventure
2022-01-21 17:06:03 +01:00
We use the [typesafe-i18n](https://github.com/ivanhofer/typesafe-i18n) package to handle the translation.
2021-12-18 22:32:15 +01:00
2021-12-08 20:12:18 +01:00
## Add a new language
It is very easy to add a new language!
2022-01-21 17:06:03 +01:00
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)).
2021-12-08 20:12:18 +01:00
2022-01-21 17:06:03 +01:00
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):
2021-12-08 20:12:18 +01:00
2022-01-21 17:06:03 +01:00
```ts
import type { Translation } from "../i18n-types";
2021-12-08 20:12:18 +01:00
2022-01-21 17:06:03 +01:00
const fr_FR: Translation = {
...en_US,
language: "Français",
country: "France",
};
2021-12-08 20:12:18 +01:00
2022-01-21 17:06:03 +01:00
export default fr_FR;
```
2021-12-08 20:12:18 +01:00
## Add a new key
### Add a simple key
2022-01-21 17:06:03 +01:00
2021-12-08 20:12:18 +01:00
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.
2022-01-21 17:06:03 +01:00
Please use kamelcase to name your keys!
2021-12-08 20:12:18 +01:00
Example:
2022-01-21 17:06:03 +01:00
```ts
2021-12-08 20:12:18 +01:00
{
2022-01-21 17:06:03 +01:00
messages: {
coffeMachine: {
start: "Coffe machine has been started!";
2021-12-08 20:12:18 +01:00
}
2022-01-21 17:06:03 +01:00
}
2021-12-08 20:12:18 +01:00
}
```
2022-01-21 17:06:03 +01:00
In the code you can translate using `$LL`:
2021-12-08 20:12:18 +01:00
2022-01-21 17:06:03 +01:00
```ts
import LL from "../../i18n/i18n-svelte";
2022-01-21 17:06:03 +01:00
console.log($LL.messages.coffeMachine.start());
2021-12-08 20:12:18 +01:00
```
### Add a key with parameters
You can also use parameters to make the translation dynamic.
2022-01-21 17:06:03 +01:00
Use the tag { [parameter name] } to apply your parameters in the translations
2021-12-08 20:12:18 +01:00
Example:
2022-01-21 17:06:03 +01:00
```ts
2021-12-08 20:12:18 +01:00
{
2022-01-21 17:06:03 +01:00
messages: {
coffeMachine: {
playerStart: "{ playerName } started the coffee machine!";
2021-12-08 20:12:18 +01:00
}
2022-01-21 17:06:03 +01:00
}
2021-12-08 20:12:18 +01:00
}
```
In the code you can use it like this:
2022-01-21 17:06:03 +01:00
```ts
$LL.messages.coffeMachine.playerStart.start({
playerName: "John",
2021-12-08 20:12:18 +01:00
});
```