Skip to main content

scrapy-translate: Scrapy pipeline to translate item fields

It's useful if you need to translate some text from parsed items into another language. But it is also suitable for other types of text processing.

Usage

1. Develop a translation service

The service should implement the following protocol to provide a mapping between the original and translated strings.

class TranslationProvider(Protocol):
    async def translate(self, strings: Collection[str], /) -> dict[str, str]: ...

2. Develop a caching service (optionally)

This may help to reduce the number of strings that need to be translated. The service should implement the following protocol.

class CacheProvider(Protocol):
    async def get(self, strings: Collection[str], /) -> dict[str, str]: ...

    async def set(self, strings: dict[str, str], /) -> None: ...

3. Add metadata to the item's class fields

The pipeline will only process the specified fields. Strings and lists of strings are supported as field values.

class Item(scrapy.Item):
    field = Field(translate=True)  # type: Union[str, list[str]]

It's also possible to translate HTML markup text while preserving the structure.

class Item(scrapy.Item):
    field = Field(translate=True, translate_html=True)

If you do want certain field values to be cached, you can specify that.

class Item(scrapy.Item):
    field = Field(translate=True, translate_cache=True)

4. Adjust project settings

Set the translation service class to the following setting.

TRANSLATION_PROVIDER = "myproject.services.MyTranslationProvider"

You can optionally set the caching service class. By default, there is no caching.

TRANSLATION_CACHE_PROVIDER = "myproject.services.MyCacheProvider"

And activate the pipeline.

ITEM_PIPELINES = {
    "scrapy_translate.pipeline.TranslatePipeline": 100,
}

Metadata

Release files for scrapy-translate 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scrapy-translate 1.3
File Size Uploaded
scrapy_translate-1.3.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scrapy-translate 1.3
File Interpreter ABI Platform
scrapy_translate-1.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / scrapy_translate-1.3.tar.gz

Download URL scrapy_translate-1.3.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d4cc8151d382efeedee9dc1a7d29ad8dce5b064f0422ddbd537e76aef25335c2
BLAKE2b-256 checksum
How to use checksums
a03fcd711111b4b734e75c4077a0142e50b765ab30322b8e931f3d3ac7d5a6e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / scrapy_translate-1.3-py3-none-any.whl

Download URL scrapy_translate-1.3-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
adb64142b9d8547d5d6bb4a894810d6cc7aae2b4050d8c96a5bc8c44fbca96ee
BLAKE2b-256 checksum
How to use checksums
50d97834dfc5a58959422580956a05521c994669ea8db6a03ff344cfb7fb4056
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page