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-2016 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 'en_US' 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  break
106 
107 
108 ##
109 # Get the current locale name from the current system
111 
112  # Get app instance
113  app = QCoreApplication.instance()
114 
115  # Setup of our list of translators and paths
116  translator_types = (
117  {"type": 'QT',
118  "pattern": 'qt_%s',
119  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
120  {"type": 'OpenShot',
121  "pattern": os.path.join('%s', 'LC_MESSAGES', 'OpenShot'),
122  "path": os.path.join(info.PATH, 'locale')},
123  )
124 
125  # Determine the environment locale, or default to system locale name
126  locale_names = [os.environ.get('LANG', QLocale().system().name()),
127  os.environ.get('LOCALE', QLocale().system().name())
128  ]
129 
130  # Loop through environment variables
131  found_language = False
132  for locale_name in locale_names:
133 
134  # Don't try on default locale, since it fails to load what is the default language
135  if 'en_US' in locale_name:
136  continue
137 
138  # Go through each translator and try to add for current locale
139  for type in translator_types:
140  trans = QTranslator(app)
141  if find_language_match(type["pattern"], type["path"], trans, locale_name):
142  found_language = True
143 
144  # Exit if found language
145  if found_language:
146  return locale_name.replace(".UTF8", "").replace(".UTF-8", "")
147 
148  # default locale
149  return "en"
150 
151 # Try the full locale and base locale trying to find a valid path
152 # returns True when a match was found.
153 # pattern - a string expected to have one pipe to be filled by locale strings
154 # path - base path for file (pattern may contain more path)
155 #
156 ##
157 # Match all combinations of locale, language, and country
158 def find_language_match(pattern, path, translator, locale_name):
159 
160  success = False
161  locale_parts = locale_name.split('_')
162 
163  i = len(locale_parts)
164  while not success and i > 0:
165  formatted_name = pattern % "_".join(locale_parts[:i])
166  log.info('Attempting to load {} in \'{}\''.format(formatted_name, path))
167  success = translator.load(formatted_name, path)
168  if success:
169  log.info('Successfully loaded {} in \'{}\''.format(formatted_name, path))
170  i -= 1
171 
172  return success
173 
174 ##
175 # Get all language names and countries packaged with OpenShot
177 
178  # Get app instance
179  app = QCoreApplication.instance()
180 
181  # Loop through all supported language locale codes
182  all_languages = []
183  for locale_name in info.SUPPORTED_LANGUAGES:
184  native_lang_name = QLocale(locale_name).nativeLanguageName().title()
185  country_name = QLocale(locale_name).nativeCountryName().title()
186  all_languages.append((locale_name, native_lang_name, country_name))
187 
188  # Return list
189  return all_languages
def get_all_languages()
Get all language names and countries packaged with OpenShot.
Definition: language.py:176
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:110
def get_settings()
Get the current QApplication&#39;s settings instance.
Definition: settings.py:43
def find_language_match(pattern, path, translator, locale_name)
Match all combinations of locale, language, and country.
Definition: language.py:158