There's no need to rely on Google Translator's Kit to batch translate HTML or PHP-coded files. A good approach is to extract your translatable strings into PO files, translate them with a proper translation memory tool, then rebuild your localized HTML or templates. This prevents your PHP tags or attributes from being altered.
**Option 1: Translate Toolkit (html2po / po2html)**
1. Extract strings to a POT template:
```
html2po -P src/ locale/templates.pot
```
2. Create a per-language PO file and translate:
```
msginit -i locale/templates.pot -l fr -o locale/fr.po
# translate locale/fr.po with your editor (Poedit, Virtaal, etc.)
```
3. Build the localized HTML:
```
po2html -t src/ locale/fr.po build/fr/
```
Repeat for each language. Your original HTML remains untouched; only new or changed strings need translating when you update your source.
**Option 2: po4a (PO for anything)**
This is useful when you have many files. Configure a `po4a.cfg`:
```
[po4a_langs] fr de es
[po4a_paths] locale/$lang.po
[type: xhtml] src/*.html $lang:build/$lang/%.html
```
Run `po4a-updatepo` to extract or update strings, then `po4a-translate` to produce localized HTML once translations are done.
**Best practices**
- Use UTF-8 encoding.
- Do not translate IDs, anchors or data- attributes. Do translate `alt` and `title` attributes if you need them localized.
- Keep your sentences well-formed so they extract cleanly.
- Test the output with a link checker.
If you prefer a hosted workflow instead of running tools locally, there are many translation management services that accept HTML or PO and preserve your tags. For example, Pairaphrase can import PO or HTML files, apply translation memory and glossaries, and then export localized HTML without stripping your PHP tags. Other platforms like Smartling, memoQ and Weblate offer similar features. The key is to use a tool designed for software translation rather than a general web translator.