From 4bb63e7a55dbae8ef1363edad493bd5b7828cdb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Tue, 21 May 2024 15:16:35 +0200 Subject: [PATCH] feat: set up jinja2 i18n --- babel.cfg | 2 ++ locale/fr/LC_MESSAGES/site.po | 29 +++++++++++++++++++++ noxfile.py | 47 ++++++++++++++++++++++++++++++++--- site.py | 5 ++++ src/lib/nav.html | 4 +-- src/pages/index/games.html | 2 +- 6 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 babel.cfg create mode 100644 locale/fr/LC_MESSAGES/site.po diff --git a/babel.cfg b/babel.cfg new file mode 100644 index 0000000..528ebdd --- /dev/null +++ b/babel.cfg @@ -0,0 +1,2 @@ +[jinja2: src/**.html] +encoding = utf-8 diff --git a/locale/fr/LC_MESSAGES/site.po b/locale/fr/LC_MESSAGES/site.po new file mode 100644 index 0000000..2a7bfed --- /dev/null +++ b/locale/fr/LC_MESSAGES/site.po @@ -0,0 +1,29 @@ +# French translations for PROJECT. +# Copyright (C) 2024 ORGANIZATION +# This file is distributed under the same license as the PROJECT project. +# FIRST AUTHOR , 2024. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2024-05-21 15:15+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language: fr\n" +"Language-Team: fr \n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.15.0\n" + +#: src/lib/nav.html:6 +msgid "Collective" +msgstr "Collectif" + +#: src/lib/nav.html:9 src/pages/index/games.html:1 +msgid "Games" +msgstr "Jeux" + diff --git a/noxfile.py b/noxfile.py index 48dd9d4..5a318c6 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,8 +1,11 @@ """Nox configuration file.""" +from pathlib import Path +from tempfile import TemporaryDirectory from shutil import copytree from nox import Session, session +_LOCALES = ["fr"] @session() def lint(session: Session) -> None: @@ -20,19 +23,55 @@ def mypy(session: Session) -> None: @session def build(session: Session) -> None: """Run unit tests.""" - session.install("nodeenv", "git+https://git.collectivit.org/collectivit/jean-website") + session.install( + "nodeenv", + "git+https://git.collectivit.org/collectivit/jean-website", + "babel", + "jinja2" + ) + session.run_always("nodeenv", "-p") session.run_always("npm", "install", "-g", "sass") - session.log("Building website...") - session.run("jwebsite", "build") - session.log("Building css...") session.run("npx", "sass", "src/style.scss", "build/style.css", "--style=compressed") session.log("Copying static files to build directory...") copytree("src/assets", "build", dirs_exist_ok=True) + with TemporaryDirectory() as locale_dir: + for locale in _LOCALES: + output_dir = Path(locale_dir) / locale / "LC_MESSAGES" + output_dir.mkdir(exist_ok=True, parents=True) + + session.run( + "pybabel", + "compile", + f"--input-file=locale/{locale}/LC_MESSAGES/site.po", + f"--output-file={output_dir}/site.mo", + "--use-fuzzy" + ) + + session.log("Building website...") + session.run( + "jwebsite", "build", + env={ + 'FROG_LOCALE_DIR': str(locale_dir) + } + ) + +@session +def extract_messages(session: Session) -> None: + session.install("babel", "jinja2") + + with TemporaryDirectory() as tmp_dir: + messages_file = Path(tmp_dir) / "messages.po" + session.run("pybabel", "extract", "--mapping", "babel.cfg", f"--output-file={messages_file}", ".") + session.run("pybabel", "update", "--domain=site", f"--input-file={messages_file}", "--output-dir=locale") + +@session +def compile_messages(session: Session) -> None: + session.install("babel", "jinja2") @session() def devenv(session: Session) -> None: diff --git a/site.py b/site.py index 50081ea..2a8d255 100644 --- a/site.py +++ b/site.py @@ -1 +1,6 @@ +from os import environ + +if "FROG_LOCALE_DIR" in environ: + site.set_translations("site", environ["FROG_LOCALE_DIR"], languages=["fr"]) + site.render('pages/index.html', 'index.html') diff --git a/src/lib/nav.html b/src/lib/nav.html index 2556e50..4380601 100644 --- a/src/lib/nav.html +++ b/src/lib/nav.html @@ -3,10 +3,10 @@