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 try:
41  from language import openshot_lang
42  language_path=":/locale/"
43  log.debug("Using compiled translation resources")
44 except ImportError:
45  language_path=os.path.join(info.PATH, 'language')
46  log.debug("Loading translations from: {}".format(language_path))
47 
48 ##
49 # Find the current locale, and install the correct translators
51 
52  # Get app instance
53  app = QCoreApplication.instance()
54 
55  # Setup of our list of translators and paths
56  translator_types = (
57  {"type": 'QT',
58  "prefix": 'qt_', # Older versions of Qt use this file (built-in translations)
59  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
60  {"type": 'QT',
61  "prefix": 'qtbase_', # Newer versions of Qt use this file (built-in translations)
62  "path": QLibraryInfo.location(QLibraryInfo.TranslationsPath)},
63  {"type": 'QT',
64  "prefix": 'qt_',
65  "path": os.path.join(info.PATH, 'language')}, # Optional path where we package QT translations
66  {"type": 'QT',
67  "prefix": 'qtbase_',
68  "path": os.path.join(info.PATH, 'language')}, # Optional path where we package QT translations
69  {"type": 'OpenShot',
70  "prefix": 'OpenShot.', # Our custom translations
71  "path": language_path},
72  )
73 
74  # Determine the environment locale, or default to system locale name
75  locale_names = [os.environ.get('LANG', QLocale().system().name()),
76  os.environ.get('LOCALE', QLocale().system().name())
77  ]
78 
79  # Get the user's configured language preference
80  preference_lang = settings.get_settings().get('default-language')
81 
82  # Output all languages detected from various sources
83  log.info("Qt Detected Languages: {}".format(QLocale().system().uiLanguages()))
84  log.info("LANG Environment Variable: {}".format(os.environ.get('LANG', "")))
85  log.info("LOCALE Environment Variable: {}".format(os.environ.get('LOCALE', "")))
86  log.info("OpenShot Preference Language: {}".format(preference_lang))
87 
88  # Check if the language preference is something other than "Default"
89  if preference_lang == "en_US":
90  # Override language list with en_US, don't add to it
91  locale_names = [ "en_US" ]
92  elif preference_lang != "Default":
93  # Prepend preference setting to list
94  locale_names.insert(0, preference_lang)
95 
96  # If the user has used the --lang command line arg, override with that
97  # (We've already checked that it's in SUPPORTED_LANGUAGES)
98  if info.CMDLINE_LANGUAGE:
99  locale_names = [ info.CMDLINE_LANGUAGE ]
100  log.info("Language overridden on command line, using: {}".format(info.CMDLINE_LANGUAGE))
101 
102  # Default the locale to C, for number formatting
103  locale.setlocale(locale.LC_ALL, 'C')
104 
105  # Loop through environment variables
106  found_language = False
107  for locale_name in locale_names:
108 
109  # Go through each translator and try to add for current locale
110  for type in translator_types:
111  trans = QTranslator(app)
112  if find_language_match(type["prefix"], type["path"], trans, locale_name):
113  # Install translation
114  app.installTranslator(trans)
115  found_language = True
116 
117  # Exit if found language for type: "OpenShot"
118  if found_language:
119  log.debug("Exiting translation system (since we successfully loaded: {})".format(locale_name))
120  info.CURRENT_LANGUAGE = locale_name
121  break
122 
123 
124 # Try the full locale and base locale trying to find a valid path
125 # returns True when a match was found.
126 # pattern - a string expected to have one pipe to be filled by locale strings
127 # path - base path for file (pattern may contain more path)
128 #
129 ##
130 # Match all combinations of locale, language, and country
131 def find_language_match(prefix, path, translator, locale_name):
132 
133  filename = prefix + locale_name
134  log.debug('Attempting to load {} in \'{}\''.format(filename,path))
135  success = translator.load(filename, path)
136  if success:
137  log.debug('Successfully loaded {} in \'{}\''.format(filename, path))
138  return success
139 
140 ##
141 # Get all language names and countries packaged with OpenShot
143 
144  # Loop through all supported language locale codes
145  all_languages = []
146  for locale_name in info.SUPPORTED_LANGUAGES:
147  try:
148  native_lang_name = QLocale(locale_name).nativeLanguageName().title()
149  country_name = QLocale(locale_name).nativeCountryName().title()
150  all_languages.append((locale_name, native_lang_name, country_name))
151  except:
152  # Ignore failed parsing of language
153  pass
154 
155  # Return list
156  return all_languages
157 
159  return info.CURRENT_LANGUAGE
160 
def find_language_match
Match all combinations of locale, language, and country.
Definition: language.py:131
def init_language
Find the current locale, and install the correct translators.
Definition: language.py:50
def get_current_locale
Definition: language.py:158
def get_all_languages
Get all language names and countries packaged with OpenShot.
Definition: language.py:142
def get_settings
Get the current QApplication's settings instance.
Definition: settings.py:44