Feature request: making gettext more robust

2023-06-15 Thread Gergely Kalmár
Hello all,

It seems that gettext is currently quite permissive – it falls back to the 
default language whenever a translation file is missing or if the requested 
message ID is missing from the translation file. This can lead to errors 
slipping through easily.

Consider this example from the documentation:

from django.http import HttpResponse
from django.utils.translation import gettext as _

def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)

Let's also assume there's two languages used in the application: LANGUAGES 
= [('en', 'English'), ('de', 'German')])

Note that even if you display the view with the German language, you will 
see "Welcome to my site." and will not receive any error or warning about 
the fact that the German translation file doesn't even exist yet.

Then create a translation catalog file and translate the sentence. Notice 
that the translated sentence appears now properly. Now change the output 
line to output = _("Welcome to my updated site."). Notice how the 
translated sentence turns back into English even when using German as a 
language and you don't get any warning or error again.

I think it would be great if there was a way to make gettext raise an error 
when the translation file is missing or when the msgid is missing. In order 
to add this feature in a backwards-compatible manner we could consider 
controlling this behavior through a new settings option. Alternatively, a 
warning could be also emitted, I could convert those into errors at least 
during testing. Silently falling back to a different language upon changes 
is just not great, I think.

Thanks,
Gergely

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/875cb2fa-4e69-4184-b5c5-f5d98c66fa8dn%40googlegroups.com.


Re: Feature request: making gettext more robust

2023-06-15 Thread Tobias Kunze
On 23-06-15 04:29:59, Gergely Kalmár wrote:
>It seems that gettext is currently quite permissive – it falls back to the 
>default language whenever a translation file is missing or if the requested 
>message ID is missing from the translation file. This can lead to errors 
>slipping through easily.
>
>I think it would be great if there was a way to make gettext raise an error 
>when the translation file is missing or when the msgid is missing.

Agreed that this is annoying behaviour, but as far as I can tell, there's not
much that Django can do. IIRC we only wrap Python's gettext module¹.

The relevant method, GNUTranslations.gettext, returns the original message if
no translation has been found, and it does so without indicating that this is
a fallback response².

AIUI this behaviour is rooted in GNU's gettext, which (just like the Python
version) allows you to set a priority list of languages to fall back to³.

Tobias

¹ https://docs.python.org/3/library/gettext.html
² https://docs.python.org/3/library/gettext.html#gettext.GNUTranslations
³ https://www.gnu.org/software/gettext/manual/gettext.html#The-LANGUAGE-variable

-- 
Tobias Kunze / rixx (er/he)
rixx.de software development
Mühlenbecker Weg 1, 16515 Oranienburg
https://rixx.de | https://pretalx.com
Tel.: +49 176 64636590

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/hwrmdsns7qerh2q5w34uthcb7pplbj2koc4aqogbabpcnmh2qd%40abkbnhls66bu.


Re: Feature request: making gettext more robust

2023-06-15 Thread Jure Erznožnik
The behaviour is the same on Android. iOS makes it more straight-forward 
because you HAVE TO have all translations in all languages you support.


LP,
Jure

On 15. 06. 23 16:15, Tobias Kunze wrote:

On 23-06-15 04:29:59, Gergely Kalmár wrote:

It seems that gettext is currently quite permissive – it falls back to the
default language whenever a translation file is missing or if the requested
message ID is missing from the translation file. This can lead to errors
slipping through easily.

I think it would be great if there was a way to make gettext raise an error
when the translation file is missing or when the msgid is missing.

Agreed that this is annoying behaviour, but as far as I can tell, there's not
much that Django can do. IIRC we only wrap Python's gettext module¹.

The relevant method, GNUTranslations.gettext, returns the original message if
no translation has been found, and it does so without indicating that this is
a fallback response².

AIUI this behaviour is rooted in GNU's gettext, which (just like the Python
version) allows you to set a priority list of languages to fall back to³.

Tobias

¹ https://docs.python.org/3/library/gettext.html
² https://docs.python.org/3/library/gettext.html#gettext.GNUTranslations
³ https://www.gnu.org/software/gettext/manual/gettext.html#The-LANGUAGE-variable



--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/273f1b3f-f953-e204-a0e9-95a7b3cd51e7%40gmail.com.


Re: Feature request: making gettext more robust

2023-06-15 Thread Michiel Beijen
> On 15 Jun 2023, at 16:15, Tobias Kunze  wrote:
> 
> On 23-06-15 04:29:59, Gergely Kalmár wrote:
>> It seems that gettext is currently quite permissive – it falls back to the 
>> default language whenever a translation file is missing or if the requested 
>> message ID is missing from the translation file. This can lead to errors 
>> slipping through easily.
>> 
>> I think it would be great if there was a way to make gettext raise an error 
>> when the translation file is missing or when the msgid is missing.
> 
> Agreed that this is annoying behaviour, but as far as I can tell, there's not
> much that Django can do. IIRC we only wrap Python's gettext module¹.
> 
> The relevant method, GNUTranslations.gettext, returns the original message if
> no translation has been found, and it does so without indicating that this is
> a fallback response².
> 
> AIUI this behaviour is rooted in GNU's gettext, which (just like the Python
> version) allows you to set a priority list of languages to fall back to³.

In ‘runtime’ indeed it is difficult to get a warning for an untranslated 
string; the best way to go about it is to generate the translation file and 
check for untranslated string in your translation file via some automated check 
such as a Github Action.

The added benefit this has is that if there is a translation string hiding in a 
lesser used part of your app such as the password reset form or so, it will 
still be spotted by the translation file generation, whereas you might 
otherwise miss this if you’re just clicking around in the app.

—
Michiel

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/4FE0664B-5E88-460C-826F-F0A85FC09D5B%40x14.nl.