read translation text from excel sheet into QTranslator at runtime
-
I want to load translation text for all available strings in qml from the excel sheet, where the user can modify translation text for language as per his requirement which can also be affected in the application on reload button and user can see the changed transition on UI
Excel Sheet format be like this image url)
-
Finally, I have implemented this using QXlsx https://github.com/QtExcel/QXlsx library for reading MS Xlsx file and store it in the below DS to load data mentioned in post image above:
QMap<QString, QMap<QString, QString>> m_langTransMap;
QMap<Input String,QMap<LangName,TranslatedText>>Use of Own Translator:
QML:
Text{
text: myTranslator.getTranslatedText(
"Main Controller") + myTranslator.emptyString]
}class TranslationHandler : public QObject {
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)public:
void TranslationHandler::loadTranslationData() {
/// Read Excel Data
/// load it into QMap
emit languageChanged();
}Reference Link:
-
Hi,
If you want to use QTranslator, you would need to write your own data extractor, create the translation files and then call lconvert to regenerate the final files to be loaded.
-
@SGaist But looking at lconvert tool is used to merge ts file while deploying, we want to change transition text while running app(Deployed App at customer PC)
And how to read excel(xlsx) file with above format in Qt? any idea
-
@Vijay-R-Gawade if its a true excel file and not just a CSV:
https://wiki.qt.io/Handling_Microsoft_Excel_file_format -
In that case you will have to implement your own translation handling.
-
@SGaist Thanks for the suggestion. So I need to write my own function which will return the translation text for the input string according to define in excel & can be maintained in some DS like QMAP and used it in QML/C++ instead of qsTr() & tr() calls.
I will try this & update here
-
In addition to what @SGaist suggested, better to define the your own ID for each string & use them inside your application. Read the excel(better to make it csv) & keep the mapping of ID to different languages. Keep this in seperate model object. Based on the selection, your model object should return different strings.
-
Finally, I have implemented this using QXlsx https://github.com/QtExcel/QXlsx library for reading MS Xlsx file and store it in the below DS to load data mentioned in post image above:
QMap<QString, QMap<QString, QString>> m_langTransMap;
QMap<Input String,QMap<LangName,TranslatedText>>Use of Own Translator:
QML:
Text{
text: myTranslator.getTranslatedText(
"Main Controller") + myTranslator.emptyString]
}class TranslationHandler : public QObject {
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)public:
void TranslationHandler::loadTranslationData() {
/// Read Excel Data
/// load it into QMap
emit languageChanged();
}Reference Link: