Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Add full i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Sep 25, 2018
1 parent f2387f2 commit 30bc5eb
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
13 changes: 7 additions & 6 deletions night_mode/color_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .internals import alert
from .gui import create_button, remove_layout, AddonDialog
from .languages import _


class ColorSwatch(QPushButton):
Expand All @@ -28,7 +29,7 @@ def __init__(self, parent, color=None, on_color_change=None, name='Color', verif
if color:
self.set_color(color)
else:
self.setText('(Not specified)')
self.setText(_('(Not specified)'))

self.clicked.connect(self.pick_color)

Expand All @@ -54,13 +55,13 @@ def pick_color(self, qt_color=None):
qt_color = QColorDialog.getColor(
qt_color,
parent=self,
title='Select %s' % self.name
title=_('Select %s') % _(self.name)
)

if qt_color.isValid():
color = qt_color.name()
if self.verify_colors and not self.parent.is_acceptable(color):
alert(f'This color ({color}) is already mapped. Please select a different one.')
alert(_('This color (%s) is already mapped. Please select a different one.') % color)
return self.pick_color(qt_color=qt_color)

self.set_color(color)
Expand Down Expand Up @@ -124,7 +125,7 @@ def __init__(self, parent, color_map, title='Customise colors swapping', on_upda
self.init_ui(title)

def init_ui(self, title):
self.setWindowTitle(title)
self.setWindowTitle(_(title))

btn_add_mapping = create_button('+ Add colors mapping', self.on_add)
btn_close = create_button('Close', self.close)
Expand All @@ -138,10 +139,10 @@ def init_ui(self, title):
body = QVBoxLayout()
body.setAlignment(Qt.AlignTop)

header = QLabel(
header = QLabel(_(
'Specify how particular colors on your cards '
'should be swapped when the night mode is on.'
)
))
header.setAlignment(Qt.AlignCenter)

mappings = QVBoxLayout()
Expand Down
4 changes: 3 additions & 1 deletion night_mode/gui.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from PyQt5.QtWidgets import QPushButton, QDialog

from .languages import _


class AddonDialog(QDialog):
def __init__(self, *args, **kwargs):
QDialog.__init__(*args, **kwargs)


def create_button(name, callback=None):
button = QPushButton(name)
button = QPushButton(_(name))
if callback:
button.clicked.connect(callback)
return button
Expand Down
17 changes: 17 additions & 0 deletions night_mode/languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import gettext
from os import path

from anki.lang import getLang, _ as fallback_translation

lang = getLang()
this_dir = path.dirname(path.abspath(__file__))
locale_dir = path.join(this_dir, 'locale')
trans = gettext.translation('Anki-Night-Mode', locale_dir, languages=[lang], fallback=True)


def _(text):
try:
return trans.gettext(text)
except Exception as e:
print(e)
return fallback_translation(text)
Binary file not shown.
Binary file not shown.
8 changes: 6 additions & 2 deletions night_mode/menu.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QAction, QMenu

from anki.lang import _
from aqt import mw

from .languages import _


def get_or_create_menu(attribute_name, label):

Expand All @@ -15,8 +16,11 @@ def get_or_create_menu(attribute_name, label):
mw.form.menuTools.menuAction(),
menu
)
else:
menu = getattr(mw, attribute_name)
menu.setTitle(_(label))

return getattr(mw, attribute_name)
return menu


class Menu:
Expand Down
2 changes: 1 addition & 1 deletion night_mode/night_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from .styles import Style, MessageBoxStyle

__addon_name__ = 'Night Mode'
__version__ = '2.1.9'
__version__ = '2.2.0alpha'
__anki_version__ = '2.1'


Expand Down
11 changes: 6 additions & 5 deletions night_mode/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from PyQt5.QtWidgets import QLabel, QHBoxLayout, QVBoxLayout, QCheckBox

from .gui import create_button, AddonDialog
from .languages import _


class StylerCheckButton(QCheckBox):

def __init__(self, parent, styler):
QCheckBox.__init__(self, styler.friendly_name, parent)
QCheckBox.__init__(self, _(styler.friendly_name), parent)
self.styler = styler
if styler.is_active:
self.toggle()
Expand All @@ -20,7 +21,7 @@ def switch_state(self, state):

class StylersSelectorWindow(AddonDialog):

def __init__(self, parent, disabled_stylers: set, all_stylers, title='Choose what to style', on_update=None):
def __init__(self, parent, disabled_stylers: set, all_stylers, title=_('Choose what to style'), on_update=None):
super().__init__(self, parent, Qt.Window)
self.on_update = on_update
self.disabled_stylers = disabled_stylers
Expand All @@ -43,12 +44,12 @@ def init_ui(self, title):
body = QVBoxLayout()
body.setAlignment(Qt.AlignTop)

header = QLabel(
header = QLabel(_(
'Select which parts of Anki should be displayed '
'in eye-friendly, dark colors.\n\n'
'To disable all dialog windows, '
'use the "Enable in dialogs" switch which is available in menu.'
)
))
header.setAlignment(Qt.AlignCenter)

stylers = QVBoxLayout()
Expand All @@ -62,7 +63,7 @@ def init_ui(self, title):
self.stylers_layout = stylers

checked_boxes = sum(1 for checkbox in self.stylers_checkboxes if checkbox.isChecked())
check_all = QCheckBox('Check/uncheck all', self)
check_all = QCheckBox(_('Check/uncheck all'), self)
check_all.setChecked(checked_boxes > len(self.stylers_checkboxes) / 2)
check_all.stateChanged.connect(self.check_uncheck_all)

Expand Down

0 comments on commit 30bc5eb

Please sign in to comment.