Qt Linguist: recover text to the base language
-
Hi everyone!
I have the text table . In runtime I change the text language from lang1 (base lang) to lang2.
Table text is saves to the file in the end of the process.Is it possible to recover text from lang2 to lang1 at the start of app or save text to file in base language?
-
@SGaist, yes, I use QTranslator to change language.
My case:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
So, user selects one of the QComboBox variant (ver.1). After translation, this parameter contains translated version (ver.2) of the text. This text saves to the xml.
But when the app starts and xml reads, variable has ver.2 and it can't be shown correctly.@sitesv said in Qt Linguist: recover text to the base language:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
Okay, I understand why this might get complicated. In a lot of cases it is sufficient to be able to compare the text that was read in from the file with a specific string (where we can use the translated string again).
In your case, I suggest the following: For each entry in the QComboBox add the non-translated text as user data. When adding items you can use
QComboBox::addItem(tr(text),text). When the user selects something you get back the non-translated string usingcomboBox->itemData(comboBox->currentIndex()).toString(). Now, it gets slightly more complicated: Change from a QTableWidget to a QTableView. This also means that you need to write your own model class derived fromQAbstractItemModel. Inside the model store the non-translated texts. The magic now happens with your derived class which needs to overridedata(...). For theDisplayRoleof this function you then return the translated string, i.e. only when displaying the string in the table will you translate the text and everything before that is the non-translated text. You can then still get out the non-translated text from the model (your own derived class) and store this in your xml file. -
Hi,
If you are using QTranslator, it's a one way translation from the original text in code to the one provided by QTranslator.
You need to provide more information about your text table. What is it ? Is it user modifiable ? How do you save and restore it ?
-
With Qt Linguist you are editing .ts files. These get converted to .qm files. The .qm files store the information in binary format as a hash table for quicker access. IIRC the base language string is saved alongside the translation because of possible collisions. However, Qt does not provide a way to access the original string (which you actually know when you are looking up the translation). Hash tables also just work well one way. It is not feasible to do the inverse look up. As the data is all stored inside the .qm file you could read that information yourself or change the Qt source code to give you the information you want.
-
Hi,
If you are using QTranslator, it's a one way translation from the original text in code to the one provided by QTranslator.
You need to provide more information about your text table. What is it ? Is it user modifiable ? How do you save and restore it ?
@SGaist, yes, I use QTranslator to change language.
My case:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
So, user selects one of the QComboBox variant (ver.1). After translation, this parameter contains translated version (ver.2) of the text. This text saves to the xml.
But when the app starts and xml reads, variable has ver.2 and it can't be shown correctly. -
This post is deleted!
-
@SGaist, yes, I use QTranslator to change language.
My case:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
So, user selects one of the QComboBox variant (ver.1). After translation, this parameter contains translated version (ver.2) of the text. This text saves to the xml.
But when the app starts and xml reads, variable has ver.2 and it can't be shown correctly.@davidbland889, thanks for reply.
but I trying to figure out for possibility to resolve my case without external api. I think my case is simple and common :) -
@davidbland889, thanks for reply.
but I trying to figure out for possibility to resolve my case without external api. I think my case is simple and common :) -
@SGaist, yes, I use QTranslator to change language.
My case:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
So, user selects one of the QComboBox variant (ver.1). After translation, this parameter contains translated version (ver.2) of the text. This text saves to the xml.
But when the app starts and xml reads, variable has ver.2 and it can't be shown correctly.@sitesv said in Qt Linguist: recover text to the base language:
I use QComboBox, that shows a list of the parameter variants. QComboBox is connected to the QTableWidget as a delegate.
Okay, I understand why this might get complicated. In a lot of cases it is sufficient to be able to compare the text that was read in from the file with a specific string (where we can use the translated string again).
In your case, I suggest the following: For each entry in the QComboBox add the non-translated text as user data. When adding items you can use
QComboBox::addItem(tr(text),text). When the user selects something you get back the non-translated string usingcomboBox->itemData(comboBox->currentIndex()).toString(). Now, it gets slightly more complicated: Change from a QTableWidget to a QTableView. This also means that you need to write your own model class derived fromQAbstractItemModel. Inside the model store the non-translated texts. The magic now happens with your derived class which needs to overridedata(...). For theDisplayRoleof this function you then return the translated string, i.e. only when displaying the string in the table will you translate the text and everything before that is the non-translated text. You can then still get out the non-translated text from the model (your own derived class) and store this in your xml file. -
@SimonSchroeder Thank You!
-
S sitesv has marked this topic as solved on