ultrabug / mkdocs-static-i18n

MkDocs i18n plugin using static translation markdown files
https://ultrabug.github.io/mkdocs-static-i18n/
MIT License
223 stars 37 forks source link

Localized names for theme switch #239

Closed jokelbaf closed 11 months ago

jokelbaf commented 12 months ago

Hello! I was wondering if there is any way to localize these switch names for dark and light themes:

theme:
  ...
  palette:
    - scheme: default
      toggle:
        icon: fontawesome/solid/moon
        name: Switch to dark mode
    - scheme: slate 
      toggle:
        icon: fontawesome/solid/sun
        name: Switch to light mode    

What I tried was:

plugins:
  - i18n:
      docs_structure: folder
      languages:
        - locale: en
          name: English
          build: true
          default: true
          theme:
            palette:
              - scheme: default
                toggle:
                  name: Switch to dark mode
              - scheme: slate 
                toggle:
                  name: Switch to light mode
        - locale: ru 
          name: Русский
          build: true
          theme:
            palette:
              - scheme: default
                toggle:
                  name: Включить тёмную тему
              - scheme: slate 
                toggle:
                  name: Включить светлую тему

But I got the following error:

Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\mkdocs\livereload\__init__.py", line 190, in _build_loop
    func()
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\mkdocs\commands\serve.py", line 67, in builder
    build(config, live_server=None if is_clean else server, dirty=is_dirty)
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\mkdocs\commands\build.py", line 340, in build
    _build_theme_template(template, env, files, config, nav)
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\mkdocs\commands\build.py", line 110, in _build_theme_template
    output = _build_template(template_name, template, files, config, nav)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\mkdocs\commands\build.py", line 89, in _build_template
    output = template.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\environment.py", line 1301, in render
    self.environment.handle_exception()
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\material\404.html", line 4, in top-level template code
    {% extends "main.html" %}
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\material\main.html", line 4, in top-level template code
    {% extends "base.html" %}
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\material\base.html", line 143, in top-level template code
    {% block header %}
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\material\base.html", line 144, in block 'header'
    {% include "partials/header.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\material\partials\header.html", line 47, in top-level template code
    {% include ".icons/" ~ option.toggle.icon ~ ".svg" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\loaders.py", line 218, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: .icons/.svg

Am I doing something wrong? I have no idea what this error means because I have never saw it before and it is gone when I remove theme from locales in mkdocs.yml. I am using mkdocs-static-i18n 1.0.0.

ultrabug commented 12 months ago

@JokelBaf I'm impressed by how fast you gave a try to the upcoming release :tornado: !

Can you provide me with a repository I can reproduce this from maybe?

jokelbaf commented 12 months ago

@ultrabug Of course. It is https://github.com/MahiruBot/mahirubot.github.io. I commented all lines in mkdocs.yml that caused troubles because github actions failed to build.

ultrabug commented 11 months ago

@JokelBaf thanks. This happens because the theme override for languages does not specify the icon option which is not copied over by the plugin. That's a bug on my part, working on a fix!

ultrabug commented 11 months ago

Fixed on main branch ;) thanks again

jokelbaf commented 11 months ago

Thank you for quick response and fix of the bug, everything works perfectly now! Can I ask another question about 1.0.0 here? Since the question is quite simple, I don't want to create a new issue for it.

Is there any way to localize site_description? When I tried to use site_description property in the plugin config, I got a "Unrecognised configuration name" warning.

ultrabug commented 11 months ago

Is there any way to localize site_description? When I tried to use site_description property in the plugin config, I got a "Unrecognised configuration name" warning.

I just tried and it works as intended, maybe you misspelled it? please show a YAML

jokelbaf commented 11 months ago

I just tried and it works as intended, maybe you misspelled it? please show a YAML

Well, here is my config - https://github.com/MahiruBot/mahirubot.github.io/blob/master/mkdocs.yml.

The strange thing is that it really works, description is localized. But I keep getting warnings on every mkdocs serve command:

WARNING -  Config value 'plugins': Plugin 'i18n' option 'languages': Sub-option 'site_description': Unrecognised configuration name: site_description
WARNING -  Config value 'plugins': Plugin 'i18n' option 'languages': Sub-option 'site_description': Unrecognised configuration name: site_description
...
INFO    -  mkdocs_static_i18n: Overriding 'en' config 'site_description' with '...'
INFO    -  mkdocs_static_i18n: Overriding 'ru' config 'site_description' with '...'
ultrabug commented 11 months ago

My eyes were not seeing those mkdocs generated WARNING messages :see_no_evil: sorry!

I know how to fix that, gimme a few

ultrabug commented 11 months ago

done, warnings should be gone!

jokelbaf commented 11 months ago

Thank you!