summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-02-29 13:53:38 +0100
committerRémi Verschelde <rverschelde@gmail.com>2024-02-29 13:53:38 +0100
commit846428e0c6d819e5779c671192969f65ce585de3 (patch)
tree93b1e6bb55fdad243bb51e9409d890140f8a5f57 /core
parent1cc9190c70ac1a1b0e460353f291c1c661fafc9e (diff)
parentd70c45b5c85022fb02224235ff6df24ebf83dcf1 (diff)
downloadredot-engine-846428e0c6d819e5779c671192969f65ce585de3.tar.gz
Merge pull request #86222 from YeldhamDev/give_me_those_strings_baby
Add option to add built-in strings in the POT generation
Diffstat (limited to 'core')
-rw-r--r--core/config/project_settings.cpp1
-rw-r--r--core/object/object.cpp18
-rw-r--r--core/string/translation.cpp38
-rw-r--r--core/string/translation.h10
-rw-r--r--core/string/ustring.cpp21
-rw-r--r--core/string/ustring.h37
6 files changed, 99 insertions, 26 deletions
diff --git a/core/config/project_settings.cpp b/core/config/project_settings.cpp
index d9a5a5094a..d1c90ed7e6 100644
--- a/core/config/project_settings.cpp
+++ b/core/config/project_settings.cpp
@@ -1514,6 +1514,7 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF_INTERNAL("internationalization/locale/translation_remaps", PackedStringArray());
GLOBAL_DEF_INTERNAL("internationalization/locale/translations", PackedStringArray());
GLOBAL_DEF_INTERNAL("internationalization/locale/translations_pot_files", PackedStringArray());
+ GLOBAL_DEF_INTERNAL("internationalization/locale/translation_add_builtin_strings_to_pot", false);
ProjectSettings::get_singleton()->add_hidden_prefix("input/");
}
diff --git a/core/object/object.cpp b/core/object/object.cpp
index 5db1d2534f..d9a5713c20 100644
--- a/core/object/object.cpp
+++ b/core/object/object.cpp
@@ -1483,10 +1483,15 @@ String Object::tr(const StringName &p_message, const StringName &p_context) cons
}
if (Engine::get_singleton()->is_editor_hint()) {
+ String tr_msg = TranslationServer::get_singleton()->extractable_translate(p_message, p_context);
+ if (!tr_msg.is_empty()) {
+ return tr_msg;
+ }
+
return TranslationServer::get_singleton()->tool_translate(p_message, p_context);
- } else {
- return TranslationServer::get_singleton()->translate(p_message, p_context);
}
+
+ return TranslationServer::get_singleton()->translate(p_message, p_context);
}
String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
@@ -1499,10 +1504,15 @@ String Object::tr_n(const StringName &p_message, const StringName &p_message_plu
}
if (Engine::get_singleton()->is_editor_hint()) {
+ String tr_msg = TranslationServer::get_singleton()->extractable_translate_plural(p_message, p_message_plural, p_n, p_context);
+ if (!tr_msg.is_empty()) {
+ return tr_msg;
+ }
+
return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context);
- } else {
- return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
}
+
+ return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context);
}
void Object::_clear_internal_resource_paths(const Variant &p_var) {
diff --git a/core/string/translation.cpp b/core/string/translation.cpp
index db0c3f6006..2f25f56eac 100644
--- a/core/string/translation.cpp
+++ b/core/string/translation.cpp
@@ -772,6 +772,20 @@ StringName TranslationServer::tool_translate_plural(const StringName &p_message,
return p_message_plural;
}
+void TranslationServer::set_property_translation(const Ref<Translation> &p_translation) {
+ property_translation = p_translation;
+}
+
+StringName TranslationServer::property_translate(const StringName &p_message) const {
+ if (property_translation.is_valid()) {
+ StringName r = property_translation->get_message(p_message);
+ if (r) {
+ return r;
+ }
+ }
+ return p_message;
+}
+
void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
doc_translation = p_translation;
}
@@ -800,13 +814,13 @@ StringName TranslationServer::doc_translate_plural(const StringName &p_message,
return p_message_plural;
}
-void TranslationServer::set_property_translation(const Ref<Translation> &p_translation) {
- property_translation = p_translation;
+void TranslationServer::set_extractable_translation(const Ref<Translation> &p_translation) {
+ extractable_translation = p_translation;
}
-StringName TranslationServer::property_translate(const StringName &p_message) const {
- if (property_translation.is_valid()) {
- StringName r = property_translation->get_message(p_message);
+StringName TranslationServer::extractable_translate(const StringName &p_message, const StringName &p_context) const {
+ if (extractable_translation.is_valid()) {
+ StringName r = extractable_translation->get_message(p_message, p_context);
if (r) {
return r;
}
@@ -814,6 +828,20 @@ StringName TranslationServer::property_translate(const StringName &p_message) co
return p_message;
}
+StringName TranslationServer::extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
+ if (extractable_translation.is_valid()) {
+ StringName r = extractable_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
+ if (r) {
+ return r;
+ }
+ }
+
+ if (p_n == 1) {
+ return p_message;
+ }
+ return p_message_plural;
+}
+
bool TranslationServer::is_pseudolocalization_enabled() const {
return pseudolocalization_enabled;
}
diff --git a/core/string/translation.h b/core/string/translation.h
index 4eca37f6be..bd7082dc9d 100644
--- a/core/string/translation.h
+++ b/core/string/translation.h
@@ -82,8 +82,9 @@ class TranslationServer : public Object {
HashSet<Ref<Translation>> translations;
Ref<Translation> tool_translation;
- Ref<Translation> doc_translation;
Ref<Translation> property_translation;
+ Ref<Translation> doc_translation;
+ Ref<Translation> extractable_translation;
bool enabled = true;
@@ -181,11 +182,14 @@ public:
Ref<Translation> get_tool_translation() const;
StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
+ void set_property_translation(const Ref<Translation> &p_translation);
+ StringName property_translate(const StringName &p_message) const;
void set_doc_translation(const Ref<Translation> &p_translation);
StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
- void set_property_translation(const Ref<Translation> &p_translation);
- StringName property_translate(const StringName &p_message) const;
+ void set_extractable_translation(const Ref<Translation> &p_translation);
+ StringName extractable_translate(const StringName &p_message, const StringName &p_context = "") const;
+ StringName extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
void setup();
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index a8a96e6e3f..f4b00255a1 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -5391,9 +5391,7 @@ String DTRN(const String &p_text, const String &p_text_plural, int p_n, const St
/**
* "Run-time TRanslate". Performs string replacement for internationalization
- * within a running project. The translation string must be supplied by the
- * project, as Godot does not provide built-in translations for `RTR()` strings
- * to keep binary size low. A translation context can optionally be specified to
+ * without the editor. A translation context can optionally be specified to
* disambiguate between identical source strings in translations. When
* placeholders are desired, use `vformat(RTR("Example: %s"), some_string)`.
* If a string mentions a quantity (and may therefore need a dynamic plural form),
@@ -5407,9 +5405,8 @@ String RTR(const String &p_text, const String &p_context) {
String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
if (rtr.is_empty() || rtr == p_text) {
return TranslationServer::get_singleton()->translate(p_text, p_context);
- } else {
- return rtr;
}
+ return rtr;
}
return p_text;
@@ -5417,13 +5414,10 @@ String RTR(const String &p_text, const String &p_context) {
/**
* "Run-time TRanslate for N items". Performs string replacement for
- * internationalization within a running project. The translation string must be
- * supplied by the project, as Godot does not provide built-in translations for
- * `RTRN()` strings to keep binary size low. A translation context can
- * optionally be specified to disambiguate between identical source strings in
- * translations. Use `RTR()` if the string doesn't need dynamic plural form.
- * When placeholders are desired, use
- * `vformat(RTRN("%d item", "%d items", some_integer), some_integer)`.
+ * internationalization without the editor. A translation context can optionally
+ * be specified to disambiguate between identical source strings in translations.
+ * Use `RTR()` if the string doesn't need dynamic plural form. When placeholders
+ * are desired, use `vformat(RTRN("%d item", "%d items", some_integer), some_integer)`.
* The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
*
* NOTE: Do not use `RTRN()` in editor-only code (typically within the `editor/`
@@ -5434,9 +5428,8 @@ String RTRN(const String &p_text, const String &p_text_plural, int p_n, const St
String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
if (rtr.is_empty() || rtr == p_text || rtr == p_text_plural) {
return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
- } else {
- return rtr;
}
+ return rtr;
}
// Return message based on English plural rule if translation is not possible.
diff --git a/core/string/ustring.h b/core/string/ustring.h
index b1348ceb48..468a015302 100644
--- a/core/string/ustring.h
+++ b/core/string/ustring.h
@@ -556,6 +556,43 @@ String DTRN(const String &p_text, const String &p_text_plural, int p_n, const St
String RTR(const String &p_text, const String &p_context = "");
String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "");
+/**
+ * "Extractable TRanslate". Used for strings that can appear inside an exported
+ * project (such as the ones in nodes like `FileDialog`), which are made possible
+ * to add in the POT generator. A translation context can optionally be specified
+ * to disambiguate between identical source strings in translations.
+ * When placeholders are desired, use vformat(ETR("Example: %s"), some_string)`.
+ * If a string mentions a quantity (and may therefore need a dynamic plural form),
+ * use `ETRN()` instead of `ETR()`.
+ *
+ * NOTE: This function is for string extraction only, and will just return the
+ * string it was given. The translation itself should be done internally by nodes
+ * with `atr()` instead.
+ */
+_FORCE_INLINE_ String ETR(const String &p_text, const String &p_context = "") {
+ return p_text;
+}
+
+/**
+ * "Extractable TRanslate for N items". Used for strings that can appear inside an
+ * exported project (such as the ones in nodes like `FileDialog`), which are made
+ * possible to add in the POT generator. A translation context can optionally be
+ * specified to disambiguate between identical source strings in translations.
+ * Use `ETR()` if the string doesn't need dynamic plural form. When placeholders
+ * are desired, use `vformat(ETRN("%d item", "%d items", some_integer), some_integer)`.
+ * The placeholder must be present in both strings to avoid run-time warnings in `vformat()`.
+ *
+ * NOTE: This function is for string extraction only, and will just return the
+ * string it was given. The translation itself should be done internally by nodes
+ * with `atr()` instead.
+ */
+_FORCE_INLINE_ String ETRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context = "") {
+ if (p_n == 1) {
+ return p_text;
+ }
+ return p_text_plural;
+}
+
bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end);
_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) {