Translation methods¶
In the Sphinx build configuration file (conf.py
), translate
codelist CSV files and JSON Schema files.
import os
from glob import glob
from pathlib import Path
from ocds_babel.translate import translate
def setup(app):
basedir = Path(__file__).resolve().parents[1]
localedir = basedir / 'locale'
language = app.config.overrides.get('language', 'en')
headers = ['Title', 'Description', 'Extension']
translate([
(glob(str(basedir / 'schema' / '*-schema.json')), basedir / 'build' / language, 'schema'),
(glob(str(basedir / 'schema' / 'codelists')), basedir / 'build' / language, 'codelists'),
], localedir, language, headers)
translate
automatically determines the translation method to used based on filenames.
The arguments to translate
are:
A list of tuples. Each tuple has three values:
Input files (a list of paths of files to translate)
Output directory (the path of the directory in which to write translated files)
Gettext domain (the filename without extension of the message catalog to use)
Locale directory (the path of the directory containing message catalog files)
Target language (the code of the language to translate to)
Optional keyword arguments to replace
{{marker}}
markers with values, e.g.version='1.1'
Methods are also available for translating extension.json
and for translating Markdown files.
Install requirements for Markdown translation¶
To translate Markdown files, you must install:
pip install ocds-babel[markdown]
- ocds_babel.translate.translate(configuration, localedir, language, headers, **kwargs)[source]¶
Write files, translating any translatable strings.
For translated strings in schema files, replace {{lang}} with the language code. Keyword arguments may specify additional replacements.
- ocds_babel.translate.translate_codelist(io, translator, headers=(), **kwargs)[source]¶
Accept a CSV file as an IO object, and return its translated contents in CSV format.
- ocds_babel.translate.translate_codelist_data(source, translator, headers=(), **kwargs)[source]¶
Accept CSV rows as an iterable object (e.g. a list of dictionaries), and return translated rows.
- ocds_babel.translate.translate_schema(io, translator, **kwargs)[source]¶
Accept a JSON file as an IO object, and return its translated contents in JSON format.
- ocds_babel.translate.translate_schema_data(source, translator, **kwargs)[source]¶
Accept JSON data, and return translated data.