r/Compilers • • 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.

7 Upvotes

11 comments sorted by

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

2

u/thisismugisha Oct 16 '26

It doesn't have to make sense to English speakers, nor does it actually have to be useful. I'd just like to know how it'd be done.

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.

1

u/thisismugisha Oct 16 '26

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

That'd go against the main objective. And it doesn't have to work for anyone else.

2

u/AromaticCredit1533 Oct 16 '26

Most Python libraries just write their own messages as strings tho, no i18n 🥲

But speaking of your idea about "symbolics". I use the GNOME desktop environment, and their ecosystem of GUI apps are very interesting, each UI-related text is written as a kind of formatting string and wrapped within an "i18n()" function, when the app is compiled the strings are substituted with the correct language, the string can contained "variables" that indicate "plural noun" or "singular noun" or "verb" and such and will be substituted according to a "grammar", all the while there are separate human "translator" contributors who will fill in those "grammar" files.

2

u/reini_urban Oct 16 '26

Keywords not. Causes a lot of trouble, and missing support for those poor users

1

u/thisismugisha Oct 16 '26

How useful the end results could actually be is irrelevant. It doesn't need to be adopted by anyone else. I'm well aware that there'd be little to no outside help that other English based languages have.

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.

1

u/thisismugisha Oct 16 '26

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 feel like your advice goes against what I wrote in my OP. I do not expect the results to be used internationally, nor do they actually need to be useful to anybody. I just want to see how it could happen.

1

u/leosmi_ajutar Oct 16 '26 edited Oct 16 '26

Then like I said, you would modify the compiler (or interpreter in Python's case) frontend accordingly. This would entail anywhere the toolchain looks for 'let', you would need to scan for whatever the French/Spanish/Dutch/etc. equivalents are.

Once the toolchain itself understands, can 'Python itself respond'.

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

rg -n -U -A 5 \
  '\b(?:_PyErr_(?:Format|SetString|SetObject)|PyErr_(?:Format|SetString|SetObject))\s*\(\s*PyExc_TypeError\b' \
  -g '*.[ch]'

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

PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %T", tmp)

If you look at a function like _PyList_Concat

https://github.com/python/cpython/blob/main/Objects/listobject.c#L856

if (!PyList_Check(bb)) {
    PyErr_Format(PyExc_TypeError,
              "can only concatenate list (not \"%.200s\") to list",
              Py_TYPE(bb)->tp_name);
    return NULL;
}

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/