i18n with gettext
This commit is contained in:
20
raven/i18n.py
Normal file
20
raven/i18n.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import gettext
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
locale = os.environ['LANG'] if os.environ['LANG'] else "en"
|
||||
|
||||
_ = False
|
||||
src_dir = Path(__file__).resolve().parent.parent
|
||||
try:
|
||||
translation = gettext.translation('messages', localedir=src_dir / 'locales', languages=[locale])
|
||||
if translation:
|
||||
translation.install()
|
||||
_ = translation.gettext
|
||||
ngettext = translation.ngettext
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
if not _:
|
||||
_ = gettext.gettext
|
||||
ngettext = gettext.ngettext
|
||||
print('No translation found')
|
||||
@@ -18,6 +18,8 @@ from tabs import Tabs
|
||||
from urlutils import url_is_file
|
||||
from functools import reduce
|
||||
|
||||
from raven.i18n import _
|
||||
|
||||
MAX_LINE_LENGTH = 4096
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
@@ -35,9 +37,7 @@ class MainWindow(QMainWindow):
|
||||
self.settings = SettingsStore.load()
|
||||
PluginRegistry.execute("set_settings", self.settings)
|
||||
|
||||
|
||||
|
||||
self.setWindowTitle(self.tr("RavenLog"))
|
||||
self.setWindowTitle(_("RavenLog"))
|
||||
self._restore_window()
|
||||
|
||||
self.setDockNestingEnabled(True)
|
||||
@@ -45,7 +45,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
self.tabs = Tabs(self.settings)
|
||||
|
||||
self._menu_recent_files = QMenu(self.tr("Open &Recent"), self)
|
||||
self._menu_recent_files = QMenu(_("Open &Recent"), self)
|
||||
self.setCentralWidget(self.tabs)
|
||||
self.status_bar = QStatusBar(self)
|
||||
self.setStatusBar(self.status_bar)
|
||||
@@ -64,10 +64,10 @@ class MainWindow(QMainWindow):
|
||||
menu_contributions = sort_menu_contributions(menu_contributions)
|
||||
|
||||
known_menus = [
|
||||
("file", self.tr("&File")),
|
||||
("settings", self.tr("&Settings")),
|
||||
("window", self.tr("&Window")),
|
||||
("help", self.tr("&Help"))
|
||||
("file", _("&File")),
|
||||
("settings", _("&Settings")),
|
||||
("window", _("&Window")),
|
||||
("help", _("&Help"))
|
||||
]
|
||||
|
||||
for (menu_id, menu_label) in known_menus:
|
||||
@@ -98,7 +98,7 @@ class MainWindow(QMainWindow):
|
||||
qmenu.addAction(action)
|
||||
|
||||
def _raction_to_qaction(self, raction: RAction, qmenu: QMenu) -> QAction:
|
||||
action = QAction(self.tr(raction.label), qmenu)
|
||||
action = QAction(_(raction.label), qmenu)
|
||||
if raction.icon_from_theme:
|
||||
action.setIcon(QIcon.fromTheme(raction.icon_from_theme))
|
||||
if raction.icon_file:
|
||||
@@ -114,7 +114,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def _action_highlighter(self):
|
||||
manage = RAction(
|
||||
"&Highlighter",
|
||||
_("&Highlighter"),
|
||||
action=lambda: HighlightingDialog(self.settings).exec(),
|
||||
shortcut='Ctrl+H'
|
||||
)
|
||||
@@ -122,7 +122,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def _action_highlight_search_terms(self):
|
||||
highlight_search_terms = RAction(
|
||||
"Highlight &Searches",
|
||||
_("Highlight &Searches"),
|
||||
action=lambda checked: self.settings.set_session("general", "highlight_search_term",
|
||||
str(checked)) or self.update()
|
||||
)
|
||||
@@ -131,7 +131,7 @@ class MainWindow(QMainWindow):
|
||||
return highlight_search_terms
|
||||
|
||||
def _action_new_tab(self):
|
||||
new_tab = RAction("Open Tab on Save As File")
|
||||
new_tab = RAction(_("Open Tab on Save As File"))
|
||||
new_tab.set_checkable(True)
|
||||
new_tab.set_checked(self.settings.session.getboolean("general", "open_tab_on_save_as_file"))
|
||||
new_tab.set_action(
|
||||
|
||||
@@ -61,7 +61,7 @@ class PluginRegistry():
|
||||
if len(sig.parameters) != len(args):
|
||||
raise RuntimeError("method %s.%s has wrong number of arguments. expected %s but was %s " % (
|
||||
plugin, function_name, len(args), len(sig.parameters)))
|
||||
print("calling %s with args %s" % (fun, args))
|
||||
# print("calling %s with args %s" % (fun, args))
|
||||
if len(args) == 0:
|
||||
return_value = fun()
|
||||
elif len(args) == 1:
|
||||
|
||||
@@ -9,23 +9,20 @@ from raven.pluginbase import PluginBase
|
||||
from raven.plugins.ravenlog.Tab import Tab
|
||||
from settings import Settings
|
||||
|
||||
from raven.i18n import _
|
||||
|
||||
class LogFilePlugin(PluginBase):
|
||||
def __init__(self):
|
||||
super(LogFilePlugin, self).__init__()
|
||||
self.settings = None
|
||||
self.tr = None
|
||||
|
||||
def set_settings(self, settings: Settings):
|
||||
self.settings = settings
|
||||
|
||||
def set_translator(self, tr: Callable[[str], str]):
|
||||
self.tr = tr
|
||||
|
||||
def create_tab(self, file: str) -> Optional[Tab]:
|
||||
if not os.path.isfile(file):
|
||||
message = QMessageBox(QMessageBox.Icon.Warning, "File not found",
|
||||
"'%s' is not a file or cannot be opened" % file)
|
||||
message = QMessageBox(QMessageBox.Icon.Warning, _("File not found"),
|
||||
_("'{0}' is not a file or cannot be opened").format(file))
|
||||
message.exec()
|
||||
return None
|
||||
|
||||
|
||||
@@ -7,26 +7,22 @@ from raven.pluginregistry import PluginRegistry
|
||||
from raven.plugins.domain.menucontribution import MenuContribution
|
||||
from raven.plugins.domain.raction import RAction
|
||||
from raven.plugins.notes.noteswidget import NotesWidget
|
||||
|
||||
from raven.i18n import _
|
||||
|
||||
class NotesPlugin(PluginBase):
|
||||
|
||||
def __init__(self):
|
||||
super(NotesPlugin, self).__init__()
|
||||
self.settings = None
|
||||
self.tr = None
|
||||
self.tab_counter = 0
|
||||
|
||||
def set_translator(self, tr: Callable[[str], str]):
|
||||
self.tr = tr
|
||||
|
||||
def get_menu_contributions(self) -> [MenuContribution]:
|
||||
return [
|
||||
MenuContribution("window", action=self._add_notes_tab_action(), action_id="add notes tab", after="<last>"),
|
||||
]
|
||||
|
||||
def _add_notes_tab_action(self) -> RAction:
|
||||
open_file = RAction(self.tr("Add &Notes"), self._add_notes_tab, shortcut='Ctrl+Shift+N',
|
||||
open_file = RAction(_("Add &Notes"), self._add_notes_tab, shortcut='Ctrl+Shift+N',
|
||||
icon_from_theme="filenew")
|
||||
return open_file
|
||||
|
||||
@@ -34,5 +30,5 @@ class NotesPlugin(PluginBase):
|
||||
self.tab_counter = self.tab_counter + 1
|
||||
notes = NotesWidget(
|
||||
"notes_tab_%d" % self.tab_counter,
|
||||
self.tr("Notes %d") % self.tab_counter)
|
||||
_("Notes {0}").format(self.tab_counter))
|
||||
PluginRegistry.execute_single("add_dock", Qt.DockWidgetArea.RightDockWidgetArea, notes)
|
||||
|
||||
@@ -12,26 +12,23 @@ from raven.plugins.domain.raction import RAction
|
||||
from raven.plugins.domain.rmenu import RMenu
|
||||
from settings import Settings
|
||||
|
||||
from raven.i18n import _
|
||||
|
||||
class OpenFilePlugin(PluginBase):
|
||||
def __init__(self):
|
||||
super(OpenFilePlugin, self).__init__()
|
||||
self.settings = None
|
||||
self.tr = None
|
||||
|
||||
def set_settings(self, settings: Settings):
|
||||
self.settings = settings
|
||||
|
||||
def set_translator(self, tr: Callable[[str], str]):
|
||||
self.tr = tr
|
||||
|
||||
def _action_open_file(self) -> RAction:
|
||||
open_file = RAction(self.tr("&Open..."), self._open_file_dialog, shortcut='Ctrl+O',
|
||||
open_file = RAction(_("&Open..."), self._open_file_dialog, shortcut='Ctrl+O',
|
||||
icon_from_theme="document-open")
|
||||
return open_file
|
||||
|
||||
def _sub_menu_recent_files(self) -> RMenu:
|
||||
self._menu_recent_files = RMenu(self.tr("Open &Recent"), icon_from_theme="document-open-recent")
|
||||
self._menu_recent_files = RMenu(_("Open &Recent"), icon_from_theme="document-open-recent")
|
||||
self._update_recent_files_menu()
|
||||
return self._menu_recent_files
|
||||
|
||||
@@ -47,7 +44,7 @@ class OpenFilePlugin(PluginBase):
|
||||
|
||||
dialog = QFileDialog()
|
||||
(selected_file, _filter) = dialog.getOpenFileName(
|
||||
caption=self.tr("Open File"),
|
||||
caption=_("Open File"),
|
||||
dir=directory
|
||||
)
|
||||
# directory=directory
|
||||
|
||||
@@ -11,17 +11,13 @@ from raven.pluginbase import PluginBase
|
||||
from raven.plugins.domain.menucontribution import MenuContribution
|
||||
from raven.plugins.domain.raction import RAction
|
||||
from raven.plugins.ravenlog.Tab import Tab
|
||||
|
||||
from raven.i18n import _
|
||||
|
||||
class RavenLogPlugin(PluginBase):
|
||||
def __init__(self):
|
||||
super(RavenLogPlugin, self).__init__()
|
||||
self.tr = None
|
||||
self.main_window = None
|
||||
|
||||
def set_translator(self, tr: Callable[[str], str]):
|
||||
self.tr = tr
|
||||
|
||||
def create_main_window(self):
|
||||
if not self.main_window:
|
||||
self.main_window = MainWindow()
|
||||
@@ -46,7 +42,7 @@ class RavenLogPlugin(PluginBase):
|
||||
|
||||
def _action_about(self) -> RAction:
|
||||
about_action = RAction(
|
||||
self.tr("&About"),
|
||||
_("&About"),
|
||||
action=lambda: AboutDialog().exec(),
|
||||
icon_file=constants.raven_icon
|
||||
)
|
||||
@@ -54,6 +50,6 @@ class RavenLogPlugin(PluginBase):
|
||||
|
||||
def _action_close(self) -> RAction:
|
||||
icon = "close" if sys.platform == 'win32' or sys.platform == 'cygwin' else "exit"
|
||||
close_action = RAction(self.tr("E&xit"), action=lambda: self.main_window.destruct(), shortcut='Ctrl+X',
|
||||
close_action = RAction(_("E&xit"), action=lambda: self.main_window.destruct(), shortcut='Ctrl+X',
|
||||
icon_from_theme=icon)
|
||||
return close_action
|
||||
|
||||
Reference in New Issue
Block a user