Skip to content

Commit 6f4fadf

Browse files
committed
Expose standardize_locale add_default param publicly
Comparing locales can have surprising outcomes since it standardizes locales with defaults. For example, zh and zh_CN result in an exact match since the defaults change them both to zh_Hans_CN. Expose the add_default parameter publicly with a default of false so the fully standardized locale can be inspected.
1 parent fd4c29a commit 6f4fadf

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* translation_server.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
String TranslationServer::_standardize_locale_bind_compat_98972(const String &p_locale) const {
34+
return standardize_locale(p_locale, false);
35+
}
36+
37+
void TranslationServer::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::_standardize_locale_bind_compat_98972);
39+
}
40+
41+
#endif // DISABLE_DEPRECATED

core/string/translation_server.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "translation_server.h"
32+
#include "translation_server.compat.inc"
3233

3334
#include "core/config/project_settings.h"
3435
#include "core/io/resource_loader.h"
@@ -218,8 +219,8 @@ TranslationServer::Locale::Locale(const TranslationServer &p_server, const Strin
218219
}
219220
}
220221

221-
String TranslationServer::standardize_locale(const String &p_locale) const {
222-
return Locale(*this, p_locale, false).operator String();
222+
String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {
223+
return Locale(*this, p_locale, p_add_defaults).operator String();
223224
}
224225

225226
int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
@@ -591,7 +592,7 @@ void TranslationServer::_bind_methods() {
591592
ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);
592593

593594
ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
594-
ClassDB::bind_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::standardize_locale);
595+
ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));
595596

596597
ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
597598
ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);

core/string/translation_server.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ class TranslationServer : public Object {
5252

5353
static inline TranslationServer *singleton = nullptr;
5454
bool _load_translations(const String &p_from);
55-
String _standardize_locale(const String &p_locale, bool p_add_defaults) const;
5655

5756
static void _bind_methods();
5857

58+
#ifndef DISABLE_DEPRECATED
59+
String _standardize_locale_bind_compat_98972(const String &p_locale) const;
60+
static void _bind_compatibility_methods();
61+
#endif
62+
5963
struct LocaleScriptInfo {
6064
String name;
6165
String script;
@@ -129,7 +133,7 @@ class TranslationServer : public Object {
129133
void set_pseudolocalization_enabled(bool p_enabled);
130134
void reload_pseudolocalization();
131135

132-
String standardize_locale(const String &p_locale) const;
136+
String standardize_locale(const String &p_locale, bool p_add_defaults = false) const;
133137

134138
int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
135139

doc/classes/TranslationServer.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@
160160
<method name="standardize_locale" qualifiers="const">
161161
<return type="String" />
162162
<param index="0" name="locale" type="String" />
163+
<param index="1" name="add_defaults" type="bool" default="false" />
163164
<description>
164-
Returns a [param locale] string standardized to match known locales (e.g. [code]en-US[/code] would be matched to [code]en_US[/code]).
165+
Returns a [param locale] string standardized to match known locales (e.g. [code]en-US[/code] would be matched to [code]en_US[/code]). If [param add_defaults] is [code]true[/code], the locale may have a default script or country added.
165166
</description>
166167
</method>
167168
<method name="translate" qualifiers="const">

misc/extension_api_validation/4.3-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,10 @@ GH-98918
122122
Validate extension JSON: Error: Field 'classes/FileAccess/methods/open_encrypted/arguments': size changed value in new API, from 3 to 4.
123123

124124
Optional argument added to allow setting initialization vector. Compatibility method registered.
125+
126+
127+
GH-98972
128+
--------
129+
Validate extension JSON: Error: Field 'classes/TranslationServer/methods/standardize_locale/arguments': size changed value in new API, from 1 to 2.
130+
131+
Optional argument added. Compatibility method registered.

tests/core/string/test_translation_server.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ TEST_CASE("[TranslationServer] Locale operations") {
104104
res = ts->standardize_locale(loc);
105105

106106
CHECK(res == "de_DE");
107+
108+
// No added defaults.
109+
loc = "es_ES";
110+
res = ts->standardize_locale(loc, true);
111+
112+
CHECK(res == "es_ES");
113+
114+
// Add default script.
115+
loc = "az_AZ";
116+
res = ts->standardize_locale(loc, true);
117+
118+
CHECK(res == "az_Latn_AZ");
119+
120+
// Add default country.
121+
loc = "pa_Arab";
122+
res = ts->standardize_locale(loc, true);
123+
124+
CHECK(res == "pa_Arab_PK");
125+
126+
// Add default script and country.
127+
loc = "zh";
128+
res = ts->standardize_locale(loc, true);
129+
130+
CHECK(res == "zh_Hans_CN");
131+
132+
// Explicitly don't add defaults.
133+
loc = "zh";
134+
res = ts->standardize_locale(loc, false);
135+
136+
CHECK(res == "zh");
107137
}
108138

109139
TEST_CASE("[TranslationServer] Comparing locales") {

0 commit comments

Comments
 (0)