Skip to content

Commit

Permalink
Merge branch 'godotengine:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Bioblaze authored Oct 4, 2024
2 parents 025b594 + 32239d4 commit bac64b2
Show file tree
Hide file tree
Showing 59 changed files with 2,875 additions and 376 deletions.
292 changes: 290 additions & 2 deletions core/string/translation_domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,170 @@
#include "core/string/translation.h"
#include "core/string/translation_server.h"

struct _character_accent_pair {
const char32_t character;
const char32_t *accented_character;
};

static _character_accent_pair _character_to_accented[] = {
{ 'A', U"Å" },
{ 'B', U"ß" },
{ 'C', U"Ç" },
{ 'D', U"Ð" },
{ 'E', U"É" },
{ 'F', U"" },
{ 'G', U"Ĝ" },
{ 'H', U"Ĥ" },
{ 'I', U"Ĩ" },
{ 'J', U"Ĵ" },
{ 'K', U"ĸ" },
{ 'L', U"Ł" },
{ 'M', U"" },
{ 'N', U"й" },
{ 'O', U"Ö" },
{ 'P', U"" },
{ 'Q', U"" },
{ 'R', U"Ř" },
{ 'S', U"Ŝ" },
{ 'T', U"Ŧ" },
{ 'U', U"Ũ" },
{ 'V', U"" },
{ 'W', U"Ŵ" },
{ 'X', U"" },
{ 'Y', U"Ÿ" },
{ 'Z', U"Ž" },
{ 'a', U"á" },
{ 'b', U"" },
{ 'c', U"ć" },
{ 'd', U"" },
{ 'e', U"é" },
{ 'f', U"" },
{ 'g', U"ǵ" },
{ 'h', U"" },
{ 'i', U"í" },
{ 'j', U"ǰ" },
{ 'k', U"" },
{ 'l', U"ł" },
{ 'm', U"" },
{ 'n', U"" },
{ 'o', U"ô" },
{ 'p', U"" },
{ 'q', U"" },
{ 'r', U"ŕ" },
{ 's', U"š" },
{ 't', U"ŧ" },
{ 'u', U"ü" },
{ 'v', U"" },
{ 'w', U"ŵ" },
{ 'x', U"" },
{ 'y', U"ý" },
{ 'z', U"ź" },
};

String TranslationDomain::_get_override_string(const String &p_message) const {
String res;
for (int i = 0; i < p_message.length(); i++) {
if (pseudolocalization.skip_placeholders_enabled && _is_placeholder(p_message, i)) {
res += p_message[i];
res += p_message[i + 1];
i++;
continue;
}
res += '*';
}
return res;
}

String TranslationDomain::_double_vowels(const String &p_message) const {
String res;
for (int i = 0; i < p_message.length(); i++) {
if (pseudolocalization.skip_placeholders_enabled && _is_placeholder(p_message, i)) {
res += p_message[i];
res += p_message[i + 1];
i++;
continue;
}
res += p_message[i];
if (p_message[i] == 'a' || p_message[i] == 'e' || p_message[i] == 'i' || p_message[i] == 'o' || p_message[i] == 'u' ||
p_message[i] == 'A' || p_message[i] == 'E' || p_message[i] == 'I' || p_message[i] == 'O' || p_message[i] == 'U') {
res += p_message[i];
}
}
return res;
};

String TranslationDomain::_replace_with_accented_string(const String &p_message) const {
String res;
for (int i = 0; i < p_message.length(); i++) {
if (pseudolocalization.skip_placeholders_enabled && _is_placeholder(p_message, i)) {
res += p_message[i];
res += p_message[i + 1];
i++;
continue;
}
const char32_t *accented = _get_accented_version(p_message[i]);
if (accented) {
res += accented;
} else {
res += p_message[i];
}
}
return res;
}

