EDIT: As per the comments below, this is only an issue with older Qt versions. It works as expected since at least Qt 5.12.8.
Using the QLocale
class of Qt, I want to get the scripts which are typically used with a given language. I came up with this:
QSet<QLocale::Script> scriptsOfLanguage(QLocale::Language language) {
QSet<QLocale::Script> scripts;
for (const auto &locale : QLocale::matchingLocales(language, QLocale::AnyScript, QLocale::AnyCountry)) {
scripts.insert(locale.script());
}
scripts.remove(QLocale::AnyScript);
return scripts;
}
However, for some reason this code only works for languages which use at least two different scripts:
scriptsOfLanguage(QLocale::Chinese)
correctly returnsQSet(QLocale::SimplifiedHanScript, QLocale::TraditionalHanScript)
- However,
scriptsOfLanguage(QLocale::French)
returnsQSet()
instead of the expectedQSet(QLocale::LatinScript)
- Likewise,
scriptsOfLanguage(QLocale::Arabic)
also returnsQSet()
instead of the expectedQSet(QLocale::ArabicScript)
So, this code doesn't work as expected for most languages. What am I missing here, and what can I do about it (except to maybe hard-code this list of scripts and their associated languages into a huge switch
statement)?