r/Compilers • u/thisismugisha • Oct 16 '26
How would you go about translating programming language keywords, documentation and error messages to other non-English languages?
Take Python for instance.
>>> listss = [1, 2, "sfd"]
>>> kook = listss + 2
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
kook = listss + 2
~~~~~~~^~~
TypeError: can only concatenate list (not "int") to list
>>>How would I translate all the results given to me by Python into another language of my choosing, and get something like below (written in Kinyarwanda):
>>> listss = [1, 2, "sfd"]
>>> kook = listss + 2
Ibisobanuro (Ihamagarwa rya mbere riri hejuru yurutonde):
Inyandiko "<python-shyira-1>", umurongo wa 1, muri <igitabo>
kook = listss + 2
~~~~~~~^~~
Ikosa ry'ubwoko: urutonde ugendana nurundi rutonde; ntabwo rugendana numubare wuzuye.
>>>The intention is not to create custom messages, exceptions, and documentation that sits beside or on top of python. I want python itself to respond like this, from the get-go?
I would like the translation to be complete. CLI options, help messages, information on built-in modules, keywords, everything.
While thinking about this, I realized that the end result would just be a completely new language, and I'm fine with that, but I also don't know enough about creating languages or where to get the source code of an existing language to translate it to a new language, so I come here to ask for guidance.
6
u/MatthiasWM Oct 16 '26
In the 80s, there were BASIC interpreters available where keywords an error messages were in German. Never a commercial success. It’s just too annoying, none of the documentation applies, and German has complex grammar, so that the English flow did not apply and everything became unreadable. On top of that, German Umlaute äöü we’re not standardized, so copying a German program to another machine became gibberish.
For I=0 to 10 step 2
Für I=0 bis 10 Schritt 2 ; makes no Sense
3
u/AustinVelonaut Oct 16 '26
I haven't looked at the Python source, but I think that error messages should be symbolic (i.e. an abstract data type) that has a pluggable "localization" interface for actually printing in different languages, or at least have the actual concrete strings isolated to a single module, so that modifying it is fairly easy.
Handling actual language keywords would be a bit more difficult, though, and lead to a balkanization of code.
2
u/reini_urban Oct 16 '26
Keywords not. Causes a lot of trouble, and missing support for those poor users
1
u/Slow-Mechanic-7427 Oct 16 '26 edited Oct 16 '26
6In my experience, the language itself and inline comments/documentation stays in the primary language. Otherwise the frontend will grow exponentially complex due to left-to-right vs right-to-left, keywords being single vs multiple wordd, etc.
For error messages I assume you have error codes? Simply match them up to their associated messages and return it from a function. How you go about this can be as simple or complex as you like.
I started with if blocks, such as if (ecode = 1) return "index out of bounds" if (ecode =2) return "out of memory" .... etc. One of these functions per language then have another function that looks at a cli flag if(lang = "en") genEnglishError(code) if (lang = "fr") genFrenchError(code).
This is obviously quite basic so once my compiler/language supported string intrepolation I modified error messages to have more info with placeholders.
Nowadays I have moved onto a data system with tables so localization is done outside of compiler in seperate files. Such as csv for instance. A particular language can be baked in at compile time of compiler or passing a cli flag when users are compiling their program. This way programmers arent handling it anymore.
2
u/seg_lol Oct 16 '26 edited Oct 16 '26
CPython doesn't currently have a localization system for error messages. But this is a neat project!
You could scan with
I get just over 1700 occurrences, I am sure there are more.
For a demo, you could wrap python in a process that detects the exceptions and either looks up a static re-canned translation, or uses an LLM to translate.
There is a language that is Python like for teaching kids programming that has translations for most languages, you should check it out. https://www.hedy.org/
I think it would be really neat if CPython was localized.
If you look in the CPython source you will see lots of lines like
If you look at a function like
_PyList_Concathttps://github.com/python/cpython/blob/main/Objects/listobject.c#L856
That string passed to PyErr_Format would need to be localized, which there are many ways to achieve this.
https://en.wikipedia.org/wiki/C_localization_functions
https://www.gnu.org/software/gettext/