11

QLocale と setDefault 関数を使用してロケールを変更しようとしましたが、うまくいかないようです。C ローカライゼーション ライブラリと QLocale を使用してロケールを変更する例を次に示します。C ローカリゼーション ライブラリでは機能しているようですが、QLocale では setDefault 関数呼び出しが無視されるようです。

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(&currentTime);
std::tm* timeinfo = std::localtime(&currentTime);
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でロケールを適切に変更してプログラムに影響を与える方法は?

4

1 に答える 1

19

QLocale::setDefault()システムロケールは変更されません。QLocaleデフォルトのコンストラクタで作成されたオブジェクトを変更します。

おそらく、システムロケールは、ユーザーがシステムコントロールパネル/設定を介してのみ変更できます。システムロケールにないものをフォーマットする場合は、ロケールオブジェクトを使用して具体的に行う必要があります。

このコード:

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" 
于 2012-11-09T19:14:29.090 に答える