Skip to main content

Scrapy text translation pipeline

Project description

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 = "scrapy_translate.providers.NullCacheProvider"
TRANSLATION_CACHE_PROVIDER = "myproject.services.MyCacheProvider"

And activate the pipeline.

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

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

scrapy_translate-1.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

scrapy_translate-1.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file scrapy_translate-1.1.tar.gz.

File metadata

  • Download URL: scrapy_translate-1.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.5

File hashes

Hashes for scrapy_translate-1.1.tar.gz
Algorithm Hash digest
SHA256 1798125112c6c5bfcf8e47667617efc2548b2cc251c614cde7dbd2a6a740111a
MD5 39d47defdd72cea07c23bd8944ec5dbf
BLAKE2b-256 c2e80f62b591ec11a8cfb20ba003c484f8c0936986ab67d299c7575b14ec77f1

See more details on using hashes here.

File details

Details for the file scrapy_translate-1.1-py3-none-any.whl.

File metadata

  • Download URL: scrapy_translate-1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.5

File hashes

Hashes for scrapy_translate-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5620e9fe0c51102d1e6cb488e7e2b3c2c51829dda13f63dbbac4221821c6f8f
MD5 4f927b2fb956485ff8fb8baff55885fd
BLAKE2b-256 211a0a88855300a6aadc2ddad12d3dd868e8681c5dc93a3e472eb8899d41f217

See more details on using hashes here.

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