Skip to content

Translations in themes remove messages provided by Nikola #3835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
htgoebel opened this issue Mar 23, 2025 · 4 comments · May be fixed by #3836
Open

Translations in themes remove messages provided by Nikola #3835

htgoebel opened this issue Mar 23, 2025 · 4 comments · May be fixed by #3836

Comments

@htgoebel
Copy link
Contributor

Environment

  • Python Version: 3.10
  • Nikola Version: Nikola v8.3.1
  • Operating System: GNU/Linux

Description:

When creating a theme that provides additional translated message, the messages provided by Nikola are no longer translated.

Expected

The messages provided by Nikola are till present in the translation.

Reproduce

To reproduce the issue, we call load_messages like nikola would do in a project.

  1. Save this code to a file called translations-bug.py
from nikola.utils import load_messages

# mimic the arguments "nkola build" would pass
msgs = load_messages(
    ['my-themes/dummy', 'nikola/data/themes/base'],
    {'de': ''},
    'de',
    ['themes'])

for s in (
        "Posted:",  # string translated in "base" theme
        "My new message",  # string translated in dummy theme
):
    if s not in msgs["de"]:
        print(f"{s:<20} string missing")
    elif msgs["de"][s] == s:
        print(f"{s:<20} not translated")
    else:
        print(f"{s:<20} translated to   {msgs['de'][s]}")
  1. Run it like this:
MSG_FILE='my-themes/dummy/messages/messages_de.py'
MSG_DIR=$(dirname $MSG_FILE)
mkdir -p $MSG_DIR 

echo '===== Without translation file in theme ---'
rm -rf $MSG_DIR/*
python translations-bug.py

echo
echo '===== With translation file in theme ---'
rm -rf $MSG_DIR/*
echo > $MSG_FILE 'MESSAGES = {"My new message": "Meine neue Meldung"}'
python translations-bug.py

echo
echo '===== With translation file containing no strings in theme ---'
rm -rf $MSG_DIR/*
echo > $MSG_FILE 'MESSAGES = {}'
python translations-bug.py

Output:

===== Without translation file in theme ---
Posted:              translated to   Veröffentlicht:
My new message       string missing

===== With translation file in theme ---
Posted:              not translated
My new message       translated to   Meine neue Meldung

===== With translation file containing no strings in theme ---
Posted:              not translated
My new message       string missing

Expected output:

===== Without translation file in theme ---
Posted:              translated to   Veröffentlicht:
My new message       string missing

===== With translation file in theme ---
Posted:              translated to   Veröffentlicht:
My new message       translated to   Meine neue Meldung

===== With translation file containing no strings in theme ---
Posted:              translated to   Veröffentlicht:
My new message       string missing

@htgoebel htgoebel added the bug label Mar 23, 2025
@Kwpolska Kwpolska added the minor label Mar 23, 2025
@Kwpolska
Copy link
Member

I don’t think we’ve ever documented that messages would be picked up from the parent theme, I can’t find any mention of that in our docs.

We haven’t changed messages in 7 years, and considering most users don’t override them, it will be easier for you to just copy the file from the base theme and modify it to your needs. I don’t think we’ll change that.

@htgoebel htgoebel linked a pull request Mar 23, 2025 that will close this issue
3 tasks
@htgoebel
Copy link
Contributor Author

The issue is not about overwriting, but about adding. Obviously the idea is to allow adding translations without the need to copy all the strings. Otherwise the code would simply load the theme's translation file.

I just created #3836 fixing the issue (and cleaning up the method).

@Kwpolska
Copy link
Member

As I understand the original code, its primary goal is to ensure all messages have some value (even if it’s the English one) to avoid crashes for missing messages. The proposed change looks OK to me, but needs more testing and needs to avoid overwriting messages with English ones if the base theme has those.

@htgoebel
Copy link
Contributor Author

needs to avoid overwriting messages with English ones if the base theme has those.

I agree on this, it's just what the former code does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants