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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scrapy_translate-1.3.tar.gz.
File metadata
- Download URL: scrapy_translate-1.3.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4cc8151d382efeedee9dc1a7d29ad8dce5b064f0422ddbd537e76aef25335c2
|
|
| MD5 |
d58715c06988b380fc1323bf3ad97c4b
|
|
| BLAKE2b-256 |
a03fcd711111b4b734e75c4077a0142e50b765ab30322b8e931f3d3ac7d5a6e8
|
File details
Details for the file scrapy_translate-1.3-py3-none-any.whl.
File metadata
- Download URL: scrapy_translate-1.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adb64142b9d8547d5d6bb4a894810d6cc7aae2b4050d8c96a5bc8c44fbca96ee
|
|
| MD5 |
b5cf04d047bcb49aae63a1b2b792bafb
|
|
| BLAKE2b-256 |
50d97834dfc5a58959422580956a05521c994669ea8db6a03ff344cfb7fb4056
|