Skip to content

Commit

Permalink
Merge pull request #98972 from dbnicholson/standardize-add-defaults
Browse files Browse the repository at this point in the history
Expose `TranslationServer::standardize_locale` `add_default` param publicly
  • Loading branch information
Repiteo committed Nov 21, 2024
2 parents 0d8352b + 6f4fadf commit f952bfe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 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_bind_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_bind_compat_98972);
}

#endif // DISABLE_DEPRECATED
7 changes: 4 additions & 3 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 @@ -218,8 +219,8 @@ TranslationServer::Locale::Locale(const TranslationServer &p_server, const Strin
}
}

String TranslationServer::standardize_locale(const String &p_locale) const {
return Locale(*this, p_locale, false).operator String();
String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {
return Locale(*this, p_locale, p_add_defaults).operator String();
}

int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {
Expand Down Expand Up @@ -591,7 +592,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 inline TranslationServer *singleton = nullptr;
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_bind_compat_98972(const String &p_locale) const;
static void _bind_compatibility_methods();
#endif

struct LocaleScriptInfo {
String name;
String script;
Expand Down Expand Up @@ -129,7 +133,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
7 changes: 7 additions & 0 deletions misc/extension_api_validation/4.3-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,10 @@ GH-98918
Validate extension JSON: Error: Field 'classes/FileAccess/methods/open_encrypted/arguments': size changed value in new API, from 3 to 4.

Optional argument added to allow setting initialization vector. Compatibility method registered.


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.
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 @@ -104,6 +104,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 f952bfe

Please sign in to comment.