How to change the languages(human) in my Python application

51 Views Asked by At

How can I implement a system for changing languages in an application. I'm making an application in English and I want that when I click on a button, all the text changes to another language (French). Also i use microframework flet and i develop for mobile devices.

I'm new to programming. I think you can just try to create the same code in another human language and just switch between them. But what other options do I have?

1

There are 1 best solutions below

0
Rafa On

You can store text in JSON files. When you select a language, the application loads a specific file. For example:

main.py:

import json

lan = input()
match lan:
    case "FR":
        file = "main_FR.json"
    case "EN" | _:
        file = "main_EN.json"

with open(file) as f:
    text = json.load(f)

# Section 1
title_1 = text["section_1"]["title"]
text_1 = text["section_1"]["text"]

main_EN.json:

{
  "section_1": {
    "title": "Welcome to the Application",
    "text": "This is the English version of our application. Enjoy exploring all the features."
  }
}

main_FR.json:

{
  "section_1": {
    "title": "Bienvenue dans l'Application",
    "text": "Ceci est la version française de notre application. Profitez de toutes les fonctionnalités."
  }
}

This is also an easy way to scale. If you want to add new languages, just create new json files and add in the match-case statement.