String TranslationDomain::_wrap_with_fakebidi_characters(const String &p_message) const {
String res;
char32_t fakebidiprefix = U'\u202e';
char32_t fakebidisuffix = U'\u202c';
res += fakebidiprefix;
// The fake bidi unicode gets popped at every newline so pushing it back at every newline.
for (int i = 0; i < p_message.length(); i++) {
if (p_message[i] == '\n') {
res += fakebidisuffix;
res += p_message[i];
res += fakebidiprefix;
} else if (pseudolocalization.skip_placeholders_enabled && _is_placeholder(p_message, i)) {
res += fakebidisuffix;
res += p_message[i];
res += p_message[i + 1];
res += fakebidiprefix;
i++;
} else {
res += p_message[i];
}
}
res += fakebidisuffix;
return res;
}

String TranslationDomain::_add_padding(const String &p_message, int p_length) const {
String underscores = String("_").repeat(p_length * pseudolocalization.expansion_ratio / 2);
String prefix = pseudolocalization.prefix + underscores;
String suffix = underscores + pseudolocalization.suffix;

return prefix + p_message + suffix;
}

const char32_t *TranslationDomain::_get_accented_version(char32_t p_character) const {
if (!is_ascii_alphabet_char(p_character)) {
return nullptr;
}

for (unsigned int i = 0; i < sizeof(_character_to_accented) / sizeof(_character_to_accented[0]); i++) {
if (_character_to_accented[i].character == p_character) {
return _character_to_accented[i].accented_character;
}
}

return nullptr;
}

bool TranslationDomain::_is_placeholder(const String &p_message, int p_index) const {
return p_index < p_message.length() - 1 && p_message[p_index] == '%' &&
(p_message[p_index + 1] == 's' || p_message[p_index + 1] == 'c' || p_message[p_index + 1] == 'd' ||
p_message[p_index + 1] == 'o' || p_message[p_index + 1] == 'x' || p_message[p_index + 1] == 'X' || p_message[p_index + 1] == 'f');
}

StringName TranslationDomain::get_message_from_translations(const String &p_locale, const StringName &p_message, const StringName &p_context) const {
StringName res;
int best_score = 0;
Expand Down Expand Up @@ -129,9 +293,9 @@ StringName TranslationDomain::translate(const StringName &p_message, const Strin
}

if (!res) {
return p_message;
return pseudolocalization.enabled ? pseudolocalize(p_message) : p_message;
}
return res;
return pseudolocalization.enabled ? pseudolocalize(res) : res;
}

StringName TranslationDomain::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
Expand All @@ -152,13 +316,137 @@ StringName TranslationDomain::translate_plural(const StringName &p_message, cons
return res;
}

bool TranslationDomain::is_pseudolocalization_enabled() const {
return pseudolocalization.enabled;
}

void TranslationDomain::set_pseudolocalization_enabled(bool p_enabled) {
pseudolocalization.enabled = p_enabled;
}

bool TranslationDomain::is_pseudolocalization_accents_enabled() const {
return pseudolocalization.accents_enabled;
}

void TranslationDomain::set_pseudolocalization_accents_enabled(bool p_enabled) {
pseudolocalization.accents_enabled = p_enabled;
}

bool TranslationDomain::is_pseudolocalization_double_vowels_enabled() const {
return pseudolocalization.double_vowels_enabled;
}

void TranslationDomain::set_pseudolocalization_double_vowels_enabled(bool p_enabled) {
pseudolocalization.double_vowels_enabled = p_enabled;
}

bool TranslationDomain::is_pseudolocalization_fake_bidi_enabled() const {
return pseudolocalization.fake_bidi_enabled;
}

void TranslationDomain::set_pseudolocalization_fake_bidi_enabled(bool p_enabled) {
pseudolocalization.fake_bidi_enabled = p_enabled;
}

bool TranslationDomain::is_pseudolocalization_override_enabled() const {
return pseudolocalization.override_enabled;
}

void TranslationDomain::set_pseudolocalization_override_enabled(bool p_enabled) {
pseudolocalization.override_enabled = p_enabled;
}

