Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set python exception messages to use my native language?

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?

like image 896
Hugo Corrá Avatar asked Apr 19 '13 13:04

Hugo Corrá


2 Answers

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.

like image 144
Bakuriu Avatar answered Sep 21 '22 01:09

Bakuriu


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.

like image 42
James Avatar answered Sep 20 '22 01:09

James