Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] i18n: Add support for translating the class reference #53511

Merged
merged 7 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions core/io/translation_loader_po.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,23 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
}

l = l.substr(1, l.length());
//find final quote
// Find final quote, ignoring escaped ones (\").
// The escape_next logic is necessary to properly parse things like \\"
// where the blackslash is the one being escaped, not the quote.
int end_pos = -1;
bool escape_next = false;
for (int i = 0; i < l.length(); i++) {
if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) {
if (l[i] == '\\' && !escape_next) {
escape_next = true;
continue;
}

if (l[i] == '"' && !escape_next) {
end_pos = i;
break;
}

escape_next = false;
}

if (end_pos == -1) {
Expand Down
2 changes: 1 addition & 1 deletion core/io/translation_loader_po.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

class TranslationLoaderPO : public ResourceFormatLoader {
public:
static RES load_translation(FileAccess *f, Error *r_error);
static RES load_translation(FileAccess *f, Error *r_error = nullptr);
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
Expand Down
16 changes: 13 additions & 3 deletions core/translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,6 @@ void TranslationServer::setup() {
}
fallback = GLOBAL_DEF("locale/fallback", "en");
#ifdef TOOLS_ENABLED

{
String options = "";
int idx = 0;
Expand All @@ -1194,7 +1193,6 @@ void TranslationServer::setup() {
ProjectSettings::get_singleton()->set_custom_property_info("locale/fallback", PropertyInfo(Variant::STRING, "locale/fallback", PROPERTY_HINT_ENUM, options));
}
#endif
//load translations
}

void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
Expand All @@ -1204,12 +1202,24 @@ void TranslationServer::set_tool_translation(const Ref<Translation> &p_translati
StringName TranslationServer::tool_translate(const StringName &p_message) const {
if (tool_translation.is_valid()) {
StringName r = tool_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;
}

StringName TranslationServer::doc_translate(const StringName &p_message) const {
if (doc_translation.is_valid()) {
StringName r = doc_translation->get_message(p_message);
if (r) {
return r;
}
}
return p_message;
}

Expand Down
3 changes: 3 additions & 0 deletions core/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class TranslationServer : public Object {

Set<Ref<Translation>> translations;
Ref<Translation> tool_translation;
Ref<Translation> doc_translation;

Map<String, String> locale_name_map;

Expand Down Expand Up @@ -107,6 +108,8 @@ class TranslationServer : public Object {

void set_tool_translation(const Ref<Translation> &p_translation);
StringName tool_translate(const StringName &p_message) const;
void set_doc_translation(const Ref<Translation> &p_translation);
StringName doc_translate(const StringName &p_message) const;

void setup();

Expand Down
10 changes: 10 additions & 0 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4469,6 +4469,16 @@ String TTR(const String &p_text) {
return p_text;
}

String DTR(const String &p_text) {
// Comes straight from the XML, so remove indentation and any trailing whitespace.
const String text = p_text.dedent().strip_edges();

if (TranslationServer::get_singleton()) {
return TranslationServer::get_singleton()->doc_translate(text);
}

return text;
}
#endif

String RTR(const String &p_text) {
Expand Down
16 changes: 8 additions & 8 deletions core/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,25 +414,25 @@ _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {

/* end of namespace */

//tool translate
// Tool translate (TTR and variants) for the editor UI,
// and doc translate for the class reference (DTR).
#ifdef TOOLS_ENABLED

//gets parsed
// Gets parsed.
String TTR(const String &);
//use for C strings
String DTR(const String &);
// Use for C strings.
#define TTRC(m_value) (m_value)
//use to avoid parsing (for use later with C strings)
// Use to avoid parsing (for use later with C strings).
#define TTRGET(m_value) TTR(m_value)

#else

#define TTR(m_value) (String())
#define DTR(m_value) (String())
#define TTRC(m_value) (m_value)
#define TTRGET(m_value) (m_value)

#endif

//tool or regular translate
// Runtime translate for the public node API.
String RTR(const String &);

bool is_symbol(CharType c);
Expand Down
23 changes: 23 additions & 0 deletions doc/translations/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Makefile providing various facilities to manage translations

TEMPLATE = classes.pot
POFILES = $(wildcard *.po)
LANGS = $(POFILES:%.po=%)

all: update merge

update:
@cd ../..; \
python3 doc/translations/extract.py \
--path doc/classes modules/*/doc_classes \
--output doc/translations/$(TEMPLATE)

merge:
@for po in $(POFILES); do \
echo -e "\nMerging $$po..."; \
msgmerge -w 79 -C $$po $$po $(TEMPLATE) > "$$po".new; \
mv -f "$$po".new $$po; \
done

check:
@for po in $(POFILES); do msgfmt -c $$po -o /dev/null; done
Loading