bool TranslationDomain::is_pseudolocalization_skip_placeholders_enabled() const {
return pseudolocalization.skip_placeholders_enabled;
}

void TranslationDomain::set_pseudolocalization_skip_placeholders_enabled(bool p_enabled) {
pseudolocalization.skip_placeholders_enabled = p_enabled;
}

float TranslationDomain::get_pseudolocalization_expansion_ratio() const {
return pseudolocalization.expansion_ratio;
}

void TranslationDomain::set_pseudolocalization_expansion_ratio(float p_ratio) {
pseudolocalization.expansion_ratio = p_ratio;
}

String TranslationDomain::get_pseudolocalization_prefix() const {
return pseudolocalization.prefix;
}

void TranslationDomain::set_pseudolocalization_prefix(const String &p_prefix) {
pseudolocalization.prefix = p_prefix;
}

String TranslationDomain::get_pseudolocalization_suffix() const {
return pseudolocalization.suffix;
}

void TranslationDomain::set_pseudolocalization_suffix(const String &p_suffix) {
pseudolocalization.suffix = p_suffix;
}

StringName TranslationDomain::pseudolocalize(const StringName &p_message) const {
String message = p_message;
int length = message.length();
if (pseudolocalization.override_enabled) {
message = _get_override_string(message);
}

if (pseudolocalization.double_vowels_enabled) {
message = _double_vowels(message);
}

if (pseudolocalization.accents_enabled) {
message = _replace_with_accented_string(message);
}

if (pseudolocalization.fake_bidi_enabled) {
message = _wrap_with_fakebidi_characters(message);
}

return _add_padding(message, length);
}

void TranslationDomain::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationDomain::get_translation_object);
ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationDomain::add_translation);
ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationDomain::remove_translation);
ClassDB::bind_method(D_METHOD("clear"), &TranslationDomain::clear);
ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationDomain::translate, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("translate_plural", "message", "message_plural", "n", "context"), &TranslationDomain::translate_plural, DEFVAL(StringName()));

ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationDomain::is_pseudolocalization_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_enabled);
ClassDB::bind_method(D_METHOD("is_pseudolocalization_accents_enabled"), &TranslationDomain::is_pseudolocalization_accents_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_accents_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_accents_enabled);
ClassDB::bind_method(D_METHOD("is_pseudolocalization_double_vowels_enabled"), &TranslationDomain::is_pseudolocalization_double_vowels_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_double_vowels_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_double_vowels_enabled);
ClassDB::bind_method(D_METHOD("is_pseudolocalization_fake_bidi_enabled"), &TranslationDomain::is_pseudolocalization_fake_bidi_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_fake_bidi_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_fake_bidi_enabled);
ClassDB::bind_method(D_METHOD("is_pseudolocalization_override_enabled"), &TranslationDomain::is_pseudolocalization_override_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_override_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_override_enabled);
ClassDB::bind_method(D_METHOD("is_pseudolocalization_skip_placeholders_enabled"), &TranslationDomain::is_pseudolocalization_skip_placeholders_enabled);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_skip_placeholders_enabled", "enabled"), &TranslationDomain::set_pseudolocalization_skip_placeholders_enabled);
ClassDB::bind_method(D_METHOD("get_pseudolocalization_expansion_ratio"), &TranslationDomain::get_pseudolocalization_expansion_ratio);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_expansion_ratio", "ratio"), &TranslationDomain::set_pseudolocalization_expansion_ratio);
ClassDB::bind_method(D_METHOD("get_pseudolocalization_prefix"), &TranslationDomain::get_pseudolocalization_prefix);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_prefix", "prefix"), &TranslationDomain::set_pseudolocalization_prefix);
ClassDB::bind_method(D_METHOD("get_pseudolocalization_suffix"), &TranslationDomain::get_pseudolocalization_suffix);
ClassDB::bind_method(D_METHOD("set_pseudolocalization_suffix", "suffix"), &TranslationDomain::set_pseudolocalization_suffix);
ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationDomain::pseudolocalize);

ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_accents_enabled"), "set_pseudolocalization_accents_enabled", "is_pseudolocalization_accents_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_double_vowels_enabled"), "set_pseudolocalization_double_vowels_enabled", "is_pseudolocalization_double_vowels_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_fake_bidi_enabled"), "set_pseudolocalization_fake_bidi_enabled", "is_pseudolocalization_fake_bidi_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_override_enabled"), "set_pseudolocalization_override_enabled", "is_pseudolocalization_override_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_skip_placeholders_enabled"), "set_pseudolocalization_skip_placeholders_enabled", "is_pseudolocalization_skip_placeholders_enabled");
ADD_PROPERTY(PropertyInfo(Variant::Type::FLOAT, "pseudolocalization_expansion_ratio"), "set_pseudolocalization_expansion_ratio", "get_pseudolocalization_expansion_ratio");
ADD_PROPERTY(PropertyInfo(Variant::Type::STRING, "pseudolocalization_prefix"), "set_pseudolocalization_prefix", "get_pseudolocalization_prefix");
ADD_PROPERTY(PropertyInfo(Variant::Type::STRING, "pseudolocalization_suffix"), "set_pseudolocalization_suffix", "get_pseudolocalization_suffix");
}

