OpenShot Video Editor  2.0.0
test_translations.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 ##
3 #
4 # @file
5 # @brief This file verifies all translations are correctly formatted and have the correct # of string replacements
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 re
32 import fnmatch
33 import sys
34 from PyQt5.QtCore import QLocale, QLibraryInfo, QTranslator, QCoreApplication
35 
36 
37 # Get the absolute path of this project
38 language_path = os.path.dirname(os.path.abspath(__file__))
39 
40 # Get app instance
41 app = QCoreApplication(sys.argv)
42 
43 # Load POT template (all English strings)
44 POT_source = open(os.path.join(language_path, 'OpenShot', 'OpenShot.pot')).read()
45 all_strings = re.findall('^msgid \"(.*)\"', POT_source, re.MULTILINE)
46 
47 print("Testing {} strings in all translation files...\n".format(len(all_strings)))
48 
49 # Loop through folders/languages
50 for filename in fnmatch.filter(os.listdir(language_path), 'OpenShot.*.qm'):
51  lang_code = filename[:-3]
52  # Install language
53  translator = QTranslator(app)
54  app.installTranslator(translator)
55 
56  # Load translation
57  success = translator.load(lang_code, language_path)
58  print('%s\t%s' % (success, lang_code))
59 
60  # Loop through all test strings
61  for source_string in all_strings:
62  if "%s" in source_string or "%s(" in source_string or "%d" in source_string:
63  translated_string = app.translate("", source_string)
64  if source_string.count('%') != translated_string.count('%'):
65  print(' Invalid string replacement found: %s (source: %s)' % (translated_string, source_string))
66 
67  # Remove translator
68  app.removeTranslator(translator)