feat: set up jinja2 i18n

This commit is contained in:
Corentin 2024-05-21 15:16:35 +02:00
parent 9c43efc848
commit 4bb63e7a55
6 changed files with 82 additions and 7 deletions

2
babel.cfg Normal file
View File

@ -0,0 +1,2 @@
[jinja2: src/**.html]
encoding = utf-8

View File

@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language: fr\n"
"Language-Team: fr <LL@li.org>\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"

View File

@ -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:

View File

@ -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')

View File

@ -3,10 +3,10 @@
<div class="nav-bar--menu">
<ul class="nav-bar--menu-section">
<li class="nav-bar--menu-item">
<a href="#" class="nav-bar--menu-link">Collective</a>
<a href="#" class="nav-bar--menu-link">{{ gettext('Collective') }}</a>
</li>
<li class="nav-bar--menu-item">
<a href="#" class="nav-bar--menu-link">Games</a>
<a href="#" class="nav-bar--menu-link">{{ gettext('Games') }}</a>
</li>
<li class="nav-bar--menu-item">
<a href="#" class="nav-bar--menu-link">News</a>

View File

@ -1,4 +1,4 @@
{% set title = "Games" %}
{% set title = gettext("Games") %}
{% set title_image = "/img/section-games.png" %}
{% extends "lib/section.html.j2" %}