4.0 KiB
4.0 KiB
| name | description |
|---|---|
| svg-translate | Translate the text inside SVG files (text, tspan, title, desc, aria-label) into another language while keeping markup, styling and layout intact. Use for any request to translate, localize or change the language of an .svg file. |
Translate text in SVG files
Never rewrite or retype an SVG: that breaks paths, gradients, IDs and styling. Extract only the strings into JSON, translate the JSON, and write the translations back into a copy of the original with the helper script svg_text.py (a file of this skill).
Workflow
- Copy the helper into the project (skill files are read-only, never retype it):
run_command: cp <skill-dir>/svg_text.py .(<skill-dir>is shown by load_skill). Needs lxml: ifModuleNotFoundError, runpip install lxml. - Extract:
python svg_text.py extract input.svg strings.jsonWARNING: no text foundmeans the text was converted to paths or is a bitmap. It cannot be translated automatically: tell the user in the summary, do not guess.
- Translate:
read_file strings.json. Each entry hasid,text(the fragment),context(the whole sentence or label it belongs to) andwhere. Write the file again with a"translation"field added to EVERY entry (valid UTF-8 JSON, keepidunchanged). Use write_file for the complete file.- A sentence is often split over several fragments (tspans, or text before/after a tspan). Translate with the
contextin mind so that the fragments together read as one correct sentence; word order may differ, an empty string is allowed for a fragment without counterpart. - Keep numbers, units, brand names, code, URLs and formulas unchanged.
- Keep labels short; prefer the shorter wording when two translations are equally good, the text must still fit the drawing.
- Surrounding spaces are restored by the script.
- A sentence is often split over several fragments (tspans, or text before/after a tspan). Translate with the
- Apply:
python svg_text.py apply input.svg strings.json output.svg --lang de--lang <code>setsxml:langon the root (de, fr, es, ja, ...).- Add
--drop-textlengthif the SVG usestextLength/lengthAdjust(fixed lengths squeeze or stretch translated text). - Name the output by language, e.g.
diagram.de.svg. Never overwrite the original unless asked.
- Check visually, then fix and re-run step 4 from the ORIGINAL after editing
strings.json. - Summary: output file name, number of strings translated, layout problems that remain.
Visual check
Render original and translation to PNG, then look at both with view_image:
python -c "import cairosvg; cairosvg.svg2png(url='file.svg', write_to='file.png', output_width=1000)"(pip install cairosvgif missing)- or
rsvg-convert -w 1000 file.svg -o file.png, orinkscape file.svg -o file.png -w 1000
Look for text outside its box or off the canvas, overlapping labels, truncated text, missing glyphs (boxes/blanks, typical for CJK, Arabic, Hebrew when the font lacks them) and wrong line breaks. Fixes, in order of preference:
- A shorter translation in
strings.json. - Split a long label into two lines with a second
<tspan x=".." dy="..">(smalledit_fileon the output copy). - Slightly smaller
font-sizeon that element, or changetext-anchor. - For missing glyphs, add a fallback font to
font-family(e.g.Noto Sans CJK JP,Noto Sans Arabic).
Right-to-left languages: the script keeps the logical text; consider direction="rtl" and text-anchor="end" on the affected <text> elements and re-check.
What is and is not translated
- Translated: text of
<text>,<tspan>,<textPath>,<title>,<desc>,<flowPara>/<flowSpan>, text in links, andaria-label. - Not translated: strings with only digits/punctuation/symbols;
id,class,href; CSS in<style>; JavaScript strings;inkscape:label,data-*; text inside embedded raster images or path outlines. If visible text lives in<script>or CSScontent:, say so in the summary instead of editing it silently. - Several files: run steps 2-4 per file, one
strings.jsonand one language per output file.