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/usage.md
+80Lines changed: 80 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -426,6 +426,86 @@ gpt-po-translator --folder ./locales --lang de --no-ai-comment
426
426
427
427
---
428
428
429
+
## Whitespace Handling in Translations
430
+
431
+
### Overview
432
+
433
+
The tool automatically preserves leading and trailing whitespace from `msgid` entries in translations. While **best practice** is to handle whitespace in your UI framework rather than in translation strings, the tool ensures that any existing whitespace patterns are maintained exactly.
434
+
435
+
### How Whitespace Preservation Works
436
+
437
+
The tool uses a three-step process to reliably preserve whitespace:
438
+
439
+
1.**Detection and Warning**
440
+
When processing PO files, the tool scans for entries with leading or trailing whitespace and logs a warning:
441
+
```
442
+
WARNING: Found 3 entries with leading/trailing whitespace in messages.po
443
+
Whitespace will be preserved in translations, but ideally should be handled in your UI framework.
444
+
```
445
+
446
+
2.**Before Sending to AI** (Bulk Mode)
447
+
To prevent the AI from being confused by or accidentally modifying whitespace, the tool:
448
+
- Strips all leading/trailing whitespace from texts
449
+
- Stores the original whitespace pattern
450
+
- Sends only the clean text content to the AI
451
+
452
+
For example, if `msgid` is `" Incorrect"` (with leading space), the AI receives only `"Incorrect"`.
453
+
454
+
3.**After Receiving Translation**
455
+
Once the AI returns the translation, the tool:
456
+
- Extracts the original whitespace pattern from the source `msgid`
457
+
- Applies that exact pattern to the translated `msgstr`
458
+
- Ensures the output matches the input whitespace structure
459
+
460
+
So `" Incorrect"` → AI translates `"Incorrect"` → Result: `" Incorreto"` (leading space preserved)
461
+
462
+
### Examples
463
+
464
+
| Original msgid | AI Receives | AI Returns | Final msgstr |
-**Single Mode**: Each text is stripped before sending to AI, then whitespace is restored after receiving the translation
482
+
-**Bulk Mode**: Entire batches are stripped before sending to AI (JSON array of clean texts), then whitespace is restored to each translation individually
483
+
484
+
Both modes use the same preservation logic, ensuring consistent behavior.
485
+
486
+
### Best Practices
487
+
488
+
1.**Avoid whitespace in msgid when possible**
489
+
Whitespace in translation strings can cause formatting issues. Instead, handle spacing in your UI layer:
490
+
```python
491
+
# Bad - whitespace in msgid
492
+
msgid " Settings"
493
+
494
+
# Good - whitespace in code
495
+
print(f"{_('Settings')}")
496
+
```
497
+
498
+
2.**If whitespace is unavoidable**
499
+
The tool will preserve it automatically. Use verbose mode to see which entries contain whitespace:
0 commit comments