How to set python exception messages to use my native language?
Asked Answered
V

3

7

I'm working in a python library that is going to be used by Brazilians, the official speak language here is Portuguese! Basically, I want to get the Exception messages in portuguese, for example:

Instead of get...

>>> float('A')
ValueError: could not convert string to float: A

I wanna get this...

>>> float('A')
ValueError: não é possível converter a string para um float: A

Is there any way to do it?

Veratrine answered 19/4, 2013 at 13:45 Comment(7)
You could use a try and except clause where you then literally display your own (portugese) messageProtolithic
@BasJansen Yes I can, but sincerely I don't want to do it. The code that I've provided is just a small part of the problem, there are a lot more types of exceptions that I should handle... so, I was wondering if there is an easy way to solve this without writting a lot of code to convert english exceptions to portuguese exceptions.Eyeglass
@BasJansen you've essentially just asked Hugo Corra to rewrite all the error messages in python. Is there an easier way to do this?Vacillation
@SnakesandCoffee I must have misread then, I thought he wanted a specific exception in portugese not all. Doing it with except is definitely not the way to go if the goal is all the Errors. I do think that Python strongly discourages this level of localization however.Protolithic
that seems rewriting python in Portuguese.Ringhals
@thavan: for the builtin error messages, yes. A lot of software displays translated error messages based on user's current $LANGUAGE, such as bash, GNU coreutils (cp, rm, ls, etc), even --help is translated, so why not Python?Diligent
@BasJansen: why you say it strongly discourages? If Python provides and promotes tools such as the gettext module to i18n software, why doesn't it use such tools to i18n itself?Diligent
C
6

The CPython implementation does not provide any automatic mean of localizing error messages. Hence you should rewrite all python's error messages in portuguese and either add a lot of try...except blocks or replace sys.excepthook to handle the exceptions.

If you plan to do this I'd consider using the gettext module for the localization.

Other python implementations do have localized error messages(like IronPython).

In my opinion what you want to do is not a good practice. These brazilian developer must know a bit of English anyway if they want to look at some documentation of python/libraries(which aren't probably all translated in portuguese).

Also if you localize the error messages searching for them on the web becomes much harder.

Cryptonymous answered 19/4, 2013 at 14:14 Comment(3)
good points. but localized error messages are a good idea when localizing a program for the end user. the user does not need to know English, and an IOError's repr() is not a bad way to tell your user that his external hard drive was disconnected while the program was writing to it.Dilley
This gets into an important design question: should exceptions carry human-friendly messages? Human-friendly error messages naturally suggest showing those messages to end users, and then it makes sense for exception messages to be localized. Human-friendly error messages do not look like they exist only to guide developers (or for exception-handling code to use, in the rare case when that's appropriate, instead-of or in-addition-to defining a custom class for our exceptions, and maybe setting attributes on the exception).Hexapod
If we want to make it clear that developers are not supposed to be getting into the habit of exposing those errors to end-users, then we should maybe go out of our way to make the exceptions less end-user-friendly in a few specific ways - like using programmer English instead of natural English (dropping or abbreviating words, like how we do in variable, function, and class names), deliberately putting information in errors in formats weird/cryptic to non-developers but still easy for familiar developers to read, etc.Hexapod
S
2

The default language for python is English and one is always encouraged to use it (as per PEP 8).

So, as far as I know, they do not support/encourage using different languages like this, so you'll likely have to change everything yourself.

Succoth answered 19/4, 2013 at 14:9 Comment(0)
D
2

Python (actually CPython) currently has no internationalized error messages. In 2012 Mariano Reingart requested this in bug report #16344, called "Traceback Internationalization Proposal". After a lot of discussion they agreed that this PEP draft must move forward before further discussions, and so the bug was closed in 2015.

Maybe you can jump in at this bug report or the Python Dev mailing list and voice your opinion to raise interest on the subject, also perhaps reach Reingart and query about the current status on this issue.

Diligent answered 27/7, 2020 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.