TranslationDomain::TranslationDomain() {
Expand Down
42 changes: 42 additions & 0 deletions core/string/translation_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,28 @@ class Translation;
class TranslationDomain : public RefCounted {
GDCLASS(TranslationDomain, RefCounted);

struct PseudolocalizationConfig {
bool enabled = false;
bool accents_enabled = true;
bool double_vowels_enabled = false;
bool fake_bidi_enabled = false;
bool override_enabled = false;
bool skip_placeholders_enabled = true;
float expansion_ratio = 0.0;
String prefix = "[";
String suffix = "]";
};

HashSet<Ref<Translation>> translations;
PseudolocalizationConfig pseudolocalization;

String _get_override_string(const String &p_message) const;
String _double_vowels(const String &p_message) const;
String _replace_with_accented_string(const String &p_message) const;
String _wrap_with_fakebidi_characters(const String &p_message) const;
String _add_padding(const String &p_message, int p_length) const;
const char32_t *_get_accented_version(char32_t p_character) const;
bool _is_placeholder(const String &p_message, int p_index) const;

protected:
static void _bind_methods();
Expand All @@ -59,6 +80,27 @@ class TranslationDomain : public RefCounted {
StringName translate(const StringName &p_message, const StringName &p_context) const;
StringName translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const;

bool is_pseudolocalization_enabled() const;
void set_pseudolocalization_enabled(bool p_enabled);
bool is_pseudolocalization_accents_enabled() const;
void set_pseudolocalization_accents_enabled(bool p_enabled);
bool is_pseudolocalization_double_vowels_enabled() const;
void set_pseudolocalization_double_vowels_enabled(bool p_enabled);
bool is_pseudolocalization_fake_bidi_enabled() const;
void set_pseudolocalization_fake_bidi_enabled(bool p_enabled);
bool is_pseudolocalization_override_enabled() const;
void set_pseudolocalization_override_enabled(bool p_enabled);
bool is_pseudolocalization_skip_placeholders_enabled() const;
void set_pseudolocalization_skip_placeholders_enabled(bool p_enabled);
float get_pseudolocalization_expansion_ratio() const;
void set_pseudolocalization_expansion_ratio(float p_ratio);
String get_pseudolocalization_prefix() const;
void set_pseudolocalization_prefix(const String &p_prefix);
String get_pseudolocalization_suffix() const;
void set_pseudolocalization_suffix(const String &p_suffix);

StringName pseudolocalize(const StringName &p_message) const;

TranslationDomain();
};

Expand Down
Loading

0 comments on commit bac64b2

Please sign in to comment.