Skip to main content

I18n with gettext for Hugo

Project description

hugo-gettext

I18n with gettext for Hugo.

Currently compliant with Hugo 0.110.0.

Install

pip install hugo-gettext

Usage

There are three commands corresponding to three steps that hugo-gettext supports:

  • hugo-gettext extract extracts messages from source files (files in the source language) to one or many POT files;
  • hugo-gettext compile compiles PO files to binary MO files;
  • hugo-gettext generate uses MO files to generate target files (files in target languages).

These are types of text that hugo-gettext can extract messages from and can generate in target languages:

  • Front matter and content in content files;
  • Strings in string file (i.e. "translation table") in i18n folder
    • other keys
    • Support comment
  • Menu item names, site title, and site description in site config file: all in languages.en config key:
    • title: title
    • description: params.description
    • menu: menu.main.<entry>.name
  • Data in data files: processed as Markdown (front matter text aren't processed as Markdown).

Each project can specify multiple text domains to work with (i.e. messages are extracted to multiple POT files and subsequently multiple PO files are available to use for generation), however all types of text outside content files always belong to a default domain which is derived from a name that each project must have. Content files can be associated with the default domain or custom domains.

Directory structure in each step

Compilation

  • From a folder containing subdirectories with PO files inside, in the form of <dir>/<lang_code>/<domain>.po
  • To a locale folder
  • Structure: locale/<lang_code>/LC_MESSAGES/<domain>.po

Generation

  • Conditions in front matter
  • hugo_lang_codes are prepended to absolute links in aliases dict in front matter
  • How data file generation works
  • Requirement for a language to be qualified (a config block is added and data files are generated, string file is generated anyway):
    • there's no content file to translate but string file is translated, or
    • there are content files to translate and some files are translated
  • A content file is considered translated if
    • The front matter is translated, or
    • There's nothing in the content, or
    • The translation rate of the content is higher than 50%

Markdown

CommonMark compliant. All core Markdown elements are supported, as well as table, and definition list.

Some notes about how different elements are handled:

  • Inlines: hard line breaks are replaced with
    , newlines and consecutive spaces are not kept;
  • Content of each HTML block isn't parsed into finer tokens but processed as a whole;

Shortcodes

  • If the string contains only one shortcode
  • Newlines are kept for arguments quoted with backticks
  • hg_stop shortcode to stop processing a content file

Attributes

Configuration

  • package: Project-Id-Version in POT metadata
  • default_domain_name
  • report_address and team_address: Report-Msgid-Bugs-To and Language-Team in POT metadata
  • excluded_keys: in front matter
  • excluded_data_keys: in data files
  • rtl_langs
  • shortcodes: can use * wildcard to indicate all shortcodes

Custom functions

The path of the file should be passed as an argument to the command line with -c or --customs option, or set in the config file with customs key.

The following functions are called to make default_domain_name, excluded_keys, report_address, team_address, and rtl_langs attributes of Config:

  • get_default_domain_name: will be called with package as an argument, returns package by default
  • get_custom_excluded_keys: returns an empty set by default
  • get_pot_fields: returns a dictionary of 'report_address' and 'team_address' keys
  • get_rtl_langs: returns a list of codes of RTL languages

Two functions are called during the generation step:

  • load_lang_names: returns an empty dictionary by default
  • convert_lang_code: function to convert gettext language codes to Hugo language codes, returns gettext language codes by default

Development

  • With Conda
conda env create -f environment.yml
conda activate hg
poetry install

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

hugo-gettext-0.6.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

hugo_gettext-0.6.0-py3-none-any.whl (39.8 kB view details)

Uploaded Python 3

File details

Details for the file hugo-gettext-0.6.0.tar.gz.

File metadata

  • Download URL: hugo-gettext-0.6.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.5

File hashes

Hashes for hugo-gettext-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3a7727affce3af14b0f1528d71236c268633f3918e0d9ebc84484e1c993c8598
MD5 ce0393a08d16d1394d96cf45edc27222
BLAKE2b-256 97079fdf180b849c11ee0f2d97445ad8cc358cd68683281c631853216f3063de

See more details on using hashes here.

File details

Details for the file hugo_gettext-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: hugo_gettext-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 39.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.5

File hashes

Hashes for hugo_gettext-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7815fa9a0b00ab4b9e8184792a04562f7655920f8b6003b6f99c6288aa622fe9
MD5 d33deae221feaff7f9f8485eb549925e
BLAKE2b-256 72804794e93521d2127145e6ee95b13af3a6e87aff888bf51c9bcaa7bb880d92

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page