Improve OS::get_locale() on macOS and Windows, replace "-" with "_" and use system macros instead of bitwise AND. Add locale format info to the documentation.

This commit is contained in:
bruvzg 2020-07-25 23:42:11 +03:00
parent f2d6a4bf84
commit f797e1c078
No known key found for this signature in database
GPG key ID: FCED35F1CECE0D3A
3 changed files with 11 additions and 6 deletions

View file

@ -201,7 +201,12 @@
<return type="String"> <return type="String">
</return> </return>
<description> <description>
Returns the host OS locale. Returns the host OS locale as a string of the form [code]language_Script_COUNTRY_VARIANT@extra[/code].
[code]language[/code] - 2 or 3 letter [url=https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes]language code[/url], in lower case.
[code]Script[/code] - optional, 4 letter [url=https://en.wikipedia.org/wiki/ISO_15924]script code[/url], in title case.
[code]COUNTRY[/code] - optional, 2 or 3 letter [url=https://en.wikipedia.org/wiki/ISO_3166-1]country code[/url], in upper case.
[code]VARIANT[/code] - optional, language variant, region and sort order. Variant can have any number of underscored key words.
[code]extra[/code] - optional, semicolon separated list of additional key words. Currency, calendar, sort order and numbering system information.
</description> </description>
</method> </method>
<method name="get_model_name" qualifiers="const"> <method name="get_model_name" qualifiers="const">

View file

@ -284,7 +284,7 @@ Error OS_OSX::shell_open(String p_uri) {
String OS_OSX::get_locale() const { String OS_OSX::get_locale() const {
NSString *locale_code = [[NSLocale preferredLanguages] objectAtIndex:0]; NSString *locale_code = [[NSLocale preferredLanguages] objectAtIndex:0];
return [locale_code UTF8String]; return String([locale_code UTF8String]).replace("-", "_");
} }
String OS_OSX::get_executable_path() const { String OS_OSX::get_executable_path() const {

View file

@ -567,21 +567,21 @@ String OS_Windows::get_locale() const {
LANGID langid = GetUserDefaultUILanguage(); LANGID langid = GetUserDefaultUILanguage();
String neutral; String neutral;
int lang = langid & ((1 << 9) - 1); int lang = PRIMARYLANGID(langid);
int sublang = langid & ~((1 << 9) - 1); int sublang = SUBLANGID(langid);
while (wl->locale) { while (wl->locale) {
if (wl->main_lang == lang && wl->sublang == SUBLANG_NEUTRAL) if (wl->main_lang == lang && wl->sublang == SUBLANG_NEUTRAL)
neutral = wl->locale; neutral = wl->locale;
if (lang == wl->main_lang && sublang == wl->sublang) if (lang == wl->main_lang && sublang == wl->sublang)
return wl->locale; return String(wl->locale).replace("-", "_");
wl++; wl++;
} }
if (neutral != "") if (neutral != "")
return neutral; return String(neutral).replace("-", "_");
return "en"; return "en";
} }