mkdocs-static-i18n icon indicating copy to clipboard operation
mkdocs-static-i18n copied to clipboard

fix: translate indented admonitions

Open joapuiib opened this issue 4 months ago • 8 comments

This PR allows detecting indented admonitions and translating them.

Closes #328

It also modifies pyproject.toml to be able to pass args to pytest.

[tool.hatch.envs.test.scripts]
 test = [
-    "pytest -xs",
+    "pytest -xs {args:tests}",
 ]

joapuiib avatar Nov 05 '25 13:11 joapuiib

Thanks a lot @joapuiib , that's super clear

@ShimYama also states:

Also, while admonitions are not case-sensitive, their translations are case-sensitive.

What would you have to say about that please?

ultrabug avatar Nov 05 '25 17:11 ultrabug

Also, while admonitions are not case-sensitive, their translations are case-sensitive.

I did miss that completely. I came across the indenting problem, and I found the issue after I wrote the fix.

Let me try to fix that as well.

joapuiib avatar Nov 05 '25 17:11 joapuiib

@ultrabug I think I've fixed it by transforming the type to lowercase before checking if a translation is available at admonitions_translation.

if (
    not title or title.strip() == ""
) and admonition_type.lower() in admonition_translations:
    new_title = admonition_translations[admonition_type.lower()]
    line = f'{indent}{marker}{admonition_type} "{new_title}"'

A 'limitation' would be that admonitions_translation keys should be lowercase.

"admonition_translations": {
    "tip": "Conseil",
    "warning": "Avertissement",
}

joapuiib avatar Nov 05 '25 17:11 joapuiib

@joapuiib out of my head I think we can overcome this if:

  1. we update the config parsing part of the admonition_translations and force .title() on admonition_translations keys
  2. make admonition_type = admonition_type = m.group("type").title()

?

ultrabug avatar Nov 06 '25 17:11 ultrabug

we update the config parsing part of the admonition_translations and force .title() on admonition_translations keys

I thought about this option and I think it's a good approach.

make admonition_type = admonition_type = m.group("type").title()

This would also work, but the translated markdown would be always in lowercase, even if the user writes it in uppercase. I don't know about admonitions extension implementation, but in my opinion is better to leave it as the user has written it.

joapuiib avatar Nov 06 '25 19:11 joapuiib

@ultrabug Should we force .title() or .lower()?

I'd rather store admonition_types keys as lowercase and leave the admonition_type = m.group("type") as written by the user in the document.

diff --git a/mkdocs_static_i18n/plugin.py b/mkdocs_static_i18n/plugin.py
index b8ccd92..5dda0e9 100644
--- a/mkdocs_static_i18n/plugin.py
+++ b/mkdocs_static_i18n/plugin.py
@@ -47,6 +47,11 @@ class I18n(ExtendedPlugin):
         )
 
         admonition_translations = self.current_language_config.admonition_translations or {}
+
+        admonition_translations = {
+            k.lower(): v for k, v in admonition_translations.items()
+        }
+
         if len(admonition_translations) > 0 and (
             "markdown_extensions" not in config or "admonition" not in config["markdown_extensions"]
         ):

joapuiib avatar Nov 08 '25 11:11 joapuiib

sorry @joapuiib

Ok for lower() ofc

ultrabug avatar Dec 03 '25 12:12 ultrabug

@ultrabug I've applied the previous patch in order to force lower() in all admonition_translations keys.

Both problems should be fixed: indentation and case-insensitive types.

joapuiib avatar Dec 04 '25 12:12 joapuiib

Thanks a lot @joapuiib

ultrabug avatar Dec 09 '25 17:12 ultrabug