Is there a way in Qt / QLocale to get the script of a language?

70 Views Asked by At

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 returns QSet(QLocale::SimplifiedHanScript, QLocale::TraditionalHanScript)
  • However, scriptsOfLanguage(QLocale::French) returns QSet() instead of the expected QSet(QLocale::LatinScript)
  • Likewise, scriptsOfLanguage(QLocale::Arabic) also returns QSet() instead of the expected QSet(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)?

0

There are 0 best solutions below