1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-24 15:26:15 +00:00

Merge pull request #108862 from timothyqiu/plural-rules

Move context and plural support to `Translation`
This commit is contained in:
Thaddeus Crews
2025-10-15 16:31:07 -05:00
15 changed files with 678 additions and 478 deletions

View File

@@ -37,15 +37,6 @@
#include "core/os/os.h"
#include "core/string/locales.h"
Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;
HashMap<String, String> TranslationServer::language_map;
HashMap<String, String> TranslationServer::script_map;
HashMap<String, String> TranslationServer::locale_rename_map;
HashMap<String, String> TranslationServer::country_name_map;
HashMap<String, String> TranslationServer::variant_map;
HashMap<String, String> TranslationServer::country_rename_map;
void TranslationServer::init_locale_info() {
// Init locale info.
language_map.clear();
@@ -114,6 +105,18 @@ void TranslationServer::init_locale_info() {
}
idx++;
}
// Init plural rules.
plural_rules_map.clear();
idx = 0;
while (plural_rules[idx][0] != nullptr) {
const Vector<String> rule_locs = String(plural_rules[idx][0]).split(" ");
const String rule = String(plural_rules[idx][1]);
for (const String &l : rule_locs) {
plural_rules_map[l] = rule;
}
idx++;
}
}
TranslationServer::Locale::operator String() const {
@@ -306,6 +309,26 @@ String TranslationServer::get_locale_name(const String &p_locale) const {
return name;
}
String TranslationServer::get_plural_rules(const String &p_locale) const {
const String *rule = plural_rules_map.getptr(p_locale);
if (rule) {
return *rule;
}
Locale l = Locale(*this, p_locale, false);
if (!l.country.is_empty()) {
rule = plural_rules_map.getptr(l.language + "_" + l.country);
if (rule) {
return *rule;
}
}
rule = plural_rules_map.getptr(l.language);
if (rule) {
return *rule;
}
return String();
}
Vector<String> TranslationServer::get_all_languages() const {
Vector<String> languages;
@@ -585,6 +608,7 @@ void TranslationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);
ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);
ClassDB::bind_method(D_METHOD("get_plural_rules", "locale"), &TranslationServer::get_plural_rules);
ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));