Skip to main content

Utilities for extracting and applying translations to multilingual SVG files.

Project description

SVG Translation Tool

This tool extracts multilingual text pairs from SVG files and applies translations to other SVG files by inserting missing <text systemLanguage="XX"> blocks.

Installation

This tool requires Python 3.10+. Install the lightweight core dependencies with:

pip install CopySvgTranslate

Usage

Extracting and injecting in a single step

from pathlib import Path
from CopySvgTranslate import svg_extract_and_inject

tree = svg_extract_and_inject(
    extract_file=Path("examples/source_multilingual.svg"),
    inject_file=Path("examples/target_missing_translations.svg"),
    data_output_file = Path("examples/data.json"),
    save_result=True,
)

if tree is not None:
    print("Injection completed!")

The helper stores the extracted phrases under Path("examples/data.json") and, when save_result=True, writes the translated SVG to output_dir=Path("./translated"). If you also need statistics about how many translations were inserted, call the lower level injector with return_stats=True:

from CopySvgTranslate.injection import inject

tree, stats = inject(
    inject_file="examples/target_missing_translations.svg",
    mapping_files=["CopySvgTranslate/data/source_multilingual.svg.json"],
    output_dir=Path("./translated"),
    save_result=True,
    return_stats=True,
)

print(stats)

Injecting with pre-translated data

When you already have the translation JSON, load it and use inject directly. Important parameters include overwrite to update existing translations and output_dir to control where translated files are written.

from pathlib import Path
from CopySvgTranslate import inject

translations = {
    "new": {
        "Hello": {"ar": "مرحبًا", "fr": "Bonjour"},
    }
}

tree, stats = inject(
    inject_file=Path("examples/target_missing_translations.svg"),
    all_mappings=translations,
    output_dir=Path("./translated"),
    overwrite=True,
    save_result=True,
    return_stats=True,
)

print("Saved to", Path("./translated/target_missing_translations.svg"))
print(stats)

Data Model

The extractor writes a JSON document rooted under the "new" key. Each entry maps normalized English text to a dictionary of language codes and translations. An example of the modern format:

{
  "new": {
    "but are connected in anti-phase": {
      "ar": "لكنها موصولة بمرحلتين متعاكستين."
    }
  }
}

Older exports may omit the wrapper and look like {"english": {"ar": "…"}}. The injector transparently accepts both structures, but the recommended format is the nested "new" layout shown above.

Extract Example

Input SVG (arabic.svg)

<switch style="font-size:30px;font-family:Bitstream Vera Sans">
    <text x="259.34814" y="927.29651" style="font-size:30px;font-family:Bitstream Vera Sans"
        id="text2213-ar"
        xml:space="preserve" systemLanguage="ar">
        <tspan x="259.34814" y="927.29651" id="tspan2215-ar">لكنها موصولة بمرحلتين متعاكستين.</tspan>
    </text>
    <text x="259.34814" y="927.29651" style="font-size:30px;font-family:Bitstream Vera Sans"
        id="text2213"
        xml:space="preserve">
        <tspan x="259.34814" y="927.29651" id="tspan2215">but are connected in anti-phase</tspan>
    </text>
</switch>

Extracted JSON (arabic.svg.json)

{
  "new": {
    "but are connected in anti-phase": {
      "ar": "لكنها موصولة بمرحلتين متعاكستين."
    }
  }
}

Injection Example

  • TODO

Testing

Run the unit tests:

python -m pytest tests -v

Implementation Details

Text Normalization

The tool normalizes text by:

  • Trimming leading and trailing whitespace
  • Replacing multiple internal whitespace characters with a single space
  • Optionally converting to lowercase for case-insensitive matching

ID Generation

When adding new translation nodes, the tool generates unique IDs by:

  • Taking the existing ID and appending the language code (e.g., text2213 becomes text2213-ar)
  • If the generated ID already exists, appending a numeric suffix until unique (e.g., text2213-ar-1)

Error Handling

The tool includes comprehensive error handling for:

  • Missing input files
  • Invalid XML structure
  • Missing required attributes
  • File permission issues

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

copysvgtranslate-0.1.4.tar.gz (183.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

copysvgtranslate-0.1.4-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file copysvgtranslate-0.1.4.tar.gz.

File metadata

  • Download URL: copysvgtranslate-0.1.4.tar.gz
  • Upload date:
  • Size: 183.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for copysvgtranslate-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6cec57271b5fb5845a1985f018fb93c919c2f229f66dab10e433dce45732f120
MD5 64a3078748361ad1bca73f87bec48e65
BLAKE2b-256 13ad113b76da138becb0b99f08587899e452e406d5cc16a38b9feb656bdca7fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for copysvgtranslate-0.1.4.tar.gz:

Publisher: python-publish.yml on MrIbrahem/CopySvgTranslate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file copysvgtranslate-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for copysvgtranslate-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8375635a3b1e039c32f15f92b9609c78b847dd440feb5e569c5bf7123ee1477b
MD5 2cd03ad955223e6ddb36d86ff9e17861
BLAKE2b-256 18895ccd52da02cc53e71f6058da53ae8658494b31cb7d96c7f10d43339febe5

See more details on using hashes here.

Provenance

The following attestation bundles were made for copysvgtranslate-0.1.4-py3-none-any.whl:

Publisher: python-publish.yml on MrIbrahem/CopySvgTranslate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page