Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OS::get_locale() and documentation. #40708

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@
<return type="String">
</return>
<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.
Comment on lines +206 to +209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing some of those are OS-specific, maybe this should be documented too? Or do all OSes follow the same format aside from some using - and others using _?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall pattern is valid for all OSes, but none of the locales have all parts. Only @ is Linux specific, the rest can be encountered on any OS.

</description>
</method>
<method name="get_model_name" qualifiers="const">
Expand Down
2 changes: 1 addition & 1 deletion platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ virtual void log_error(const char *p_function, const char *p_file, int p_line, c

String OS_OSX::get_locale() const {
NSString *locale_code = [[NSLocale preferredLanguages] objectAtIndex:0];
return [locale_code UTF8String];
return String([locale_code UTF8String]).replace("-", "_");
}

String OS_OSX::get_executable_path() const {
Expand Down
8 changes: 4 additions & 4 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,21 @@ String OS_Windows::get_locale() const {

LANGID langid = GetUserDefaultUILanguage();
String neutral;
int lang = langid & ((1 << 9) - 1);
int sublang = langid & ~((1 << 9) - 1);
int lang = PRIMARYLANGID(langid);
int sublang = SUBLANGID(langid);

while (wl->locale) {
if (wl->main_lang == lang && wl->sublang == SUBLANG_NEUTRAL)
neutral = wl->locale;

if (lang == wl->main_lang && sublang == wl->sublang)
return wl->locale;
return String(wl->locale).replace("-", "_");

wl++;
}

if (neutral != "")
return neutral;
return String(neutral).replace("-", "_");

return "en";
}
Expand Down