Coverage for L10n.py: 0%

96 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-15 19:33 +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 

28 

29def get_system_language(): 

30 system = platform.system() 

31 if system == "Windows": 

32 return locale.getdefaultlocale()[0] 

33 elif system == "Linux": 

34 process = subprocess.Popen(["locale", "-b"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

35 output, _ = process.communicate() 

36 return output.decode().strip() 

37 elif system == "Darwin": 

38 process = subprocess.Popen( 

39 ["defaults", "read", "-g", "AppleLanguages"], stdout=subprocess.PIPE, stderr=subprocess.PIPE 

40 ) 

41 output, _ = process.communicate() 

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

43 return output 

44 else: 

45 return None 

46 

47 

48def pass_through(to_translate): 

49 return to_translate 

50 

51 

52def set_translation(to_lang): 

53 try: 

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

55 trans.install() 

56 translation = QTranslator() 

57 translation.load(to_lang, "locale") 

58 except IOError: 

59 translation = None 

60 return translation 

61 

62 

63def get_translation(): 

64 try: 

65 return _ 

66 except NameError: 

67 return pass_through 

68 

69 

70def init_translation(): 

71 import Configuration 

72 

73 conf = Configuration.Config() 

74 

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

76 try: 

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

78 except Exception: 

79 lang = None 

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

81 return pass_through 

82 else: 

83 return set_translation(lang) 

84 elif conf.general["ui_language"] == "en": 

85 return pass_through 

86 else: 

87 return set_translation(conf.general["ui_language"]) 

88 

89 

90def get_installed_translations(): 

91 la_list = [] 

92 la_co_list = [] 

93 

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

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

96 if "_" in la_co: 

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

98 la_list.append(la) 

99 else: 

100 la_list.append(la_co) 

101 la_co_list.append(la_co) 

102 

103 la_set = set(la_list) 

104 la_list = list(la_set) 

105 

106 la_dict = {} 

107 la_co_dict = {} 

108 try: 

109 from icu import Locale 

110 

111 for code in la_list: 

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

113 for code in la_co_list: 

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

115 except Exception: 

116 for code in la_list: 

117 la_dict[code] = code 

118 for code in la_co_list: 

119 la_co_dict[code] = code 

120 

121 return la_dict, la_co_dict 

122 

123 

124def set_locale_translation(): 

125 path = Path(GRAPHICS_PATH) 

126 transformed_path = path.parent 

127 locale_path = Path(transformed_path, "locale") 

128 path_string = str(locale_path) 

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

130 

131 gettext.bindtextdomain("fpdb", path_string) 

132 gettext.textdomain("fpdb") 

133 

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

135 root = tree.getroot() 

136 general_element = root.find("general") 

137 ui_language = general_element.attrib.get("ui_language") 

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

139 

140 try: 

141 fr_translation = gettext.translation("fpdb", path_string, languages=[ui_language]) 

142 fr_translation.install() 

143 except FileNotFoundError: 

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