jcode/skills/svg-translate/SKILL.md

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

  1. 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: if ModuleNotFoundError, run pip install lxml.
  2. Extract: python svg_text.py extract input.svg strings.json
    • WARNING: no text found means the text was converted to paths or is a bitmap. It cannot be translated automatically: tell the user in the summary, do not guess.
  3. Translate: read_file strings.json. Each entry has id, text (the fragment), context (the whole sentence or label it belongs to) and where. Write the file again with a "translation" field added to EVERY entry (valid UTF-8 JSON, keep id unchanged). 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 context in 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.
  4. Apply: python svg_text.py apply input.svg strings.json output.svg --lang de
    • --lang <code> sets xml:lang on the root (de, fr, es, ja, ...).
    • Add --drop-textlength if the SVG uses textLength/lengthAdjust (fixed lengths squeeze or stretch translated text).
    • Name the output by language, e.g. diagram.de.svg. Never overwrite the original unless asked.
  5. Check visually, then fix and re-run step 4 from the ORIGINAL after editing strings.json.
  6. 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 cairosvg if missing)
  • or rsvg-convert -w 1000 file.svg -o file.png, or inkscape 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:

  1. A shorter translation in strings.json.
  2. Split a long label into two lines with a second <tspan x=".." dy=".."> (small edit_file on the output copy).
  3. Slightly smaller font-size on that element, or change text-anchor.
  4. 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, and aria-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 CSS content:, say so in the summary instead of editing it silently.
  • Several files: run steps 2-4 per file, one strings.json and one language per output file.