Variable multi values i.e. language translated terms

Hello Guys,
I searched for this feature but I did not find anything.
We are teached since old time that a variable always has one value, so:

a = “Hello”
print(a)
Hello

Now imagine to run above code to no english tongue people, i.e. for italian people code should be:

a = “Ciao”
print(a)
Ciao

And now imagine above code must run both in English countries both in Italy. The python code should be a something like this:

def get_value():
if context.get(“lang”) == “it_IT”:
return “Ciao”
else:
return “Hello”
print(get_value())
Hello
context = {“lang”: “it_IT”}
print(get_value())
Ciao

I know that all worldwide python codes implements the tranlsation mechanism but a python implemenation will be very useful for all.

Basically string value should be implemented like a dictionary. There is no new code to create but only integrating python code in str class.

Look at the following example code:

class string(str):
    __default_lang__ = "en_US"
    __i18n_value__ = {}

    def i18n_value(self, value=None, lang=None):
        if lang and value:
            self.__i18n_value__[lang] = value

    def __repr__(self):
        if globals()["__context__"].get("lang", "") in self.__i18n_value__:
            return self.__i18n_value__[globals()["__context__"]["lang"]].__repr__()
        return super().__repr__()


__context__ = {}
a = string("Hello")
print(a.__repr__())

a.i18n_value(value="Ciao", lang="it_IT")
print(a.__repr__())

__context__["lang"] = "it_IT"
print(a.__repr__())

Results are:
‘Hello’
‘Hello’
‘Ciao’

I have some ideas about python syntax but before explain I ask if this idea should be interesting for you an if it is PEPable.?

Moving this to Help as i18n and l10n systems are already available.

2 Likes

It only has one value at a time. Of course you can change the value later.

Yes, you will have to do “something like” this - in every programming language, not just Python. The program will not change the variable name just because of some language setting - you have to write code that changes it.

Designing the code so that you can change the values easily (e.g. making a dictionary like this and writing code to look up the right text), is called internationalization (i18n); setting up the values for each language (i.e. filling in the information, to use "Ciao" when the language is Italian), is called localization (l10n).

There are many tools that can help with this, including in the standard library, as David linked.

Programming languages will not do this automatically for you, because

  • there are many values in the program that shouldn’t change because of the language;

  • there are many other reasons why the value should be different when the program runs on a different computer;

  • there are many other kinds of changes that have to be made, in order to “translate” the text, besides just replacing some ordinary strings. (For example, the translated text could be much shorter or much longer, which could change how the program looks, especially for a GUI program. Also for example, if you substitute some strings into another string, the necessary order and position could be different in different languages.)

It’s quite impossible for a programming language to guess all these things for you, so you have to write the code to tell it what to do.

3 Likes

Hi Karl, thanks for your answer. :smile:
You got right and hit how the trouble is wide. I did not wish a way to translate a variable, it was just an simple example to expose my proposal.
The assumption “It only has one value at a time” is almost true but in real world there are a lot of situations which a variable has to have more values in the same time. I say just the first I thought:

  • Translation value
  • Values in different unit of measure (i.e meter vs yard)
  • The address of people
  • … and so on

As you said “It’s quite impossible for a programming language to guess all these things for you, so you have to write the code to tell it what to do” so my idea is not about how to store a variable but about a kind of builtins functions to manage these objects.

Please read again my example from this point of view. I agree it is due of the developer to design how to implement the code; in my mind, python could make available some new functions for builtins classes:

  • the initialization function (like __init__): in my above example I set __default_lang__ and __i18n_value__; it is not necessary but it makes code clearer (read below about assignment)
  • the function to return a value (like _repr__ in my above example); name could be __return__?
  • the function to set depending values: in my above example I declared i18n_value for this purpose

Obviously must be declared some strong bonds to avoid side effects, I think:

  1. Concetually all the values are depending each other
  2. Builtins classes cannot be updated: developer must create a own class which inherits from builtin class (like in my above example I created class string)
  3. Every object (variable) has its own value exactly as today: when not explicitely it is this value to be returned by class instance in order to guarantee 100% of compatibility with existen code; the = (equal sign) assigns always this value and should remove all depening values, perhaps calling init?

I have a lot of dubts so I would like share this idea with you and hear your ideas.

Thanks in Advance