Plugin LocalizationΒΆ
Kodi has its own specific localization system that is also used for addons. This system uses numeric codes for
extracting respective localized GUI strings from addon localization .po
files using getLocalizedString
method. For example:
import xbmcaddon
import xbmcgui
addon = xbmcaddon.Addon()
xbmcgui.Dialog().ok(addon.getLocalizedString(32000), addon.getLocalizedString(32001))
As you can see, this is neither intuitive nor convenient and may lead to errors. To improve code readability the
SimplePlugin library can emulate a popular GNU Gettext localization system that is also standard for Python
programs. This emulation layer is implemented through
initialize_gettext()
and
gettext()
methods that allow to access localized addon GUI strings using
English source strings instead of numeric codes. For example:
import xbmcgui
from simpleplugin import Plugin
plugin = Plugin()
_ = plugin.initialize_gettext()
xbmcgui.Dialog().ok(_('Hello World!'), _('This is sample text.'))
The initialize_gettext()
method returns
gettext
method object that is used to extract localized GUI stirngs.
The name of this method (or function) object can be any but traditionally it is named with a single underscore _
to
reduce typing. In the preceding example you can clearly see which strings or their localized versions are used in
addon/plugin GUI elements and this code is much more readable than in the first example.
Warning
To use the Gettext emulation you need to call
initialize_gettext()
method first.
All GUI strings obtained using the emulation feature must be present at least in English strings.po
file. Attempting to access a missing string will result in simpleplugin.SimplePluginError
exception.