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"),
    save_result=True,
)

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

The helper stores the extracted phrases under CopySvgTranslate/data/ and, when save_result=True, writes the translated SVG to CopySvgTranslate/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"],
    save_result=True,
    return_stats=True,
)

print(stats)

Injecting with pre-translated data

When you already have the translation JSON, load it and use svg_extract_and_injects 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 svg_extract_and_injects

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

tree, stats = svg_extract_and_injects(
    translations=translations,
    inject_file=Path("examples/target_missing_translations.svg"),
    save_result=True,
    overwrite=True,
    output_dir=Path("./translated"),
    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. Metadata such as "default_tspans_by_id" is used internally to reconstruct the SVG structure during injection. An example of the modern format:

{
  "new": {
    "default_tspans_by_id": {
      "text2213": "but are connected in anti-phase"
    },
    "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.

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": {
    "default_tspans_by_id": {
      "text2213": "but are connected in anti-phase"
    },
    "but are connected in anti-phase": {
      "ar": "لكنها موصولة بمرحلتين متعاكستين."
    }
  }
}

Output SVG after Injection

<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>

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.0.tar.gz (34.6 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.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: copysvgtranslate-0.1.0.tar.gz
  • Upload date:
  • Size: 34.6 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.0.tar.gz
Algorithm Hash digest
SHA256 c668bb68fec17e852314368123e478dd9493efc85ee513f61981cc09ec0c6ea9
MD5 a7ddc55170ffe31f89868489627efdd4
BLAKE2b-256 c1255478d3f22f7a74e351e2ca23eba30af807af97f1eea8088e51f2425b5677

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for copysvgtranslate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64b06beaeff56f8e120700a959e8b2150f13aef37b721f690256784f823e04aa
MD5 5f786acd1070f0db11496c3a271300ed
BLAKE2b-256 5a1afb89e90ce664ce16cc124541e0d1b3548a0b9fc0b04a45f6bd64d21aafca

See more details on using hashes here.

Provenance

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