Coverage for L10n.py: 0%

96 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-28 16:41 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3 

4#Copyright 2010-2011 Steffen Schaumburg 

5#This program is free software: you can redistribute it and/or modify 

6#it under the terms of the GNU Affero General Public License as published by 

7#the Free Software Foundation, version 3 of the License. 

8# 

9#This program is distributed in the hope that it will be useful, 

10#but WITHOUT ANY WARRANTY; without even the implied warranty of 

11#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12#GNU General Public License for more details. 

13# 

14#You should have received a copy of the GNU Affero General Public License 

15#along with this program. If not, see <http://www.gnu.org/licenses/>. 

16#In the "official" distribution you can find the license in agpl-3.0.txt. 

17 

18#You may find http://boodebr.org/main/python/all-about-python-and-unicode helpful 

19import gettext 

20import platform 

21import subprocess 

22import locale 

23from pathlib import Path 

24from Configuration import GRAPHICS_PATH, CONFIG_PATH 

25import xml.etree.ElementTree as ET 

26from PyQt5.QtCore import QTranslator 

27 

28def get_system_language(): 

29 system = platform.system() 

30 if system == 'Windows': 

31 return locale.getdefaultlocale()[0] 

32 elif system == 'Linux': 

33 process = subprocess.Popen(['locale', '-b'], 

34 stdout=subprocess.PIPE, 

35 stderr=subprocess.PIPE) 

36 output, _ = process.communicate() 

37 return output.decode().strip() 

38 elif system == 'Darwin': 

39 process = subprocess.Popen(['defaults', 'read', '-g', 'AppleLanguages'], 

40 stdout=subprocess.PIPE, 

41 stderr=subprocess.PIPE) 

42 output, _ = process.communicate() 

43 output = output.decode().strip().replace('\n', '').replace('"', '') 

44 return output 

45 else: 

46 return None 

47 

48def pass_through(to_translate): 

49 return to_translate 

50 

51def set_translation(to_lang): 

52 try: 

53 trans = gettext.translation("fpdb", localedir="locale", languages=[to_lang]) 

54 trans.install() 

55 translation = QTranslator() 

56 translation.load(to_lang, "locale") 

57 except IOError: 

58 translation = None 

59 return translation 

60 

61def get_translation(): 

62 try: 

63 return _ 

64 except NameError: 

65 return pass_through 

66 

67def init_translation(): 

68 import Configuration 

69 conf = Configuration.Config() 

70 

71 if conf.general['ui_language'] in ("system", ""): 

72 try: 

73 (lang, charset) = locale.getdefaultlocale() 

74 except Exception: 

75 lang = None 

76 if lang is None or lang[:2] == "en": 

77 return pass_through 

78 else: 

79 return set_translation(lang) 

80 elif conf.general['ui_language'] == "en": 

81 return pass_through 

82 else: 

83 return set_translation(conf.general['ui_language']) 

84 

85def get_installed_translations(): 

86 la_list = [] 

87 la_co_list = [] 

88 

89 for (ident, la_co) in locale.windows_locale.items(): 

90 if gettext.find("fpdb", localedir="locale", languages=[la_co]): 

91 if "_" in la_co: 

92 la, co = la_co.split("_", 1) 

93 la_list.append(la) 

94 else: 

95 la_list.append(la_co) 

96 la_co_list.append(la_co) 

97 

98 la_set = set(la_list) 

99 la_list = list(la_set) 

100 

101 la_dict = {} 

102 la_co_dict = {} 

103 try: 

104 from icu import Locale 

105 for code in la_list: 

106 la_dict[code] = Locale.getDisplayName(Locale(code)) 

107 for code in la_co_list: 

108 la_co_dict[code] = Locale.getDisplayName(Locale(code)) 

109 except Exception: 

110 for code in la_list: 

111 la_dict[code] = code 

112 for code in la_co_list: 

113 la_co_dict[code] = code 

114 

115 return la_dict, la_co_dict 

116 

117def set_locale_translation(): 

118 path = Path(GRAPHICS_PATH) 

119 transformed_path = path.parent 

120 locale_path = Path(transformed_path, "locale") 

121 path_string = str(locale_path) 

122 print(f"Locale path: {path_string}") 

123 

124 gettext.bindtextdomain('fpdb', path_string) 

125 gettext.textdomain('fpdb') 

126 

127 tree = ET.parse(f"{CONFIG_PATH}/HUD_config.xml") 

128 root = tree.getroot() 

129 general_element = root.find('general') 

130 ui_language = general_element.attrib.get('ui_language') 

131 print(f"UI Language: {ui_language}") 

132 

133 try: 

134 fr_translation = gettext.translation('fpdb', path_string, languages=[ui_language]) 

135 fr_translation.install() 

136 except FileNotFoundError: 

137 print(f"No translation file found for domain: 'fpdb' in {path_string} for language {ui_language}") 

138