简体   繁体   中英

How does the default locale “root” work in ICU?

Currently seeing discrepancies in how default locale "root" behaves in Linux and Windows.

Using ICU v54.1

In Windows, we are seeing the Locale::getDefault() as en_GB.
Platform: WIndows Server 2012 R2

In Linux, we are seeing the Locale::getDefault() as "root".
Platform: CentOS 7
strace -e file /usr/bin/locale gives

execve("/usr/bin/locale", ["locale"], [/* 27 vars */]) = 0
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
LANG=en_GB.UTF-8
LC_CTYPE="en_GB"
LC_NUMERIC="en_GB"
LC_TIME="en_GB"
LC_COLLATE="en_GB"
LC_MONETARY="en_GB"
LC_MESSAGES="en_GB"
LC_PAPER="en_GB"
LC_NAME="en_GB"
LC_ADDRESS="en_GB"
LC_TELEPHONE="en_GB"
LC_MEASUREMENT="en_GB"
LC_IDENTIFICATION="en_GB"
LC_ALL=en_GB

At the moment not sure how "root" locale is mapped! After googling, seen it is could be mapped to en_US_POSIX - how can this be verified?

When printing out list of NumberFormat::getAvailableLocales , only getting short names instead of the full names when using locale.getDisplayName(name)

Although it works as expected, when specifying Locale::setDefault(locid) , which is not ideal!

Also tried with export LC_ALL=C , but this did not have affect on default locale in ICU.

Ideally in the code just setting the locale as "C" would be sufficient and not manage the default.

icu::Locale::Locale("C");

Decided to use getDisplayName(Locale, UnicodeString name) to return display name specific to locale when the default is 'root'

For example;

    const icu::Locale fallbackDisplayNameLocale = icu::Locale("en_US");
    UnicodeString name(icu_locale.getName());
    std::string displayName;
    if (strcmp(icu::Locale::getDefault().getLanguage(), "root") == 0)
    {
        icu_locale.getDisplayName(fallbackDisplayNameLocale, name).toUTF8String(displayName);
    } else {
        icu_locale.getDisplayName(name).toUTF8String(displayName);
    }

Possibly this helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM