简体   繁体   English

如何从数据库加载QTranslator?

[英]How to load QTranslator from database?

There is no way to load QTranslator from my own way. 无法以我自己的方式加载QTranslator。 I want to exclude .ts files from architecture of my app. 我想从我的应用程序体系结构中排除.ts文件。 I just want to load my languages from databse, whitch will be update from anywhere. 我只想从数据库加载我的语言,任何地方都可以更新。 And i don't want to load any files(.ts). 而且我不想加载任何文件(.ts)。 Does exists the way somthing like this: QTranslator::load(QStringList)??? 是否存在这样的事情:QTranslator :: load(QStringList)??? QStringList is a language pairs. QStringList是一种语言对。

The QTranslator::translate method is virtual - which means you can simply create your own translator that extends QTranslator and override this (and one other) method: QTranslator::translate方法是虚拟的-这意味着您可以简单地创建自己的转换器来扩展QTranslator并覆盖此(以及另一个)方法:

class MyTranslator : public QTranslator
{
public:
    MyTranslator(QStringList data, QObject* parent) : 
        QTranslator(parent)
    {
        // ...
    }

    bool isEmpty() const override {
        return false; //or use your own logic to determine if data contains translations
    }

    QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const override {
        // Use the data to somehow find your translation
    }
};

I understand your goal. 我了解您的目标。 Why don't you get the data from your database, save it as temporary file, load via QTranslator (regular way), then delete the temporary file? 为什么不从数据库中获取数据,将其另存为临时文件,通过QTranslator加载(常规方式),然后删除该临时文件?

Another option is maybe the overload for: 另一个选择可能是:

bool QTranslator::load(const uchar *data, int len, const QString &directory = QString()) bool QTranslator :: load(const uchar * data,int len,const QString&directory = QString())

(from: http://doc.qt.io/qt-5/qtranslator.html#load-2 ), which would allow you to load from your own structure without temp file. (来自: http : //doc.qt.io/qt-5/qtranslator.html#load-2 ),这将允许您从自己的结构中加载而不需要临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM