2022-08-26 16:17:56 +00:00
|
|
|
#!/usr/bin/env python3
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
#
|
2024-09-30 16:06:20 +00:00
|
|
|
# Copyright The Mumble Developers. All rights reserved.
|
2016-05-08 17:57:21 +00:00
|
|
|
# Use of this source code is governed by a BSD-style license
|
|
|
|
# that can be found in the LICENSE file at the root of the
|
|
|
|
# Mumble source tree or at <https://www.mumble.info/LICENSE>.
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
|
|
|
|
from __future__ import (unicode_literals, print_function, division)
|
|
|
|
|
2022-08-26 16:17:56 +00:00
|
|
|
import argparse
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import sys
|
2021-05-11 18:25:40 +00:00
|
|
|
from pathlib import Path
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
allowed_components = ('qt', 'qtbase')
|
|
|
|
local_qt_translations = []
|
2020-01-27 18:31:24 +00:00
|
|
|
override_qt = []
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
|
2020-01-27 18:31:24 +00:00
|
|
|
def parseTranslationsConfig(configFile):
|
2021-05-11 16:34:38 +00:00
|
|
|
configHandle = open(configFile, "r")
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
for currentLine in configHandle.readlines():
|
|
|
|
currentLine = currentLine.strip()
|
|
|
|
# Skip comments and empty lines
|
|
|
|
if currentLine.startswith("#") or not currentLine:
|
|
|
|
continue
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
# A config entry is supposed to be in the format <operator> <fileName>
|
|
|
|
splitParts = currentLine.split(" ", 1)
|
|
|
|
if len(splitParts) != 2:
|
|
|
|
raise RuntimeError("Invalid line in translation config file: %s" % currentLine)
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
operator = splitParts[0].lower().strip()
|
|
|
|
translationFileName = splitParts[1].strip()
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
if not translationFileName:
|
|
|
|
raise RuntimeError("Empty filename in translation config: %s" % currentLine)
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
if not translationFileName.endswith(".ts"):
|
|
|
|
raise RuntimeError("Expected translation file to have a '*.ts' name but got %s" % translationFileName)
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
# Replace the trailing .ts with .qm as this is what lrelease will turn it into
|
|
|
|
translationFileName = translationFileName[:-3] + ".qm"
|
2022-08-26 16:17:56 +00:00
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
local_qt_translations.append(translationFileName)
|
2020-01-27 18:31:24 +00:00
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
if operator == "fallback":
|
|
|
|
# fallback files are the default, so no special action has to be taken
|
|
|
|
pass
|
|
|
|
# be programmer friendly and allow "override" as well
|
|
|
|
elif operator == "overwrite" or operator == "override":
|
|
|
|
override_qt.append(translationFileName)
|
2020-01-27 18:31:24 +00:00
|
|
|
|
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
def getComponentName(fileName):
|
|
|
|
# Remove file extension
|
|
|
|
fileName = os.path.splitext(fileName)[0]
|
|
|
|
|
|
|
|
lastUnderscoreIdx = fileName.rfind('_')
|
|
|
|
if lastUnderscoreIdx == -1:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
component = fileName[:lastUnderscoreIdx]
|
|
|
|
lang = fileName[lastUnderscoreIdx+1:]
|
|
|
|
# Handle en_US-style locale names
|
|
|
|
if lang.upper() == lang:
|
|
|
|
lastUnderscoreIdx = component.rfind('_')
|
|
|
|
component = fileName[:lastUnderscoreIdx]
|
|
|
|
lang = fileName[lastUnderscoreIdx+1:]
|
2022-08-26 16:17:56 +00:00
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
return component
|
|
|
|
|
|
|
|
|
|
|
|
def dirToQrc(outFile, directoryPath, processedComponents, localTranslationDir = False):
|
|
|
|
absPath = os.path.abspath(directoryPath)
|
|
|
|
|
|
|
|
fileNames = os.listdir(absPath)
|
|
|
|
|
|
|
|
return filesToQrc(outFile, processedComponents, fileNames, absPath, localTranslationDir)
|
|
|
|
|
|
|
|
|
|
|
|
def filesToQrc(outFile, processedComponents, fileNames, directoryPath, localTranslationDir = False):
|
|
|
|
for currentFileName in fileNames:
|
2021-05-11 16:34:38 +00:00
|
|
|
isOverride = False
|
2021-05-11 18:25:40 +00:00
|
|
|
|
|
|
|
if currentFileName in override_qt and localTranslationDir:
|
2021-05-11 16:34:38 +00:00
|
|
|
# This translation should be used to overwrite an existing Qt-translation.
|
|
|
|
isOverride = True
|
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
name, extension = os.path.splitext(currentFileName)
|
|
|
|
if not extension == ".qm":
|
2021-05-11 16:34:38 +00:00
|
|
|
continue
|
2021-05-11 18:25:40 +00:00
|
|
|
|
|
|
|
component = getComponentName(currentFileName)
|
|
|
|
|
|
|
|
if not component in allowed_components:
|
2021-05-11 16:34:38 +00:00
|
|
|
continue
|
2022-08-26 16:17:56 +00:00
|
|
|
|
2021-05-11 18:25:40 +00:00
|
|
|
if name in processedComponents and not isOverride:
|
|
|
|
continue
|
|
|
|
|
|
|
|
currentFilePath = os.path.join(directoryPath, currentFileName)
|
2021-05-11 16:34:38 +00:00
|
|
|
if not isOverride:
|
2021-05-11 18:25:40 +00:00
|
|
|
print(" > Bundling Qt translation \"{0}\"".format(currentFilePath))
|
|
|
|
outFile.write(' <file alias="{0}">{1}</file>\n'.format(currentFileName, currentFilePath))
|
|
|
|
processedComponents.append(name)
|
2021-05-11 16:34:38 +00:00
|
|
|
else:
|
|
|
|
# In order to recognize translation-overrides, we have to prefix them
|
2021-05-11 18:25:40 +00:00
|
|
|
print(" > Bundling Qt overwrite translation \"{0}\"".format(currentFilePath))
|
|
|
|
outFile.write(' <file alias="{0}">{1}</file>\n'.format("mumble_overwrite_" + currentFileName, currentFilePath))
|
|
|
|
|
|
|
|
return processedComponents
|
|
|
|
|
2021-05-11 16:34:38 +00:00
|
|
|
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
|
|
|
|
def main():
|
2021-05-11 16:34:38 +00:00
|
|
|
# python generate-mumble_qt-qrc.py <output-fn> [inputs...] localDir
|
2022-08-26 16:17:56 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--output", help="The path to which to write the generated QRC file", metavar="PATH", required=True)
|
|
|
|
parser.add_argument("--translation-dir", help="The path to the directory containing the official Qt translations",
|
|
|
|
action="append", metavar="PATH", required=True)
|
|
|
|
parser.add_argument("--local-translation-dir", help="The path to the local translation directory", metavar="PATH", required=True)
|
2021-05-11 16:34:38 +00:00
|
|
|
|
2022-08-26 16:17:56 +00:00
|
|
|
args = parser.parse_args()
|
2021-05-11 16:34:38 +00:00
|
|
|
|
2022-08-26 16:17:56 +00:00
|
|
|
# parse config file
|
|
|
|
configFile = os.path.join(args.local_translation_dir, "translations.conf")
|
2021-05-11 16:34:38 +00:00
|
|
|
if os.path.isfile(configFile):
|
|
|
|
parseTranslationsConfig(configFile)
|
|
|
|
|
2022-08-26 16:17:56 +00:00
|
|
|
of = open(args.output, 'w')
|
2021-05-11 16:34:38 +00:00
|
|
|
of.write('<!DOCTYPE RCC><RCC version="1.0">\n')
|
|
|
|
of.write('<qresource>\n')
|
2021-05-11 18:25:40 +00:00
|
|
|
processedComponents = []
|
2022-08-26 16:17:56 +00:00
|
|
|
for dirName in args.translation_dir:
|
2021-05-11 18:25:40 +00:00
|
|
|
processedComponents.extend(dirToQrc(of, dirName, processedComponents))
|
2021-05-11 16:34:38 +00:00
|
|
|
# Process translations provided by Mumble itself (aka local translations)
|
2022-08-26 16:17:56 +00:00
|
|
|
filesToQrc(of, processedComponents, local_qt_translations, args.local_translation_dir, True)
|
2021-05-11 16:34:38 +00:00
|
|
|
of.write('</qresource>\n')
|
|
|
|
of.write('</RCC>\n')
|
|
|
|
of.close()
|
Auto-generate qrc file for embedded Qt translations when running qmake.
This change removes our qmake-based Qt translation embedding.
That system uses mumble_qt.qrc resource file with
hardcoded filenames for Qt translations, and some logic
implemented in qmake that copies Qt translations into
the Mumble source tree such that the paths in the
mumble_qt.qrc file match.
The new system introduces a simple Python script that
takes an output filename for the .qrc file the tool
will write, along with a set of directories containing
Qt translations.
The tool will generate a Qt resource file containing
references to all the translation files found in the
specified directories. However, the tool takes care
to only include language files once.
In typical use, the first directory parameter passed
to the tool is the QT_INSTALL_TRANSLATIONS directory,
which is where Qt stores its own translation files.
The second directory is Mumble's fallback directory.
The tool then goes through all files in the first
directory, and notes down which languages have been
processed. Multiple files for a single langauge can
be included from the a directory (qt_help_da.qm,
and qtbase_da.qm), but once a language has been
added from one directory, it will not be added
if found in the next one in line.
We use this to include a set of 'fallback'
translations for versions of Qt that do not
include them. This also allows this new style
of Qt translation embedding to be forward
compatible with newer versions of Qt that
add new translations.
Once Qt includes a translation that we have
in our fallback directory, the Qt translation
is used instead.
2015-10-17 19:47:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-05-11 16:34:38 +00:00
|
|
|
main()
|