1
# i18n reusable helper class
3
# Copyright (c) 2009 Canonical Ltd.
5
# Author: Amichai Rothman <amichai2@amichais.net>
7
# This program is free software; you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License as
9
# published by the Free Software Foundation; either version 2 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23
# This is a reusable helper component containing various i18n related
24
# utilities. It also helps narrow the gap between GTK and KDE frontends
25
# to a given application, by providing the translate_widget function to
26
# translate both language and GTK accelerators and styles to QT widgets.
27
# I hope to see this improve and grow into an abstract dual-gui helper
28
# framework, to make it much easier to port Ubuntu/GTK apps to Kubuntu
29
# as well as maintain them as they grow.
32
from gettext import gettext as _
35
# FIXME: what's the point of these functions????
36
from PyQt5.QtCore import *
37
from PyQt5.QtGui import *
38
from PyQt5.QtWidgets import *
41
regex = re.compile('<[^>]*>')
42
return regex.sub('', str)
44
def translate_string(text, nohtml=False, accelerators='replace'):
47
translated = _(str(text)) # text might be QString, unicode or str
49
translated = strip_html(translated)
50
if accelerators == 'replace':
51
translated = translated.replace('_', '&') # convert accelerators
52
elif accelerators == 'remove':
53
translated = translated.replace('_', '') # convert accelerators
56
def apply_styles(widget, str):
57
# this is obviously just a start :-)
58
if str.startswith("<b>"):
59
widget.setStyleSheet("QGroupBox {font-weight:bold;}");
61
def translate_widget(widget, recursive=True):
62
""" Translate a widget recursively, while converting GTK
63
accelerators and styles to appropriate QT equivalents.
64
The given widget is typically a dialog or main window,
65
translated after its components are initialized but
68
if isinstance(widget, QTabWidget):
69
for i in range(0, widget.count()):
70
widget.setTabText(i, translate_string(widget.tabText(i)))
71
elif isinstance(widget, QGroupBox):
72
apply_styles(widget, widget.title())
73
widget.setTitle(translate_string(widget.title(), nohtml=True))
74
elif isinstance(widget, QLineEdit):
75
widget.setText(widget.text()) # user-editable text: utf8 without translation
76
elif isinstance(widget, QWidget):
77
widget.setWindowTitle(translate_string(widget.windowTitle(), accelerators='remove'))
79
widget.setText(translate_string(widget.text()))
80
except AttributeError:
84
for child in widget.children():
85
translate_widget(child)