0

so i'm building a f1 race simulator in python and i want to make it multi language, so basically what i'm doing is creating three files, main.py italian.py and english.py

#main.py
import italian as italian
import english as english
language = input('insert a language: ')
print(language.starting_message)
#italian.py
starting_message = ('uno, due tre, quattro, allo spegnimento il via al gran premio.')
#english.py
starting_message = ("and it's lights out and away we go")

my question is, is there any way that i can choose which module to use?

  • Import the various modules inside conditional blocks (`if language == "English": import english as the_language` etc.)? Edit: I should add that this is comment is just answering your question at face value. @AdrienDerouene makes a great point saying that you *shouldn't* solve your *actual* problem this way – even though you *can*. – gspr Dec 05 '22 at 16:34
  • 2
    You take the problem in the wrong way, you should use file that store all messages with a particular key, so that you just have to switch the file name to modify the language you want to use. – Adrien Derouene Dec 05 '22 at 16:34
  • 2
    Python has builtin support for [i18n](https://docs.python.org/3/library/i18n.html) – Peter Wood Dec 05 '22 at 16:39

1 Answers1

0

The value of language is a string, not a module. You need some way to map that string to the appropriate module, something like

import italian
import english

d = {'italian': italian, 'english': english}

language = input('insert a language: ')

print(d[language].starting_message)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    As pointed by @PeterWood, [`i18n`](https://docs.python.org/3/library/i18n.html) is the best and recommend way to go. – accdias Dec 05 '22 at 16:44