| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2018 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 51 Franklin Street, * |
| 21 |
* Fifth Floor, Boston, MA 02110-1301, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "Translate.h" |
| 27 |
#include <QCoreApplication> |
| 28 |
#include <QFileInfo> |
| 29 |
|
| 30 |
using namespace Base; |
| 31 |
|
| 32 |
|
| 33 |
Translate::Translate() |
| 34 |
: Py::ExtensionModule<Translate>("__Translate__") |
| 35 |
{ |
| 36 |
add_varargs_method( |
| 37 |
"translate", |
| 38 |
&Translate::translate, |
| 39 |
"translate(context, sourcetext, disambiguation = None, n=-1)\n" |
| 40 |
"-- Returns the translation text for sourceText, by querying\n" |
| 41 |
"the installed translation files. The translation files are\n" |
| 42 |
"searched from the most recently installed file back to the\n" |
| 43 |
"first installed file." |
| 44 |
); |
| 45 |
add_varargs_method( |
| 46 |
"QT_TRANSLATE_NOOP", |
| 47 |
&Translate::translateNoop, |
| 48 |
"QT_TRANSLATE_NOOP(context, sourcetext)\n" |
| 49 |
"Marks the UTF-8 encoded string literal sourcetext for delayed translation " |
| 50 |
"in the given context.\n" |
| 51 |
"The context is typically a class name and also needs to be specified as a " |
| 52 |
"string literal." |
| 53 |
); |
| 54 |
add_varargs_method( |
| 55 |
"QT_TRANSLATE_NOOP3", |
| 56 |
&Translate::translateNoop3, |
| 57 |
"QT_TRANSLATE_NOOP3(context, sourcetext, disambiguation)\n" |
| 58 |
"Marks the UTF-8 encoded string literal sourceText for delayed translation " |
| 59 |
"in the given context\n" |
| 60 |
"with the given disambiguation. The context is typically a class and also " |
| 61 |
"needs to be specified\n" |
| 62 |
"as a string literal. The string literal disambiguation should be a short " |
| 63 |
"semantic tag to tell\n" |
| 64 |
"apart otherwise identical strings." |
| 65 |
); |
| 66 |
add_varargs_method( |
| 67 |
"QT_TRANSLATE_NOOP_UTF8", |
| 68 |
&Translate::translateNoop, |
| 69 |
"QT_TRANSLATE_NOOP_UTF8(context, sourcetext)\n" |
| 70 |
"Same as QT_TRANSLATE_NOOP" |
| 71 |
); |
| 72 |
add_varargs_method( |
| 73 |
"QT_TR_NOOP", |
| 74 |
&Translate::trNoop, |
| 75 |
"QT_TR_NOOP(sourcetext)\n" |
| 76 |
"Marks the UTF-8 encoded string literal sourcetext for delayed translation " |
| 77 |
"in the current context" |
| 78 |
); |
| 79 |
add_varargs_method( |
| 80 |
"QT_TR_NOOP_UTF8", |
| 81 |
&Translate::trNoop, |
| 82 |
"QT_TR_NOOP_UTF8(sourcetext)\n" |
| 83 |
"Same as QT_TR_NOOP" |
| 84 |
); |
| 85 |
add_varargs_method( |
| 86 |
"installTranslator", |
| 87 |
&Translate::installTranslator, |
| 88 |
"Install a translator for testing purposes" |
| 89 |
); |
| 90 |
add_varargs_method("removeTranslators", &Translate::removeTranslators, "Remove test translators"); |
| 91 |
initialize("This module is the Translate module"); // register with Python |
| 92 |
} |
| 93 |
|
| 94 |
Translate::~Translate() = default; |
| 95 |
|
| 96 |
Py::Object Translate::translate(const Py::Tuple& args) |
| 97 |
{ |
| 98 |
char* context = nullptr; |
| 99 |
char* source = nullptr; |
| 100 |
char* disambiguation = nullptr; |
| 101 |
int num = -1; |
| 102 |
if (!PyArg_ParseTuple(args.ptr(), "ss|zi", &context, &source, &disambiguation, &num)) { |
| 103 |
throw Py::Exception(); |
| 104 |
} |
| 105 |
|
| 106 |
QString str = QCoreApplication::translate(context, source, disambiguation, num); |
| 107 |
return Py::asObject(PyUnicode_FromString(str.toUtf8())); |
| 108 |
} |
| 109 |
|
| 110 |
Py::Object Translate::translateNoop(const Py::Tuple& args) |
| 111 |
{ |
| 112 |
PyObject* arg1 = nullptr; |
| 113 |
PyObject* arg2 = nullptr; |
| 114 |
if (!PyArg_ParseTuple(args.ptr(), "OO", &arg1, &arg2)) { |
| 115 |
throw Py::Exception(); |
| 116 |
} |
| 117 |
return Py::Object(arg2); |
| 118 |
} |
| 119 |
|
| 120 |
Py::Object Translate::translateNoop3(const Py::Tuple& args) |
| 121 |
{ |
| 122 |
PyObject* arg1 = nullptr; |
| 123 |
PyObject* arg2 = nullptr; |
| 124 |
PyObject* arg3 = nullptr; |
| 125 |
if (!PyArg_ParseTuple(args.ptr(), "OOO", &arg1, &arg2, &arg3)) { |
| 126 |
throw Py::Exception(); |
| 127 |
} |
| 128 |
return Py::Object(arg2); |
| 129 |
} |
| 130 |
|
| 131 |
Py::Object Translate::trNoop(const Py::Tuple& args) |
| 132 |
{ |
| 133 |
PyObject* arg1 = nullptr; |
| 134 |
if (!PyArg_ParseTuple(args.ptr(), "O", &arg1)) { |
| 135 |
throw Py::Exception(); |
| 136 |
} |
| 137 |
return Py::Object(arg1); |
| 138 |
} |
| 139 |
|
| 140 |
Py::Object Translate::installTranslator(const Py::Tuple& args) |
| 141 |
{ |
| 142 |
char* Name = nullptr; |
| 143 |
if (!PyArg_ParseTuple(args.ptr(), "et", "utf-8", &Name)) { |
| 144 |
throw Py::Exception(); |
| 145 |
} |
| 146 |
QString filename = QString::fromUtf8(Name); |
| 147 |
PyMem_Free(Name); |
| 148 |
|
| 149 |
bool ok = false; |
| 150 |
QFileInfo fi(filename); |
| 151 |
std::shared_ptr<QTranslator> translator(std::make_shared<QTranslator>(nullptr)); |
| 152 |
translator->setObjectName(fi.fileName()); |
| 153 |
if (translator->load(filename)) { |
| 154 |
qApp->installTranslator(translator.get()); |
| 155 |
translators.push_back(translator); |
| 156 |
ok = true; |
| 157 |
} |
| 158 |
|
| 159 |
return Py::Boolean(ok); // NOLINT |
| 160 |
} |
| 161 |
|
| 162 |
Py::Object Translate::removeTranslators(const Py::Tuple& args) |
| 163 |
{ |
| 164 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 165 |
throw Py::Exception(); |
| 166 |
} |
| 167 |
bool ok = true; |
| 168 |
for (const auto& it : translators) { |
| 169 |
ok &= QCoreApplication::removeTranslator(it.get()); |
| 170 |
} |
| 171 |
|
| 172 |
translators.clear(); |
| 173 |
return Py::Boolean(ok); // NOLINT |
| 174 |
} |