OpenShot Video Editor  2.0.0
language.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file loads the current language based on the computer's locale settings
5 # @author Noah Figg <eggmunkee@hotmail.com>
6 # @author Jonathan Thomas <jonathan@openshot.org>
7 #
8 # @section LICENSE
9 #
10 # Copyright (c) 2008-2018 OpenShot Studios, LLC
11 # (http://www.openshotstudios.com). This file is part of
12 # OpenShot Video Editor (http://www.openshot.org), an open-source project
13 # dedicated to delivering high quality video editing and animation solutions
14 # to the world.
15 #
16 # OpenShot Video Editor is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # OpenShot Video Editor is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
28 #
29 
30 import os
31 import locale
32 
33 from PyQt5.QtCore import QLocale, QLibraryInfo, QTranslator, QCoreApplication
34 
35 from classes.logger import log
36 from classes import info
37 from classes import settings
38 
39 
40 ##
41 # Find the current locale, and install the correct translators
43 
44  # Get app instance
45  app = QCoreApplication.instance()
46 
47  # Setup of our list of translators and paths
48  translator_types = (
49  {"type": 'QT',
50  "pattern": 'qt_%s', # Older versions of Qt use this file (built-in translations)
51  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
52  {"type": 'QT',
53  "pattern": 'qtbase_%s', # Newer versions of Qt use this file (built-in translations)
54  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
55  {"type": 'QT',
56  "pattern": 'qt_%s',
57  "path": os.path.join(info.PATH, 'locale', 'QT')}, # Optional path where we package QT translations
58  {"type": 'QT',
59  "pattern": 'qtbase_%s',
60  "path": os.path.join(info.PATH, 'locale', 'QT')}, # Optional path where we package QT translations
61  {"type": 'OpenShot',
62  "pattern": os.path.join('%s', 'LC_MESSAGES', 'OpenShot'), # Our custom translations
63  "path": os.path.join(info.PATH, 'locale')},
64  )
65 
66  # Determine the environment locale, or default to system locale name
67  locale_names = [os.environ.get('LANG', QLocale().system().name()),
68  os.environ.get('LOCALE', QLocale().system().name())
69  ]
70 
71  # Determine if the user has overwritten the language (in the preferences)
72  preference_lang = settings.get_settings().get('default-language')
73  if preference_lang != "Default":
74  # Append preference lang to top of list
75  locale_names.insert(0, preference_lang)
76 
77  # Output all system languages detected
78  log.info("Qt Detected Languages: {}".format(QLocale().system().uiLanguages()))
79  log.info("LANG Environment Variable: {}".format(os.environ.get('LANG', QLocale().system().name())))
80  log.info("LOCALE Environment Variable: {}".format(os.environ.get('LOCALE', QLocale().system().name())))
81 
82  # Default the locale to C, for number formatting
83  locale.setlocale(locale.LC_ALL, 'C')
84 
85  # Loop through environment variables
86  found_language = False
87  for locale_name in locale_names:
88 
89  # Don't try on default locale, since it fails to load what is the default language
90  if QLocale().system().name() in locale_name:
91  log.info("Skipping English language (no need for translation): {}".format(locale_name))
92  continue
93 
94  # Go through each translator and try to add for current locale
95  for type in translator_types:
96  trans = QTranslator(app)
97  if find_language_match(type["pattern"], type["path"], trans, locale_name):
98  # Install translation
99  app.installTranslator(trans)
100  found_language = True
101 
102  # Exit if found language
103  if found_language:
104  log.info("Exiting translation system (since we successfully loaded: {})".format(locale_name))
105  info.CURRENT_LANGUAGE = locale_name
106  break
107 
108 
109 ##
110 # Get the current locale name from the current system
112 
113  # Get app instance
114  app = QCoreApplication.instance()
115 
116  # Setup of our list of translators and paths
117  translator_types = (
118  {"type": 'QT',
119  "pattern": 'qt_%s',
120  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
121  {"type": 'OpenShot',
122  "pattern": os.path.join('%s', 'LC_MESSAGES', 'OpenShot'),
123  "path": os.path.join(info.PATH, 'locale')},
124  )
125 
126  # Determine the environment locale, or default to system locale name
127  locale_names = [os.environ.get('LANG', QLocale().system().name()),
128  os.environ.get('LOCALE', QLocale().system().name())
129  ]
130 
131  # Loop through environment variables
132  found_language = False
133  for locale_name in locale_names:
134 
135  # Don't try on default locale, since it fails to load what is the default language
136  if 'en_US' in locale_name:
137  continue
138 
139  # Go through each translator and try to add for current locale
140  for type in translator_types:
141  trans = QTranslator(app)
142  if find_language_match(type["pattern"], type["path"], trans, locale_name):
143  found_language = True
144 
145  # Exit if found language
146  if found_language:
147  return locale_name.replace(".UTF8", "").replace(".UTF-8", "")
148 
149  # default locale
150  return "en"
151 
152 # Try the full locale and base locale trying to find a valid path
153 # returns True when a match was found.
154 # pattern - a string expected to have one pipe to be filled by locale strings
155 # path - base path for file (pattern may contain more path)
156 #
157 ##
158 # Match all combinations of locale, language, and country
159 def find_language_match(pattern, path, translator, locale_name):
160 
161  success = False
162  locale_parts = locale_name.split('_')
163 
164  i = len(locale_parts)
165  while not success and i > 0:
166  formatted_name = pattern % "_".join(locale_parts[:i])
167  log.info('Attempting to load {} in \'{}\''.format(formatted_name, path))
168  success = translator.load(formatted_name, path)
169  if success:
170  log.info('Successfully loaded {} in \'{}\''.format(formatted_name, path))
171  i -= 1
172 
173  return success
174 
175 ##
176 # Get all language names and countries packaged with OpenShot
178 
179  # Get app instance
180  app = QCoreApplication.instance()
181 
182  # Loop through all supported language locale codes
183  all_languages = []
184  for locale_name in info.SUPPORTED_LANGUAGES:
185  try:
186  native_lang_name = QLocale(locale_name).nativeLanguageName().title()
187  country_name = QLocale(locale_name).nativeCountryName().title()
188  all_languages.append((locale_name, native_lang_name, country_name))
189  except:
190  # Ignore failed parsing of language
191  pass
192 
193  # Return list
194  return all_languages
def find_language_match
Match all combinations of locale, language, and country.
Definition: language.py:159
def init_language
Find the current locale, and install the correct translators.
Definition: language.py:42
def get_current_locale
Get the current locale name from the current system.
Definition: language.py:111
def get_all_languages
Get all language names and countries packaged with OpenShot.
Definition: language.py:177
def get_settings
Get the current QApplication's settings instance.
Definition: settings.py:44