Add get_builder() for plugins

This commit is contained in:
Philipp Hörist 2018-10-12 21:59:49 +02:00
parent 83c737312b
commit cc62761da3
2 changed files with 25 additions and 3 deletions

View File

@ -13,6 +13,7 @@
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Any
import os
import sys
@ -35,9 +36,19 @@ log = logging.getLogger('gajim.gtk.util')
class Builder:
def __init__(self, filename: str, widget: str = None) -> None:
def __init__(self,
filename: str,
widget: str = None,
domain: str = None,
gettext_: Any = None) -> None:
self._builder = Gtk.Builder()
self._builder.set_translation_domain(i18n.DOMAIN)
if domain is None:
domain = i18n.DOMAIN
self._builder.set_translation_domain(domain)
if gettext_ is None:
gettext_ = _
file_path = os.path.join(configpaths.get('GUI'), filename)
@ -46,7 +57,8 @@ class Builder:
tree = ET.parse(file_path)
for node in tree.iter():
if 'translatable' in node.attrib and node.text is not None:
node.text = _(node.text)
node.text = gettext_(node.text)
xml_text = ET.tostring(tree.getroot(),
encoding='unicode',
method='xml')

View File

@ -26,6 +26,9 @@ __all__ = ['log', 'log_calls', 'Singleton']
import logging
import functools
from gajim.plugins import plugins_i18n
from gajim.gtk.util import Builder
log = logging.getLogger('gajim.plugin_system')
'''
Logger for code related to plug-in system.
@ -130,3 +133,10 @@ class Singleton(type):
#'classname' : cls.__name__})
return cls.instance
def get_builder(file_name: str, widget: str = None) -> Builder:
return Builder(file_name,
widget,
domain=plugins_i18n.DOMAIN,
gettext_=plugins_i18n._)