From 01ff6ab55d4c87ad0622300c3c0146ce23d60248 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 21 Jul 2020 11:52:25 +1000 Subject: [PATCH] apply rtl dir to webviews https://forums.ankiweb.net/t/gui-problems-with-right-to-left-languages/1205 --- pylib/anki/lang.py | 4 ++++ qt/aqt/__init__.py | 2 +- qt/aqt/webview.py | 13 +++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pylib/anki/lang.py b/pylib/anki/lang.py index 50bf528a7..98ca438f5 100644 --- a/pylib/anki/lang.py +++ b/pylib/anki/lang.py @@ -185,6 +185,10 @@ def set_lang(lang: str, locale_dir: str) -> None: locale_folder = locale_dir +def is_rtl(lang: str) -> bool: + return lang in ("he", "ar", "fa") + + # strip off unicode isolation markers from a translated string # for testing purposes def without_unicode_isolation(s: str) -> str: diff --git a/qt/aqt/__init__.py b/qt/aqt/__init__.py index 96615ee00..9bc85d471 100644 --- a/qt/aqt/__init__.py +++ b/qt/aqt/__init__.py @@ -209,7 +209,7 @@ def setupLangAndBackend( anki.lang.set_lang(lang, ldir) # switch direction for RTL languages - if lang in ("he", "ar", "fa"): + if anki.lang.is_rtl(lang): app.setLayoutDirection(Qt.RightToLeft) else: app.setLayoutDirection(Qt.LeftToRight) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 98d802e05..56e307d1c 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -1,6 +1,7 @@ # Copyright: Ankitects Pty Ltd and contributors # -*- coding: utf-8 -*- # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + import dataclasses import json import math @@ -8,7 +9,8 @@ import re import sys from typing import Any, Callable, List, Optional, Sequence, Tuple -from anki.lang import _ +import anki +from anki.lang import _, is_rtl from anki.utils import isLin, isMac, isWin from aqt import gui_hooks from aqt.qt import * @@ -420,13 +422,19 @@ div[contenteditable="true"]:focus { body_class = theme_manager.body_class() + if is_rtl(anki.lang.currentLang): + lang_dir = "rtl" + else: + lang_dir = "ltr" + + html = """ {} @@ -438,6 +446,7 @@ body {{ zoom: {}; background: {}; {} }} self.title, self.zoomFactor(), self._getWindowColor().name(), + lang_dir, fontspec, widgetspec, head,