1#!/usr/local/bin/python3.8
2"""
3 @file
4 @brief This file verifies all translations are correctly formatted and have the correct # of string replacements
5 @author Jonathan Thomas <jonathan@openshot.org>
6
7 @section LICENSE
8
9 Copyright (c) 2008-2018 OpenShot Studios, LLC
10 (http://www.openshotstudios.com). This file is part of
11 OpenShot Video Editor (http://www.openshot.org), an open-source project
12 dedicated to delivering high quality video editing and animation solutions
13 to the world.
14
15 OpenShot Video Editor is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19
20 OpenShot Video Editor is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with OpenShot Library.  If not, see <http://www.gnu.org/licenses/>.
27 """
28
29import os
30import re
31import fnmatch
32import sys
33from PyQt5.QtCore import QLocale, QLibraryInfo, QTranslator, QCoreApplication
34
35
36# Get the absolute path of this project
37language_path = os.path.dirname(os.path.abspath(__file__))
38
39# Color for errors
40red='\033[31m'
41endc='\033[0m'
42
43found_errors = False
44
45# Get app instance
46app = QCoreApplication(sys.argv)
47
48# Load POT template (all English strings)
49all_templates = ['OpenShot.pot', 'OpenShot_transitions.pot', 'OpenShot_blender.pot']
50for template_name in all_templates:
51    POT_source = open(os.path.join(language_path, 'OpenShot', template_name)).read()
52    all_strings = re.findall('^msgid \"(.*)\"', POT_source, re.MULTILINE)
53
54    print("Testing {} strings in {}...".format(len(all_strings), template_name))
55
56    # Loop through folders/languages
57    for filename in fnmatch.filter(os.listdir(language_path), 'OpenShot*.qm'):
58        lang_code = filename[:-3]
59        # Install language
60        translator = QTranslator(app)
61        app.installTranslator(translator)
62
63        # Load translation
64        success = translator.load(lang_code, language_path)
65        if not success:
66            print(red, '%s-%s' % (success, lang_code), endc)
67
68        # Loop through all test strings
69        for source_string in all_strings:
70            if "%s" in source_string or "%s(" in source_string or "%d" in source_string:
71                translated_string = app.translate("", source_string)
72                if source_string.count('%') != translated_string.count('%'):
73                    found_errors = True
74                    print(red, '\tInvalid string replacement found: "%s" vs "%s" [%s]' %
75                          (translated_string, source_string, lang_code), endc)
76
77        # Remove translator
78        app.removeTranslator(translator)
79
80if found_errors:
81    raise(Exception("Errors detected during translation testing! See above."))
82