You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/localization.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,3 +32,74 @@ Both of these routes use the [TutorialsLayout](/src/layouts/TutorialsLayout.astr
32
32
The main difference between these two routing files is how they retrieve the correct translation. The English version retreives the English version of the content. The other translations retrieve their version of the content and then fill in any gaps with English versions. See `getCollectionInLocaleWithFallbacks()` for how this works.
33
33
34
34
Because of this subtle duplication, we try to keep the files in `src/pages/` as short as possible and move rendering logic into the [layout files](/src/layouts/).
35
+
36
+
## Using translations
37
+
38
+
When you are editing an existing page or adding a new one, there are two ways to use individual translated strings from the ["ui" content collection](/src/content/ui/). It depends on what kind of file you are editing:
39
+
40
+
### Astro files (`.astro`)
41
+
42
+
In .astro files you can use `getCurrentLocale()` and `getUiTranslator()` like so:
43
+
44
+
```astro
45
+
---
46
+
import { getCurrentLocale, getUiTranslator } from "@i18n/utils";
`getCurrentLocale()` extracts the current locale code from the page's URL. Passing this to `getUiTranslator()` tells it which language to return translations for. Whenever a translation is missing for the given language, it will return the string in English (the fallback language).
58
+
59
+
The returned translator function (`t`) accepts a key or list of keys to know which translation string to return. Check [the English translation file to see the most complete list of what's already available](/src/content/ui/en.yaml). The keys you pass to the translator function are used to lookup a string in that file (or the equivalent of it in the current language). If the `en.yaml` file has the following content:
60
+
61
+
```yaml
62
+
Forum: Forum
63
+
Sketches: Sketches
64
+
Libraries: Libraries
65
+
People: People
66
+
New intro paragraph: Welcome to this new page we just added!
67
+
sectionTitles:
68
+
main: An intro to squares
69
+
```
70
+
71
+
Then `t("New intro paragraph")` will return "Welcome to this new page we just added!" and `t("sectionTitles", main)` will return "An intro to squares".
72
+
73
+
### Preact files (`.jsx`, `.tsx`)
74
+
75
+
Preact files cannot access the current url directly with `Astro.url.pathname`, so we have to take a different approach: we pass in a translation object as a prop. Every Preact component in this project is rendered from an Astro layout or component file, so we can rely on it to get the correct current language.
76
+
77
+
In our astro file, we use our old friend `getCurrentLocale()` and then pass the result to `getUiTranslationWithFallback()` to get an object of translations. And pass it to the `HelloButton` component.
78
+
79
+
```astro
80
+
---
81
+
import { getCurrentLocale, getUiTranslationWithFallback } from "@i18n/utils";
82
+
import { HelloButton } from "@components/HellowButton/index.jsx"
Essentially, the arguments you would pass to `t` in the first example for astro files are the same you would pass as keys to the `uiTranslations` object. If its a nested key, that looks like:
0 commit comments