~ubuntu-core-dev/software-properties/main

« back to all changes in this revision

Viewing changes to softwareproperties/kde/I18nHelper.py

  • Committer: Simon Quigley
  • Date: 2018-07-14 10:11:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1049.
  • Revision ID: tsimonq2@ubuntu.com-20180714101132-jpm43js9q13vplzm
Use sed to make everything say Qt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#  i18n reusable helper class
2
 
#
3
 
#  Copyright (c) 2009 Canonical Ltd.
4
 
#
5
 
#  Author: Amichai Rothman <amichai2@amichais.net>
6
 
#
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.
11
 
#
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.
16
 
#
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
20
 
#  USA
21
 
 
22
 
#
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.
30
 
#
31
 
 
32
 
from gettext import gettext as _
33
 
import re
34
 
 
35
 
# FIXME: what's the point of these functions????
36
 
from PyQt5.QtCore import *
37
 
from PyQt5.QtGui import *
38
 
from PyQt5.QtWidgets import *
39
 
 
40
 
def strip_html(str):
41
 
  regex = re.compile('<[^>]*>')
42
 
  return regex.sub('', str)
43
 
 
44
 
def translate_string(text, nohtml=False, accelerators='replace'):
45
 
  if not text:
46
 
    return text
47
 
  translated = _(str(text)) # text might be QString, unicode or str
48
 
  if nohtml:
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
54
 
  return translated
55
 
 
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;}");
60
 
 
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
66
 
      before being shown.
67
 
      """
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'))
78
 
    try:
79
 
      widget.setText(translate_string(widget.text()))
80
 
    except AttributeError:
81
 
      pass
82
 
 
83
 
  if recursive:
84
 
    for child in widget.children():
85
 
      translate_widget(child)