Last active
June 28, 2018 16:11
-
-
Save tarptaeya/0840d1a9040af2f8775a1b879a43bace to your computer and use it in GitHub Desktop.
Using Python gettext in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "i18n.h" | |
#include <QDebug> | |
#include <QStandardPaths> | |
i18n::i18n() | |
: m_gettextModule(nullptr) | |
, m_translationFunction(nullptr) | |
, m_gettextFunction(nullptr) | |
{ | |
Py_Initialize(); | |
m_gettextModule = PyImport_ImportModule("gettext"); | |
if (!m_gettextModule) { | |
return; | |
} | |
PyObject *gettextModuleDict = PyModule_GetDict(m_gettextModule); | |
m_translationFunction = PyDict_GetItemString(gettextModuleDict, "translation"); | |
Py_DECREF(gettextModuleDict); | |
if (!m_translationFunction || !PyCallable_Check(m_translationFunction)) { | |
return; | |
} | |
PyObject *args = PyTuple_New(3); | |
PyObject *domainString = PyUnicode_FromString("DemoDomain"); | |
PyTuple_SetItem(args, 0, domainString); | |
QString localeDirLocation = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "locale", QStandardPaths::LocateDirectory); | |
PyObject *localeDir = PyUnicode_FromString(localeDirLocation.toUtf8()); | |
PyTuple_SetItem(args, 1, localeDir); | |
PyObject *languagesList = PyList_New(0); | |
PyObject *spanishLanguage = PyUnicode_FromString("es"); | |
PyList_Append(languagesList, spanishLanguage); | |
PyTuple_SetItem(args, 2, languagesList); | |
PyObject *translationObject = PyObject_CallObject(m_translationFunction, args); | |
Py_DECREF(languagesList); | |
Py_DECREF(spanishLanguage); | |
Py_DECREF(localeDir); | |
Py_DECREF(domainString); | |
Py_DECREF(args); | |
if (!translationObject) { | |
return; | |
} | |
m_gettextFunction = PyObject_GetAttrString(translationObject, "gettext"); | |
Py_DECREF(translationObject); | |
} | |
i18n::~i18n() | |
{ | |
Py_Finalize(); | |
} | |
QString i18n::getText(const QString &string) | |
{ | |
if (!m_gettextFunction || !PyCallable_Check(m_gettextFunction)) { | |
return string; | |
} | |
PyObject *args = PyTuple_New(1); | |
PyObject *stringToTranslate = PyUnicode_FromString(string.toUtf8()); | |
PyTuple_SetItem(args, 0, stringToTranslate); | |
PyObject *translatedPyString = PyObject_CallObject(m_gettextFunction, args); | |
Py_DECREF(stringToTranslate); | |
Py_DECREF(args); | |
if (!translatedPyString) { | |
return string; | |
} | |
QString translatedQString = QString::fromUtf8(PyUnicode_AsUTF8(translatedPyString)); | |
Py_DECREF(translatedPyString); | |
return translatedQString; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Python.h" | |
#include <QString> | |
class i18n | |
{ | |
public: | |
i18n(); | |
~i18n(); | |
QString getText(const QString &string); | |
private: | |
PyObject *m_gettextModule; | |
PyObject *m_translationFunction; | |
PyObject *m_gettextFunction; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "i18n.h" | |
#include "iostream" | |
int main() | |
{ | |
i18n *i = new i18n; | |
std::cout << i->getText("Hello World").toStdString() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment