Skip to main content

Lektor i18n plugin

This plugin enables a smarter way to translate a Lektor static website using old-good PO files. So you can use your beloved translation processes and tools.

Principles

The idea of this plugin is to capture the sentences or paragraphs from your content and templates, and populate a standard Gettext PO file.

Using usual tools, user can translate these files, very easily. Then the plugin will merge the translations into new alternative content files, providing a translated website.

Configuration

Configuration file

configs/i18n.ini

content = en
translations = fr,es,it
i18npath = i18n
translate_paragraphwise = False
url_prefix = https://website_url/

Where :

  • content is the language used to write contents.lr files (default is en)
  • translations is the list of target languages (you want to translate into).
  • i18npath is the directory where translation files will be produced/stored (default is i18n). This directory needs to be relative to root path.
  • translate_paragraphwise specifies whether translation strings are created per line or per paragraph. The latter is helpful for documents wrapping texts at 80 character boundaries. It is set to False by default.
  • url_prefix is the final url of your lektor website. This provides
  • translators with a way to see the strings in context.

babel.cfg

If you plan to localise your templates as well, you can use {{ _("some string") }} in your templates. To make this work, babel for Python should be installed (pip install babel; maybe pip3). A babel.cfg also has to exist in your project root with this content:

[jinja2: **/templates/**.html]
encoding = utf-8
extensions=jinja2.ext.autoescape,jinja2.ext.with_

Translatable fields

In order for a field to be marked as translatable, an option has to be set in the field definition. Both blocks and flowblocks fields are subjects to translations.

In flowblocks/*.ini and/or models/*.ini, mark a field as translatable with:

[model]
name = Page
label = {{ this.title }}

[fields.title]
label = Title
type = string
translate = True

[fields.body]
label = Body
type = markdown
translate = True

Both title and body are now translatable. It means that during the parsing phase, all sentences from title or body fields from the contents.lr files with Page model will populate the collected PO file.

Another flowblock example:

[block]
name = Section Block
button_label = Section

[fields.title]
label = Title
type = string
translate = True

[fields.body]
label = Body
type = markdown
translate = True

[fields.image]
label = Image
type = select
source = record.attachments.images

[fields.image_position]
label = Image Position
type = select
choices = left, right
choice_labels = Left, Right
default = right

Here again, body and title will be translated. But image and image_position won't.

Non-English content

Thanks to a limitation of msginit it's not so easy to translate a website with default language set to anything but English.

So if your default content language is not English, you will have to edit the first contents-en.po file and remove the translations (by hand ?)...

Installation

Prerequisites

Lektor

This plugin has been tested with Lektor 3.x.

GetText

Both Gettext and Pybabel are required. For a Debian/Ubuntu system, this means a simple :

sudo apt-get install gettext python3-babel

On macOS, use a decent package manager, like MacPorts or Homebrew. With Homebrew:

brew install gettext

and then pip to fetch pybabel:

pip install babel

Installation

Very straightforward :

$ lektor plugins add lektor-i18n

Verify installation with a simple :

$ lektor plugins list
...
lektor-i18n (version 0.1)
...

Usage

The translation mechanism is hooked into the build system. So translating a website just means building the website.

$ lektor build

On first call, a new i18n directory (can be changed in configuration file) will be created on top the lektor tree.

This directory will be populated with a single contents.pot file, compiling all the sentences found by the plugin. The list of fields eligible to translation is configured in the models/flows definition with translate=True added to each field.

For each translation language (still from the configuration file), a content-<language>.po file will be created/updated. These are the files that need to be translated with your prefered tool (like POEdit or Transifex).

All translation files (contents-*.po) are then compiled and merged with the original contents.lr files to produce all the contents-<language>.lr files in their respective directories.

Project file

It's still the user responsability to modify the project file in order to include the expected languages :

[alternatives.en]
name = English
primary = yes
locale = en_US

[alternatives.fr]
name = French
url_prefix = /fr/
locale = fr

See Lektor Documentation for more information.

Support

This plugin is provided as-is by NumeriCube a human-sized Paris-based company prodiving tailored services to smart customers.

We will be happy to try to help you with this plugin if need. Just file an issue on our [GitHub account][],

[GitHub account]: https://gihub.com/numericube/lektor-i18n-plugin/)

Release files for lektor-i18n 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lektor-i18n 0.5.0
File Size Uploaded
lektor_i18n-0.5.0.tar.gz 36.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lektor-i18n 0.5.0
File Interpreter ABI Platform
lektor_i18n-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 70.5 kB

Release files / lektor_i18n-0.5.0.tar.gz

Download URL lektor_i18n-0.5.0.tar.gz
Size 36.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5e28be002a2486fcb51a840a0cc54af35a52e7164202cfa033071a082e7c568c
BLAKE2b-256 checksum
How to use checksums
d997931ef91dd13749a546127c82fd02b10a3978928c0bdb50c4d846dc3cce17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.20.1 CPython/3.13.5 Linux/6.12.57+deb13-amd64

Release files / lektor_i18n-0.5.0-py3-none-any.whl

Download URL lektor_i18n-0.5.0-py3-none-any.whl
Size 34.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e54180ab756afdc0cb5497111dd4afaf5d99ffa260e96d091de9aee204fc47f5
BLAKE2b-256 checksum
How to use checksums
86163ec481e9221e42cd93a7f5d5d3433b0f3f1b7b7f990d00c0e542da1addbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.20.1 CPython/3.13.5 Linux/6.12.57+deb13-amd64

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.2

1 release file

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page