From fdc21747bee08985a702aac074b39239bf014eb6 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Fri, 25 Nov 2022 18:54:37 +0800 Subject: [PATCH] Improve editor property capitalization * Captialize stop words when they are the last word. * Add stop words logic in `extract.py`. (cherry picked from commit c0e9d928e6b0b20442bb98246a43c53e667ce29d) --- editor/editor_property_name_processor.cpp | 6 ++++-- editor/translations/extract.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/editor/editor_property_name_processor.cpp b/editor/editor_property_name_processor.cpp index 0e83db73f96..1add0cfefe7 100644 --- a/editor/editor_property_name_processor.cpp +++ b/editor/editor_property_name_processor.cpp @@ -64,8 +64,8 @@ String EditorPropertyNameProcessor::_capitalize_name(const String &p_name) const Vector parts = p_name.split("_", false); for (int i = 0; i < parts.size(); i++) { - // Articles/conjunctions/prepositions which should only be capitalized if first word. - if (i != 0 && stop_words.find(parts[i]) != -1) { + // Articles/conjunctions/prepositions which should only be capitalized when not at beginning and end. + if (i > 0 && i + 1 < parts.size() && stop_words.find(parts[i]) != -1) { continue; } const Map::Element *remap = capitalize_string_remaps.find(parts[i]); @@ -261,6 +261,8 @@ EditorPropertyNameProcessor::EditorPropertyNameProcessor() { capitalize_string_remaps["yz"] = "YZ"; // Articles, conjunctions, prepositions. + // The following initialization is parsed in `editor/translations/extract.py` with a regex. + // The word definition format should be kept synced with the regex. stop_words.push_back("a"); stop_words.push_back("an"); stop_words.push_back("and"); diff --git a/editor/translations/extract.py b/editor/translations/extract.py index 927ad7951ca..b3d85c4ed8f 100755 --- a/editor/translations/extract.py +++ b/editor/translations/extract.py @@ -62,11 +62,17 @@ matches.sort() remaps = {} remap_re = re.compile(r'^\t*capitalize_string_remaps\["(?P.+)"\] = (String::utf8\()?"(?P.+)"') +stop_words = set() +stop_words_re = re.compile(r'^\t*stop_words\.push_back\("(?P.+)"\)') with open("editor/editor_property_name_processor.cpp") as f: for line in f: m = remap_re.search(line) if m: remaps[m.group("from")] = m.group("to") + else: + m = stop_words_re.search(line) + if m: + stop_words.add(m.group("word")) main_po = """ @@ -126,9 +132,12 @@ capitalize_re = re.compile(r"(?<=\D)(?=\d)|(?<=\d)(?=\D([a-z]|\d))") def _process_editor_string(name): # See EditorPropertyNameProcessor::process_string(). capitalized_parts = [] - for segment in name.split("_"): - if not segment: + parts = list(filter(bool, name.split("_"))) # Non-empty only. + for i, segment in enumerate(parts): + if i > 0 and i + 1 < len(parts) and segment in stop_words: + capitalized_parts.append(segment) continue + remapped = remaps.get(segment) if remapped: capitalized_parts.append(remapped)