問(wèn)題描述
我嘗試使用 QLocale 和 setDefault 函數(shù)更改語(yǔ)言環(huán)境,但似乎不起作用.這是使用 C 本地化庫(kù)和 QLocale 更改語(yǔ)言環(huán)境的示例.對(duì)于 C 本地化庫(kù),它似乎有效,但對(duì)于 QLocale,似乎忽略了 setDefault 函數(shù)調(diào)用.
I tried to change locale using QLocale and setDefault function but it seems that it doesn't work. Here is example of changing locale using C localization library and QLocale. For C localization library it seems that it works, but for QLocale it seems that setDefault function call is ignored.
QLocale curLocale(QLocale("pl_PL"));
QLocale::setDefault(curLocale);
QDate date = QDate::currentDate();
QString dateString = date.toString();
// prints "Fri Nov 9 2012" but that was not expected
std::cout << dateString.toStdString() << std::endl;
// prints "en_US", but shouldn't it be "pl_PL"?
std::cout << QLocale::system().name().toStdString() << std::endl;
std::setlocale(LC_ALL, "pl_PL");
// prints "pl_PL"
std::cout << std::setlocale(LC_ALL, 0) << std::endl;
std::time_t currentTime;
std::time(¤tTime);
std::tm* timeinfo = std::localtime(¤tTime);
char charArray[40];
std::strftime(charArray, 40, "%a %b %d %Y", timeinfo);
// prints "pi lis 09 2012" and that's cool
std::cout << charArray << std::endl;
如何在 Qt 中正確更改語(yǔ)言環(huán)境以影響程序?
How to change properly locale in Qt so it affects the program?
推薦答案
QLocale::setDefault()
不會(huì)更改系統(tǒng)區(qū)域設(shè)置.它改變了使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建的 QLocale
對(duì)象.
QLocale::setDefault()
does not change the system locale. It changes what QLocale
object created with default constructor is.
據(jù)說(shuō),系統(tǒng)區(qū)域設(shè)置只能由用戶通過(guò)系統(tǒng)控制面板/首選項(xiàng)進(jìn)行更改.如果要格式化不在系統(tǒng)語(yǔ)言環(huán)境中的內(nèi)容,則需要專門使用語(yǔ)言環(huán)境對(duì)象來(lái)執(zhí)行此操作.
Supposedly, system locale can only be changed via system control panel/preferences by the user. If you want to format something that is not in the system locale, you need to specifically do that with a locale object.
此代碼:
QLocale curLocale(QLocale("pl_PL"));
QLocale::setDefault(curLocale);
QDate date = QDate::currentDate();
QString dateString = QLocale().toString(date);
qDebug() << dateString;
qDebug() << QLocale().name();
打印:
"pi?tek, 9 listopada 2012"
"pl_PL"
這篇關(guān)于在 Qt 中更改語(yǔ)言環(huán)境的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!