How can I find the currently active QLocale
? We can find the default system locale with QLocale.system()
, but I want something like QLocale.current()
, to check if the locale changes I am trying to make are actually working.
The documentation has lots of methods for setting locales, or finding the properties of a given QLocale
. But is there a simple method to return the current QLocale
, so that I can then apply these methods to it (e.g., name()
)?
Related questions
The answer is to simply use:
current_locale = QtCore.QLocale()
This will resolve to the system locale, unless the default locale has been explicitly re-set using QLocale.setDefault()
.
So the normal procedure would be: if necessary, set the default locale immediately after the QApplication
is created. After that, any time a QLocale
object is created with no arguments, it will either resolve to the locale you originally set yourself, or fall back to the system locale.
This seems to imply that it is best to always construct a new QLocale
object to obtain information about the locale, rather than caching the information for later re-use.
You can find the current locale by getting an instance of the class:
curr_locale = QLocale()
And then get the current locale information, e.g.:
print(curr_locale.nativeCountryName(), curr_locale.name(), curr_locale.nativeLanguageName())
And if you want to change the current locale, e.g.:
QLocale.setDefault(QLocale(QLocale.Portuguese, QLocale.Brazil))
See the QLocale documentation for more methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With