Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find current QLocale in Qt/PyQt/PySide?

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

  • How to get current application language
  • How to force QLocale::system to change
like image 292
eric Avatar asked Jan 27 '15 01:01

eric


2 Answers

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.

like image 144
ekhumoro Avatar answered Sep 23 '22 01:09

ekhumoro


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.

like image 23
Marcos Avatar answered Sep 26 '22 01:09

Marcos