aboutsummaryrefslogtreecommitdiffstats
path: root/doc/i18n-technical.md
blob: 4ea2adbea334f819a2716cdf30073067d547d492 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coinos Internationalization - Technical Information

_NOTE: This documentation was written for people with a technical background. If you do not have a technical background, consider reading [this guide](./i18n.md) instead._

Last Updated: 2022-08-23

## Dependencies

coinos's internationalization system depends on the `npm` package [`sveltekit-i18n`](https://www.npmjs.com/package/sveltekit-i18n). Consider reading [its documentation](https://github.com/sveltekit-i18n/lib/tree/master/docs/README.md) in addition to this.

## Internationalization Code

The main code for coinos's internationalization is at `src/lib/translations/index.js`. When imported, it does the following:

1. Load all of the locales asynchronously, based on .
2. Initialize `sveltekit-i18n`.

It also defines important constants, including the list of available locales and the default locale. This code is loaded by importing `$lib/translations` in `src/routes/+layout.svelte`. This code, which runs in the frontend, also persists the locale in `localStorage`.

## Locale Files

Each locale in `coinos-ui-v2` is defined by a JSON file in `src/locales`. Its name is the locale's [ISO 639-1 code](https://en.wikipedia.org/wiki/ISO_639-1) plus `.json`. It is simply a file that maps string IDs to translated strings. Here is an example of `en.json`:

```json
{
	"howItWorks": "How It Works",
	"faq": "FAQ",
	"about": "About",
	"startInSeconds": "Start in Seconds",
	"signIn": "Sign In"
}
```

## Internationalizing Strings

Strings are internationalized by simply using `sveltekit-i18n`'s `$t` function. A localized string looks like this:

```
{$t('stringID')}
```

## The Locale Selector

coinos also has a `LocaleSelector` component, which should be used to select locales. It is a dropdown that allows you to select & update the locale.

## The Guided Translation Script

coinos has a Python script that helps you with the technical parts of translation. It is located at `src/locales/guided_translation.py`. It also has some utility functions:

- `nested_dict_keys` - gets a list of keys of a nested dictionary in dot-separated shallow format (e.g. a.b.c)
- `create_locale` - adds a new locale to the lang.json file
- `localize_string` - localizes a string interactively