Localization#
ttkbootstrap translates its built-in strings — dialog buttons, date names, and
the like — through Tk’s message catalog. L looks up a translation,
set_locale switches the active locale, LocaleVar is a variable that
re-translates itself when the locale changes, and MessageCatalog is the
lower-level catalog API. The Localization guide shows them in use.
- L(src, *args, **kwargs)#
Translate
srcfor the current locale, then Python-str.formatit.The universal i18n idiom (
_()), resolved at call time – correct for the common “pick the locale at startup” case. Uses Pythonstr.format({}/{0}/{name}fields), sidestepping the Tclformatpath; for%-style specifiers useMessageCatalog.translateinstead.
- set_locale(locale)#
Set the application locale.
Called before
App()exists, the locale is queued and applied when the root is created (the intended top-of-file use). If a root already exists it applies immediately, emitting<<LocaleChanged>>so liveLocaleVarvariables re-translate.- Parameters:
locale (str)
- Return type:
None
- class LocaleVar(master=None, src='', *args, name=None, **kwargs)#
Bases:
StringVarA
StringVarthat re-translates its source on<<LocaleChanged>>.Hold a source string and show its translation for the current locale; pass it as
textvariable=to any (vanilla or themed) widget for live language switching – whenset_locale(...)changes the locale, the variable re-translates itself. Format args, if any, are applied with Pythonstr.format(likeL).Every live
LocaleVaris driven by a single<<LocaleChanged>>binding on the application root (wherelocale()generates the event), regardless of the variable’s ownmaster. The registry holds only weak references, so a droppedLocaleVaris collected without any cleanup call; usestop_tracking()to opt one out explicitly while keeping it alive.Construct a string variable.
MASTER can be given as master widget. VALUE is an optional value (defaults to “”) NAME is an optional Tcl name (defaults to PY_VARnum).
If NAME matches an existing variable and VALUE is omitted then the existing value is retained.
- set_source(src, *args, **kwargs)#
Replace the source string (and any format args) and re-translate now.
- stop_tracking()#
Stop re-translating on
<<LocaleChanged>>(leaves it at its current value). Also happens automatically when the variable is garbage-collected.- Return type:
None
- class MessageCatalog#
Bases:
objectStatic wrapper for the Tcl/Tk
::msgcatmessage catalog commands.- static load(dirname)#
Searches the specified directory for files that match the language specifications returned by
preferences. Each file located is sourced.
- static locale(newlocale=None)#
Sets the locale to newlocale. If newlocale is omitted, the current locale is returned, otherwise the current locale is set to newlocale. The initial locale defaults to the locale specified in the user’s environment.
Setting a new locale emits a
<<LocaleChanged>>virtual event on the default root so live widgets can re-translate.
- static max(*src)#
Given several source strings, max returns the length of the longest translated string. This is useful when designing localized GUIs, which may require that all buttons, for example, be a fixed width (which will be the width of the widest button).
- static preferences()#
Returns an ordered list of the locales preferred by the user, based on the user’s language specification. The list is ordered from most specific to the least preference. If the user has specified LANG=en_US_funky, this method would return {en_US_funky en_US en}.
- static set(locale, src, translated=None)#
Sets the translation for ‘src’ to ‘translated’ in the specified locale. If translated is not specified, src is used for both.
- static set_many(locale, *args)#
Sets the translation for multiple source strings in
argsin the specified locale and the current namespace. Must be an even number of args.
- static translate(src, *fmtargs)#
Returns a translation of src according to the user’s current locale.
This is the main function used to localize an application. Instead of using an English string directly, an applicaton can pass the English string through
translateand use the result. If an application is written for a single language in this fashion, then it is easy to add support for additional languages later simply by defining new message catalog entries.- Parameters:
src (str) – The string to be translated.
*fmtargs (Any, optional) – Extra arguments passed internally to the Tcl [format](https://www.tcl-lang.org/man/tcl/TclCmd/format.html) command (msgcat applies
%-style specifiers). For the Pythonstr.formatidiom, useLinstead.
- Returns:
str – The translated string.
- Return type:
See also#
Localization — how to use it, with examples.