QT's dynamic translation function, which may depend on the message (event) mechanism

Source: Internet
Author: User
Tags translate function

Qtranslator Translator;
BOOL B = translator.load (QString (":/qm/lang_en"));
Later, regardless of the use of Qobject TR, or the use of qapplication translate, all rely on the message (event) loop, there is code for proof:

QString Qcoreapplication::translate (Const Char*context,Const Char*SourceText,Const Char*disambiguation,intN)    {QString result; if(!sourcetext)returnresult; if(Self &&!self->d_func ()Translators.isempty ()) {Qlist<QTranslator*>:: Constiterator it; Qtranslator*Translationfile;  for(it = Self->d_func ()->translators.constbegin (); It! = Self->d_func ()->translators.constend (); + +it) {Translationfile= *it; Result= translationfile->Translate (context, SourceText, disambiguation, N); if(!result.isnull ()) Break; }    }    if(Result.isnull ()) Result=Qstring::fromutf8 (SourceText); REPLACEPERCENTN (&result, n); returnresult;}StaticQString GetMessage (ConstUchar *m,ConstUchar *end,Const Char*context,Const Char*sourcetext,Const Char*comment,UINTnumerus) {    ConstUchar *tn =0; UINTTn_length =0;  for (;;) {Uchar Tag=0; if(M <end) Tag= Read8 (m++); Switch((tag) tag) { CaseTag_end:Gotoend;  Casetag_translation: {intLen =read32 (m); if(Len%1)                returnQString (); M+=4; if(!numerus--) {Tn_length=Len; TN=m; } m+=Len;  Break; }         Casetag_obsolete1:m+=4;  Break;  Casetag_sourcetext: {quint32 len=read32 (m); M+=4; if(!match (M, SourceText, Len))returnQString (); M+=Len; }             Break;  Casetag_context: {quint32 len=read32 (m); M+=4; if(!Match (m, context, Len))returnQString (); M+=Len; }             Break;  Casetag_comment: {quint32 len=read32 (m); M+=4; if(*m &&!)match (M, comment, Len))returnQString (); M+=Len; }             Break; default:            returnQString (); }}end:if(!tn)returnQString (); QString Str= QString ((ConstQchar *) TN, tn_length/2); if(Qsysinfo::byteorder = =Qsysinfo::littleendian) {         for(inti =0; I < str.length (); ++i) str[i]= Qchar ((str.at (i). Unicode () >>8) + ((str.at (i). Unicode () <<8) &0xff00)); }    returnstr;} QString qtranslatorprivate::d o_translate (Const Char*context,Const Char*SourceText,Const Char*comment,intNConst{    if(Context = =0) Context=""; if(SourceText = =0) SourceText=""; if(Comment = =0) Comment=""; UINTNumerus =0; size_t NumItems=0; if(!offsetlength)Gotosearchdependencies; /*Check If the context belongs to this qtranslator.    If Many translators is installed, this step is necessary. */    if(contextlength) {quint16 htablesize=read16 (Contextarray); UINTG = elfhash (context)%htablesize; ConstUchar *c = Contextarray +2+ (g <<1); Quint16 off=Read16 (c); C+=2; if(Off = =0)            returnQString (); C= Contextarray + (2+ (Htablesize <<1) + (off <<1));  for (;;) {Quint8 len= Read8 (c + +); if(len = =0)                returnQString (); if(Match (c, context, Len)) Break; C+=Len; }} NumItems= Offsetlength/(2*sizeof(Quint32)); if(!NumItems)Gotosearchdependencies; if(N >=0) Numerus=numerushelper (n, Numerusrulesarray, numerusruleslength);  for (;;) {Quint32 h=0;        Elfhash_continue (SourceText, h);        Elfhash_continue (Comment, h);        Elfhash_finish (h); ConstUchar *start =Offsetarray; ConstUchar *end = start + ((numitems-1) <<3);  while(Start <=end) {            ConstUchar *middle = start + ((End-start) >>4) <<3); UINThash =read32 (middle); if(H = =hash) {Start=Middle;  Break; } Else if(Hash <h) {Start= Middle +8; } Else{End= Middle-8; }        }        if(Start <=end) {            //go back on equal key             while(Start! = Offsetarray && read32 (start) = = Read32 (start-8)) Start-=8;  while(Start < Offsetarray +offsetlength) {Quint32 RH=read32 (start); Start+=4; if(RH! =h) Break; Quint32 ro=read32 (start); Start+=4; QString TN= GetMessage (Messagearray + ro, Messagearray +messagelength, context, SourceText, comment, numerus); if(!tn.isnull ())returnTN; }        }        if(!comment[0])             Break; Comment=""; }searchdependencies:foreach(Qtranslator *Translator, Subtranslators) {QString tn= translator->Translate (context, sourcetext, comment, n); if(!tn.isnull ())returnTN; }    returnQString ();}

Originally in the GUI using a good tr and translate function, in the Qtservice again the thread is out of the question (in qtservice use itself no problem)

Workaround:
1. Read the translation file XML yourself, use it to make a map, for my own time dynamically to the contents of the query out
2. In each thread, apply for a service, reinstall Qtranslator, and then use the TR or translate function (do not try, do not know the line)
3. Forward the TR or translate to the global service that executes exec (), which is equivalent to going to the main event loop, and then jumping back (without trying, don't know).

Official Reference documents:
Http://doc.qt.io/qt-5/i18n-source-translation.html

QT's dynamic translation function, which may depend on the message (event) mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.