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

Develop a translation service

The service should provide a mapping between the original and translated strings. The protocol is as follows.

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

Develop a caching service (optionally)

This may help to reduce the number of strings that need to be translated. The protocol is as follows.

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

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

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 don't want certain field values to be cached, you can specify that.

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

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

And activate the pipeline.

ITEM_PIPELINES = {
    "scrapy_translate.pipelines.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.0.tar.gz (6.6 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.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for scrapy_translate-1.0.tar.gz
Algorithm Hash digest
SHA256 dfd343b4bd639ca91c57efd3cd095ac1c2c2e54b37ba8001254da9d06c5a98a4
MD5 628eb5fc5931a33f18768f80a8a3054c
BLAKE2b-256 a4db7ee0296f4efb28e5fc130190186388e82daf6209e77ad86a96169e37705e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for scrapy_translate-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14d0fd1be671d20deddca9e5499718f760a5a64d2d5d8351dbfbafb9fee2db82
MD5 95a63698da1f6e36123995280246ebe1
BLAKE2b-256 667f20a6e29c28f5f5b035606c31da62f0ba1a85927b2c390c4d3cf04898f42f

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