Skip to content

Commit

Permalink
[Qt] Set the layout direction based on user-configured language
Browse files Browse the repository at this point in the history
For Arabic and Farsi (and Hebrew and others), the Qt layout direction
was wrong.  Layout direction needs to be set to right-to-left for these
languages, resulting in basically a flipped window (which is the
standard for desktop apps using these languages).

This fix makes the Qt app render correctly in these languages.

Special thanks to Akad for calling this to my attention.
  • Loading branch information
cculianu committed Sep 4, 2020
1 parent c44f0bd commit fa8c15e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 21 additions & 2 deletions gui/qt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

from electroncash.i18n import _, set_language
from electroncash.i18n import _
from electroncash import i18n
from electroncash.plugins import run_hook
from electroncash import WalletStorage
from electroncash.util import (UserCancelled, PrintError, print_error,
Expand Down Expand Up @@ -86,7 +87,7 @@ def __init__(self, config, daemon, plugins):
super(__class__, self).__init__() # QObject init
assert __class__.instance is None, "ElectrumGui is a singleton, yet an instance appears to already exist! FIXME!"
__class__.instance = self
set_language(config.get('language'))
i18n.set_language(config.get('language'))

self.config = config
self.daemon = daemon
Expand Down Expand Up @@ -264,6 +265,24 @@ def undo_hack():

callables.append(undo_hack)

def setup_layout_direction():
"""Sets the app layout direction depending on language. To be called
after self.app is created successfully. Note this *MUST* be called
after set_language has been called."""
assert i18n.set_language_called > 0
lc = i18n.language.info().get('language')
lc = '' if not isinstance(lc, str) else lc
lc = lc.split('_')[0]
layout_direction = Qt.LeftToRight
blurb = "left-to-right"
if lc in {'ar', 'fa', 'he', 'ps', 'ug', 'ur'}: # Right-to-left languages
layout_direction = Qt.RightToLeft
blurb = "right-to-left"
self.print_error("Setting layout direction:", blurb)
self.app.setLayoutDirection(layout_direction)
# callable will be called after self.app is set-up successfully
callables.append(setup_layout_direction)

return ret

def _exit_if_required_pyqt_is_missing(self):
Expand Down
5 changes: 4 additions & 1 deletion lib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

LOCALE_DIR = os.path.join(os.path.dirname(__file__), 'locale')
language = gettext.translation('electron-cash', LOCALE_DIR, fallback=True)
set_language_called = 0

def _(x):
global language
Expand Down Expand Up @@ -60,7 +61,7 @@ def npgettext(context: str, singular: str, plural: str, n: int) -> str:
return message if '\x04' in translated else translated

def set_language(x):
global language
global language, set_language_called

if not x:
# User hasn't selected a language so we default to the system language
Expand All @@ -70,6 +71,8 @@ def set_language(x):
# match.
x = match_language(x) or x

set_language_called += 1 # Tally the number of times this has been called

if x:
language = gettext.translation('electron-cash', LOCALE_DIR, fallback=True, languages=[x])
return x # indicate to caller what code was actually used, if anything.
Expand Down

0 comments on commit fa8c15e

Please sign in to comment.