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.3.tar.gz (178.3 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.3-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: copysvgtranslate-0.1.3.tar.gz
  • Upload date:
  • Size: 178.3 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.3.tar.gz
Algorithm Hash digest
SHA256 4e1f7f4c6218e4d7c7a528d6ae781c75232e89126fe0687d529c871691883f2f
MD5 8e0d5b0981d440af3e21a6a1b9fa1122
BLAKE2b-256 b56800a31bfbd37a19c16dd0004d0e97d5a6b3c202b02a32666af79121c3a88b

See more details on using hashes here.

Provenance

The following attestation bundles were made for copysvgtranslate-0.1.3.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.3-py3-none-any.whl.

File metadata

File hashes

Hashes for copysvgtranslate-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d98ea3c86ee53f54203b7b6c50d971d0d720c1d17b300bb36362e3d5564b8e55
MD5 1bdb4dc6ad35ce6a8c10a98e7f1e2624
BLAKE2b-256 3002b1d12c2f9edfa4851b834fe2daa7b4a36bcc09b8620f0e20549c3007bcae

See more details on using hashes here.

Provenance

The following attestation bundles were made for copysvgtranslate-0.1.3-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