瀏覽代碼

Implement `OS::get_locale_language()` helper method

This method extracts the 2 or 3-letter language code from `OS::get_locale()`,
making it easier for users to identify the "main" language code for users
that might have different OS locales due to different OS or region, but
should be matched to the same translation (e.g. "generic" Spanish).

Fixes #40703.

(cherry picked from commit def99c7baf1880e3efc0b60b9eb035be8a7edb14)
Rémi Verschelde 4 年之前
父節點
當前提交
b4fc24b73f
共有 6 個文件被更改,包括 30 次插入8 次删除
  1. 5 0
      core/bind/core_bind.cpp
  2. 1 0
      core/bind/core_bind.h
  3. 6 0
      core/os/os.cpp
  4. 1 0
      core/os/os.h
  5. 12 5
      doc/classes/OS.xml
  6. 5 3
      doc/classes/TranslationServer.xml

+ 5 - 0
core/bind/core_bind.cpp

@@ -527,6 +527,10 @@ String _OS::get_locale() const {
 	return OS::get_singleton()->get_locale();
 	return OS::get_singleton()->get_locale();
 }
 }
 
 
+String _OS::get_locale_language() const {
+	return OS::get_singleton()->get_locale_language();
+}
+
 String _OS::get_latin_keyboard_variant() const {
 String _OS::get_latin_keyboard_variant() const {
 	switch (OS::get_singleton()->get_latin_keyboard_variant()) {
 	switch (OS::get_singleton()->get_latin_keyboard_variant()) {
 		case OS::LATIN_KEYBOARD_QWERTY:
 		case OS::LATIN_KEYBOARD_QWERTY:
@@ -1333,6 +1337,7 @@ void _OS::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_ticks_usec"), &_OS::get_ticks_usec);
 	ClassDB::bind_method(D_METHOD("get_ticks_usec"), &_OS::get_ticks_usec);
 	ClassDB::bind_method(D_METHOD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
 	ClassDB::bind_method(D_METHOD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
 	ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
 	ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
+	ClassDB::bind_method(D_METHOD("get_locale_language"), &_OS::get_locale_language);
 	ClassDB::bind_method(D_METHOD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
 	ClassDB::bind_method(D_METHOD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
 	ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
 	ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
 
 

+ 1 - 0
core/bind/core_bind.h

@@ -256,6 +256,7 @@ public:
 	Vector<String> get_cmdline_args();
 	Vector<String> get_cmdline_args();
 
 
 	String get_locale() const;
 	String get_locale() const;
+	String get_locale_language() const;
 	String get_latin_keyboard_variant() const;
 	String get_latin_keyboard_variant() const;
 	int keyboard_get_layout_count() const;
 	int keyboard_get_layout_count() const;
 	int keyboard_get_current_layout() const;
 	int keyboard_get_current_layout() const;

+ 6 - 0
core/os/os.cpp

@@ -284,6 +284,12 @@ String OS::get_locale() const {
 	return "en";
 	return "en";
 }
 }
 
 
+// Non-virtual helper to extract the 2 or 3-letter language code from
+// `get_locale()` in a way that's consistent for all platforms.
+String OS::get_locale_language() const {
+	return get_locale().left(3).replace("_", "");
+}
+
 // Helper function to ensure that a dir name/path will be valid on the OS
 // Helper function to ensure that a dir name/path will be valid on the OS
 String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
 String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const {
 	Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
 	Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");

+ 1 - 0
core/os/os.h

@@ -427,6 +427,7 @@ public:
 	RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
 	RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
 
 
 	virtual String get_locale() const;
 	virtual String get_locale() const;
+	String get_locale_language() const;
 
 
 	String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const;
 	String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const;
 	virtual String get_godot_dir_name() const;
 	virtual String get_godot_dir_name() const;

+ 12 - 5
doc/classes/OS.xml

@@ -256,14 +256,21 @@
 		<method name="get_locale" qualifiers="const">
 		<method name="get_locale" qualifiers="const">
 			<return type="String" />
 			<return type="String" />
 			<description>
 			<description>
-				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.
+				Returns the host OS locale as a string of the form [code]language_Script_COUNTRY_VARIANT@extra[/code]. If you want only the language code and not the fully specified locale from the OS, you can use [method get_locale_language].
+				[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 keywords.
 				[code]extra[/code] - optional, semicolon separated list of additional key words. Currency, calendar, sort order and numbering system information.
 				[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_locale_language" qualifiers="const">
+			<return type="String" />
+			<description>
+				Returns the host OS locale's 2 or 3-letter [url=https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes]language code[/url] as a string which should be consistent on all platforms. This is equivalent to extracting the [code]language[/code] part of the [method get_locale] string.
+				This can be used to narrow down fully specified locale strings to only the "common" language code, when you don't need the additional information about country code or variants. For example, for a French Canadian user with [code]fr_CA[/code] locale, this would return [code]fr[/code].
+			</description>
+		</method>
 		<method name="get_model_name" qualifiers="const">
 		<method name="get_model_name" qualifiers="const">
 			<return type="String" />
 			<return type="String" />
 			<description>
 			<description>

+ 5 - 3
doc/classes/TranslationServer.xml

@@ -27,13 +27,14 @@
 		<method name="get_loaded_locales" qualifiers="const">
 		<method name="get_loaded_locales" qualifiers="const">
 			<return type="Array" />
 			<return type="Array" />
 			<description>
 			<description>
-				Returns an Array of all loaded locales of the game.
+				Returns an array of all loaded locales of the project.
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="get_locale" qualifiers="const">
 		<method name="get_locale" qualifiers="const">
 			<return type="String" />
 			<return type="String" />
 			<description>
 			<description>
-				Returns the current locale of the game.
+				Returns the current locale of the project.
+				See also [method OS.get_locale] and [method OS.get_locale_language] to query the locale of the user system.
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="get_locale_name" qualifiers="const">
 		<method name="get_locale_name" qualifiers="const">
@@ -54,7 +55,8 @@
 			<return type="void" />
 			<return type="void" />
 			<argument index="0" name="locale" type="String" />
 			<argument index="0" name="locale" type="String" />
 			<description>
 			<description>
-				Sets the locale of the game.
+				Sets the locale of the project. The [code]locale[/code] string will be standardized to match known locales (e.g. [code]en-US[/code] would be matched to [code]en_US[/code]).
+				If translations have been loaded beforehand for the new locale, they will be applied.
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="translate" qualifiers="const">
 		<method name="translate" qualifiers="const">