Skip to content

Commit a8185fd

Browse files
authored
Migrate to gettext 😌 (#87)
1 parent 07a4643 commit a8185fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4395
-956
lines changed

β€Ž.gitignoreβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ scripts/chapters.yml
2929
src/data/speakers.json
3030
src/data/sessions.json
3131
src/data/schedule.json
32+
33+
# Generated translations
34+
src/i18n/translations.json
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Maintaining Translations
2+
3+
Feel free to skip the Updating Translations section, but fuzzy translations are important, so take a look πŸ€—
4+
5+
## Updating Translations
6+
7+
When English text changes in the code, run the update script to synchronize all languages:
8+
9+
```bash
10+
pnpm run i18n
11+
```
12+
13+
This script:
14+
1. Extracts all current `t()` calls from the codebase
15+
2. Updates the template file (`locales/template/messages.pot`)
16+
3. Merges changes into all language PO files
17+
4. Shows statistics about translated, fuzzy, and untranslated strings
18+
19+
**What happens during merge:**
20+
- **New strings**: Added with empty `msgstr ""` (need translation)
21+
- **Modified strings**: Marked as "fuzzy" (need review)
22+
- **Unchanged strings**: Kept as-is
23+
- **Deleted strings**: Moved to obsolete section at end of file
24+
25+
## Understanding Fuzzy Translations
26+
27+
When the English source text changes slightly, gettext marks translations as "fuzzy":
28+
29+
```po
30+
#, fuzzy
31+
msgid "Welcome to our conference"
32+
msgstr "Konferansımıza Hoş Geldiniz"
33+
```
34+
35+
The `#, fuzzy` flag means:
36+
- The English text changed slightly since this was translated
37+
- The existing translation might still be correct, but needs review
38+
- Review the translation and remove the `#, fuzzy` line once verified
39+
40+
## Checking Translation Status
41+
42+
The update script shows you:
43+
- **Translated**: Strings with translations
44+
- **Fuzzy**: Translations that need review (source text changed)
45+
- **Untranslated**: Strings without translations (will fall back to English)
46+
47+
Example output:
48+
49+
```
50+
Merging es...
51+
147 translated messages, 8 fuzzy translations, 55 untranslated messages.
52+
```

β€Ždocs/translating.mdβ€Ž

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Translation Guide
2+
3+
This project uses GNU gettext for internationalization. This guide explains how to add new translations and work with the translation system.
4+
5+
## Overview
6+
7+
The translation system follows these principles:
8+
9+
- **English is the source language**: All text in the code is in English, using `t("English text")` calls
10+
- **Single source of truth**: Language configuration is centralized in `src/i18n/locales.ts`
11+
- **Standard format**: Uses PO files for translations
12+
- **Build-time conversion**: PO files are converted to JSON during the build process
13+
14+
## How It Works
15+
16+
### 1. Code Usage
17+
18+
In the `.astro` or `.ts` files, use the `t()` function to mark translatable strings:
19+
20+
```typescript
21+
const t = useTranslations(lang);
22+
23+
const title = t("Welcome to PyLadiesCon");
24+
const description = t("Join us for an amazing conference");
25+
```
26+
27+
For English (the default language), `t()` returns the exact string you pass in. For other languages, it looks up the translation in the PO file.
28+
29+
### 2. Translation Workflow
30+
31+
The system automatically:
32+
1. Extracts all `t("...")` calls from the codebase
33+
2. Generates a template file (`messages.pot`) with all strings
34+
3. Merges new/changed strings into existing language files (`messages.po`)
35+
4. Converts PO files to JSON at build time for runtime usage
36+
37+
### 3. PO File Format
38+
39+
PO files are plain text files with this structure:
40+
41+
```po
42+
#: src/components/hero.astro:15
43+
msgid "Welcome to PyLadiesCon"
44+
msgstr "PyLadiesCon'a Hoş Geldiniz"
45+
46+
#: src/pages/[lang]/speakers.astro:42
47+
msgid "Our Speakers"
48+
msgstr "Konuşmacılarımız"
49+
```
50+
51+
- `msgid`: The English source text (what you wrote in the code)
52+
- `msgstr`: The translated text (what users will see)
53+
- `#:` comments show where the string is used in the codebase
54+
55+
## Adding a New Language
56+
57+
### Step 1: Add to Configuration
58+
59+
Edit `src/i18n/locales.ts` and add your language:
60+
61+
```typescript
62+
export const languages = {
63+
en: "English",
64+
es: "EspaΓ±ol",
65+
tr: "TΓΌrkΓ§e",
66+
fr: "FranΓ§ais", // ⬅️ Add your language here
67+
};
68+
```
69+
70+
### Step 2: Create the Translation File
71+
72+
Run the bootstrap script with your language code:
73+
74+
```bash
75+
pnpm run i18n:new fr
76+
```
77+
78+
This creates `locales/fr/messages.po` with all current strings ready to translate.
79+
80+
### Step 3: Translate the Strings
81+
82+
You have two options for translating:
83+
84+
#### Option A: Using Poedit (Recommended)
85+
86+
[Poedit](https://poedit.net/) is a free and open-source editor for PO files:
87+
88+
1. Download and install [Poedit](https://poedit.net/)
89+
2. Open `locales/fr/messages.po` in Poedit
90+
3. Translate each string in the visual interface
91+
4. Save the file (Poedit handles the format automatically)
92+
93+
#### Option B: Manual Editing
94+
95+
Open `locales/fr/messages.po` in any text editor and fill in the `msgstr` fields:
96+
97+
```po
98+
msgid "Welcome"
99+
msgstr "Bienvenue" # ← Add your translation here
100+
```
101+
102+
### Step 4: Build and Test
103+
104+
Run the development server to see your translations:
105+
106+
```bash
107+
pnpm run dev
108+
```
109+
110+
Visit `http://localhost:4321/fr` or switch the language to see your translations in action.
111+
112+
### Step 5: Submit Your Translations
113+
114+
Once you have completed your translations, submit a pull request to the [main repository](https://github.com/pyladies/global-conference-2025). Aaaand that's it! Your translations will be reviewed and merged for everyone to enjoy. πŸŽ‰πŸŽ‰πŸŽ‰
115+
116+
## Translation Tips
117+
118+
### Do's
119+
120+
βœ… Keep the same formatting (HTML tags, placeholders) as the source text
121+
βœ… Make sure to escape special characters (e.g., quotes, newlines, etc.)
122+
βœ… Try to use gender-neutral language where possible
123+
βœ… Test your translations in the browser to verify context
124+
125+
### Don'ts
126+
127+
❌ Don't change HTML tags or remove them (keep `<strong>`, `<a>`, etc.)
128+
❌ Don't manually edit `messages.pot` (it's auto-generated)
129+
130+
*See [Maintaining Translations](maintaining-translations.md) for keeping translations working.*

0 commit comments

Comments
Β (0)