mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 01:14:15 +02:00
[improvement] Not breaking the application when a i18n (translation) key was not found
This commit is contained in:
67
bauh/view/util/translation.py
Normal file
67
bauh/view/util/translation.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import glob
|
||||
import locale
|
||||
from typing import Tuple
|
||||
|
||||
from bauh.view.util import resource
|
||||
|
||||
|
||||
class I18n(dict):
|
||||
|
||||
def __init__(self, current_locale: dict, default_locale: dict):
|
||||
super(I18n, self).__init__()
|
||||
self.current = current_locale
|
||||
self.default = default_locale
|
||||
|
||||
def __getitem__(self, item):
|
||||
try:
|
||||
return self.current.__getitem__(item)
|
||||
except KeyError:
|
||||
if self.default:
|
||||
return self.default.__getitem__(item)
|
||||
else:
|
||||
raise
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
res = self.current.get(args[0])
|
||||
|
||||
if res is None:
|
||||
if self.default:
|
||||
return self.default.get(*args, **kwargs)
|
||||
else:
|
||||
return self.current.get(*args, **kwargs)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def get_locale_keys(key: str = None, locale_dir: str = resource.get_path('locale')) -> Tuple[str, dict]:
|
||||
|
||||
locale_path = None
|
||||
|
||||
if key is None:
|
||||
current_locale = locale.getdefaultlocale()
|
||||
else:
|
||||
current_locale = [key.strip().lower()]
|
||||
|
||||
if current_locale:
|
||||
current_locale = current_locale[0]
|
||||
|
||||
for locale_file in glob.glob(locale_dir + '/*'):
|
||||
name = locale_file.split('/')[-1]
|
||||
|
||||
if current_locale == name or current_locale.startswith(name + '_'):
|
||||
locale_path = locale_file
|
||||
break
|
||||
|
||||
if not locale_path:
|
||||
return key, {}
|
||||
|
||||
with open(locale_path, 'r') as f:
|
||||
locale_keys = f.readlines()
|
||||
|
||||
locale_obj = {}
|
||||
for line in locale_keys:
|
||||
if line:
|
||||
keyval = line.strip().split('=')
|
||||
locale_obj[keyval[0].strip()] = keyval[1].strip()
|
||||
|
||||
return locale_path.split('/')[-1], locale_obj
|
||||
@@ -1,9 +1,6 @@
|
||||
import glob
|
||||
import locale
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Tuple
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication
|
||||
|
||||
@@ -12,40 +9,6 @@ from bauh.commons.system import run_cmd
|
||||
from bauh.view.util import resource
|
||||
|
||||
|
||||
def get_locale_keys(key: str = None, locale_dir: str = resource.get_path('locale')) -> Tuple[str, dict]:
|
||||
|
||||
locale_path = None
|
||||
|
||||
if key is None:
|
||||
current_locale = locale.getdefaultlocale()
|
||||
else:
|
||||
current_locale = [key.strip().lower()]
|
||||
|
||||
if current_locale:
|
||||
current_locale = current_locale[0]
|
||||
|
||||
for locale_file in glob.glob(locale_dir + '/*'):
|
||||
name = locale_file.split('/')[-1]
|
||||
|
||||
if current_locale == name or current_locale.startswith(name + '_'):
|
||||
locale_path = locale_file
|
||||
break
|
||||
|
||||
if not locale_path:
|
||||
locale_path = resource.get_path('locale/en')
|
||||
|
||||
with open(locale_path, 'r') as f:
|
||||
locale_keys = f.readlines()
|
||||
|
||||
locale_obj = {}
|
||||
for line in locale_keys:
|
||||
if line:
|
||||
keyval = line.strip().split('=')
|
||||
locale_obj[keyval[0].strip()] = keyval[1].strip()
|
||||
|
||||
return locale_path.split('/')[-1], locale_obj
|
||||
|
||||
|
||||
def notify_user(msg: str, icon_path: str = resource.get_path('img/logo.svg')):
|
||||
os.system("notify-send -a {} {} '{}'".format(__app_name__, "-i {}".format(icon_path) if icon_path else '', msg))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user