Most real projects have a target audience in multiple countries. The most notable difference between them is the spoken language, but there are other aspects some developers may not think of. For example, dot "."
and comma ","
are both fairly common as the decimal separator throughout the world. Date formats are also very different and incompatible, and using a wrong format (for example, mm/dd/yyyy
instead of dd/mm/yyyy
) will result in a completely different date.
Qt provides the QLocale
class for dealing with locale-dependent operations, including conversions between numbers in strings. In the following code, text
and number
may have different values, depending on the system locale:
QLocale locale = QLocale::system();
QString text = locale.toString(1.2);
double number = locale.toDouble(QStringLiteral("1,2"));
QLocale
also provides methods for formatting dates and prices, and allows us to request additional information about local conventions.
Translator
ts files
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en" sourcelanguage="en">
<context>
<name>QApplication</name>
<message>
<source>Hello2</source>
<translatorcomment>there are no comment</translatorcomment>
<translation>iam good</translation>
</message>
</context>
<context>
<name>QLabel</name>
<message>
<source>Hello</source>
<translatorcomment>there are no comment</translatorcomment>
<translation>iam good</translation>
</message>
</context>
</TS>
in Qt Linguist you can release ts file to qm binary file to load by QTranslator
QTranslator translator;
if(translator.load("PNP_en_US","..\\PNP\\")){
qDebug()<<"load translation file";
}
a.installTranslator(&translator);
qDebug()<<a.tr("Hello2");
qDebug()<<QLabel::tr("Hello");