Skip to content

Commit

Permalink
Expose standardize_locale add_default param publicly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dbnicholson committed Nov 12, 2024
1 parent 1bffd6c commit a71dad5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
41 changes: 41 additions & 0 deletions core/string/translation_server.compat.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**************************************************************************/
/* translation_server.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef DISABLE_DEPRECATED

String TranslationServer::_standardize_locale_compat_98972(const String &p_locale) const {
return standardize_locale(p_locale, false);
}

void TranslationServer::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::_standardize_locale_compat_98972);
}

#endif // DISABLE_DEPRECATED
13 changes: 5 additions & 8 deletions core/string/translation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/**************************************************************************/

#include "translation_server.h"
#include "translation_server.compat.inc"

#include "core/config/project_settings.h"
#include "core/io/resource_loader.h"
Expand Down Expand Up @@ -118,11 +119,7 @@ void TranslationServer::init_locale_info() {
}
}

String TranslationServer::standardize_locale(const String &p_locale) const {
return _standardize_locale(p_locale, false);
}

String TranslationServer::_standardize_locale(const String &p_locale, bool p_add_defaults) const {
String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {
// Replaces '-' with '_' for macOS style locales.
String univ_locale = p_locale.replace("-", "_");

Expand Down Expand Up @@ -234,8 +231,8 @@ int TranslationServer::compare_locales(const String &p_locale_a, const String &p
return *cached_result;
}

String locale_a = _standardize_locale(p_locale_a, true);
String locale_b = _standardize_locale(p_locale_b, true);
String locale_a = standardize_locale(p_locale_a, true);
String locale_b = standardize_locale(p_locale_b, true);

if (locale_a == locale_b) {
// Exact match.
Expand Down Expand Up @@ -578,7 +575,7 @@ void TranslationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);

ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);
ClassDB::bind_method(D_METHOD("standardize_locale", "locale"), &TranslationServer::standardize_locale);
ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));

ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);
ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);
Expand Down
8 changes: 6 additions & 2 deletions core/string/translation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ class TranslationServer : public Object {

static TranslationServer *singleton;
bool _load_translations(const String &p_from);
String _standardize_locale(const String &p_locale, bool p_add_defaults) const;

static void _bind_methods();

#ifndef DISABLE_DEPRECATED
String _standardize_locale_compat_98972(const String &p_locale) const;
static void _bind_compatibility_methods();
#endif

struct LocaleScriptInfo {
String name;
String script;
Expand Down Expand Up @@ -111,7 +115,7 @@ class TranslationServer : public Object {
void set_pseudolocalization_enabled(bool p_enabled);
void reload_pseudolocalization();

String standardize_locale(const String &p_locale) const;
String standardize_locale(const String &p_locale, bool p_add_defaults = false) const;

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

Expand Down
3 changes: 2 additions & 1 deletion doc/classes/TranslationServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@
<method name="standardize_locale" qualifiers="const">
<return type="String" />
<param index="0" name="locale" type="String" />
<param index="1" name="add_defaults" type="bool" default="false" />
<description>
Returns a [param locale] string standardized to match known locales (e.g. [code]en-US[/code] would be matched to [code]en_US[/code]).
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.
</description>
</method>
<method name="translate" qualifiers="const">
Expand Down
6 changes: 6 additions & 0 deletions misc/extension_api_validation/4.3-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ should instead be used to justify these changes and describe how users should wo
Add new entries at the end of the file.

## Changes between 4.3-stable and 4.4-stable
GH-98972
--------
Validate extension JSON: Error: Field 'classes/TranslationServer/methods/standardize_locale/arguments': size changed value in new API, from 1 to 2.

Optional argument added. Compatibility method registered.

GH-95374
--------
Validate extension JSON: Error: Field 'classes/ShapeCast2D/properties/collision_result': getter changed value in new API, from "_get_collision_result" to &"get_collision_result".
Expand Down
30 changes: 30 additions & 0 deletions tests/core/string/test_translation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ TEST_CASE("[TranslationServer] Locale operations") {
res = ts->standardize_locale(loc);

CHECK(res == "de_DE");

// No added defaults.
loc = "es_ES";
res = ts->standardize_locale(loc, true);

CHECK(res == "es_ES");

// Add default script.
loc = "az_AZ";
res = ts->standardize_locale(loc, true);

CHECK(res == "az_Latn_AZ");

// Add default country.
loc = "pa_Arab";
res = ts->standardize_locale(loc, true);

CHECK(res == "pa_Arab_PK");

// Add default script and country.
loc = "zh";
res = ts->standardize_locale(loc, true);

CHECK(res == "zh_Hans_CN");

// Explicitly don't add defaults.
loc = "zh";
res = ts->standardize_locale(loc, false);

CHECK(res == "zh");
}

TEST_CASE("[TranslationServer] Comparing locales") {
Expand Down

0 comments on commit a71dad5

Please sign in to comment.