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 = "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.2.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.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for scrapy_translate-1.2.tar.gz
Algorithm Hash digest
SHA256 2eab291db4e67677a12a7c22fcd93c45b9249948ddc6eb6035b51e8e0b0938e4
MD5 a7672faf660fb3a4075990979709694c
BLAKE2b-256 26566bfb1a567369d5f7f0a8ba97aeb635a97b52315c1f923d96a502919e2ad1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for scrapy_translate-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a1069577c5568abeb943b0da812b8a2ff58c4ac92a05976afc83ad75231a623
MD5 3413e996d774f1d8fb4f94beed25a9a4
BLAKE2b-256 511d0fd8850b73fd50000c825732f9db1b13557f481fdc81c06f00adeda16e68

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