[파이썬] tkinter 다국어 지원

With the increasing demand for global applications, it has become essential to provide multilingual support to users. Tkinter, the standard Python interface for creating graphical user interfaces (GUIs), also offers the capability to support multiple languages.

In this blog post, we will explore how to implement 다국어 지원 (multilingual support) in tkinter using Python.

Setting up the Translation Files

The first step is to create translation files for each language you want to support. These files contain translations for all the text used in the application. To store translations, we will use the gettext module, which is part of the Python standard library.

import gettext

# Specify the language and translation file location
gettext.bindtextdomain('myapp', './locale')
gettext.textdomain('myapp')
_ = gettext.gettext

With this code, we have set up a translation domain named ‘myapp’ and specified the folder location where the translation files are stored.

Creating the GUI

Once we have our translations ready, we can start creating our GUI using tkinter. For every text that needs to be translated, we will use the _ function to wrap the text.

import tkinter as tk

root = tk.Tk()
root.title(_('My Application'))

label = tk.Label(root, text=_('Hello, World!'))
label.pack()

button = tk.Button(root, text=_('Click Me!'))
button.pack()

root.mainloop()

In this code, we have used the _ function to wrap the window title, label text, and button text. This allows us to easily translate these texts based on the user’s preferred language.

Loading Translations

To load the translations based on the user’s preferred language, we need to add a couple of lines of code before creating the GUI.

import os

# Get the user's preferred language from the environment variable
lang = os.getenv('LANGUAGE')
if not lang:
    lang = 'en_US'  # Default language if not specified

# Load the translations based on the user's preferred language
gettext.bindtextdomain('myapp', './locale')
gettext.textdomain('myapp')
gettext.bind_textdomain_codeset('myapp', 'UTF-8')
lang_dir = os.path.join('./locale', lang, 'LC_MESSAGES')
translate = gettext.translation('myapp', lang_dir, fallback=True)
_ = translate.gettext

In this code, we use the os module to get the user’s preferred language from the LANGUAGE environment variable. If the preferred language is not specified, we set a default language.

We then load the translations based on the user’s preferred language using the gettext.translation function. The gettext.bind_textdomain_codeset function is used to ensure proper encoding of the translation files.

Run the Application

With all the setup complete, we can run our application and see the multilingual support in action. By changing the LANGUAGE environment variable, you can test the application in different languages.

$ export LANGUAGE=fr_FR  # Set French as the preferred language
$ python app.py

The application will now display translated text based on the selected language.

Conclusion

In this blog post, we have learned how to add multilingual support to tkinter applications in Python. By utilizing the gettext module and loading translations based on user preferences, we can create user-friendly applications that cater to a global audience.

With tkinter’s 다국어 지원 capability, you can ensure that your applications are accessible and enjoyable for users worldwide. Happy coding!