問題描述
我嘗試使用 QLocale 和 setDefault 函數更改語言環境,但似乎不起作用.這是使用 C 本地化庫和 QLocale 更改語言環境的示例.對于 C 本地化庫,它似乎有效,但對于 QLocale,似乎忽略了 setDefault 函數調用.
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 中正確更改語言環境以影響程序?
How to change properly locale in Qt so it affects the program?
推薦答案
QLocale::setDefault()
不會更改系統區域設置.它改變了使用默認構造函數創建的 QLocale
對象.
QLocale::setDefault()
does not change the system locale. It changes what QLocale
object created with default constructor is.
據說,系統區域設置只能由用戶通過系統控制面板/首選項進行更改.如果要格式化不在系統語言環境中的內容,則需要專門使用語言環境對象來執行此操作.
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"
這篇關于在 Qt 中更改語言環境的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!