Add context support for editor property name i18n

Also:
* Removed deprecated code that remains in `extract_editor.py` after the
  script split
* Fixed borken remap extraction due to our migration from
  `String::utf8("text")` to `U"text"`
This commit is contained in:
Haoyu Qiu
2023-10-06 19:36:51 +08:00
parent 73d5be8f19
commit c74e582138
3 changed files with 44 additions and 52 deletions

View File

@@ -47,29 +47,32 @@ class ExtractType(enum.IntEnum):
class PropertyNameProcessor:
remaps: Dict[str, str] = {}
stop_words: Set[str] = set()
contexts: Dict[str, Dict[str, str]] = {}
# See String::_camelcase_to_underscore().
capitalize_re = re.compile(r"(?<=\D)(?=\d)|(?<=\d)(?=\D([a-z]|\d))")
def __init__(self):
remap_re = re.compile(r'^\t*capitalize_string_remaps\["(?P<from>.+)"\] = (String::utf8\()?"(?P<to>.+)"')
remap_re = re.compile(r'^\t*capitalize_string_remaps\["(?P<from>.+)"\] = (String::utf8\(|U)?"(?P<to>.+)"')
stop_words_re = re.compile(r'^\t*"(?P<word>.+)",')
is_inside_stop_words = False
contexts_re = re.compile(r'^\t*translation_contexts\["(?P<message>.+)"\]\["(?P<condition>.+)"\] = (String::utf8\(|U)?"(?P<context>.+)"')
with open("editor/editor_property_name_processor.cpp") as f:
for line in f:
if is_inside_stop_words:
m = stop_words_re.search(line)
if m:
self.stop_words.add(m.group("word"))
else:
is_inside_stop_words = False
else:
m = remap_re.search(line)
if m:
self.remaps[m.group("from")] = m.group("to")
m = remap_re.search(line)
if m:
self.remaps[m.group("from")] = m.group("to")
continue
if not is_inside_stop_words and not self.stop_words:
is_inside_stop_words = "stop_words = " in line
m = stop_words_re.search(line)
if m:
self.stop_words.add(m.group("word"))
continue
m = contexts_re.search(line)
if m:
context_map = self.contexts.setdefault(m.group("message"), {})
context_map[m.group("condition")] = m.group("context")
continue
def process_name(self, name: str) -> str:
# See EditorPropertyNameProcessor::process_name().
@@ -94,6 +97,18 @@ class PropertyNameProcessor:
return " ".join(capitalized_parts)
def get_context(self, name: str, property: str, klass: str) -> str:
# See EditorPropertyNameProcessor::_get_context().
if not property and not klass:
return ""
context_map = self.contexts.get(name)
if not context_map:
return ""
context = context_map.get(property)
if not context and klass:
context = context_map.get(klass + "::" + property)
return context or ""
def get_source_files() -> List[str]:
matches = []

View File

@@ -58,7 +58,6 @@ message_patterns = {
r'RTRN\(U?"(?P<message>([^"\\]|\\.)*)", "(?P<plural_message>([^"\\]|\\.)*)",[^,)]+?(, "(?P<context>([^"\\]|\\.)*)")?\)'
): ExtractType.TEXT,
}
theme_property_patterns: Dict[re.Pattern[str], ExtractType] = {}
def _is_block_translator_comment(translator_line):
@@ -104,12 +103,7 @@ def process_file(f, fname):
reading_translator_comment = False
is_block_translator_comment = False
translator_comment = ""
current_group = ""
current_subgroup = ""
patterns = message_patterns
if os.path.basename(fname) == "default_theme.cpp":
patterns = {**message_patterns, **theme_property_patterns}
while l:
@@ -141,37 +135,6 @@ def process_file(f, fname):
if extract_type == ExtractType.TEXT:
_add_message(msg, msg_plural, msgctx, location, translator_comment)
elif extract_type == ExtractType.PROPERTY_PATH:
if captures.get("usage") == "PROPERTY_USAGE_NO_EDITOR":
continue
if current_subgroup:
if msg.startswith(current_subgroup):
msg = msg[len(current_subgroup) :]
elif current_subgroup.startswith(msg):
pass # Keep this as-is. See EditorInspector::update_tree().
else:
current_subgroup = ""
elif current_group:
if msg.startswith(current_group):
msg = msg[len(current_group) :]
elif current_group.startswith(msg):
pass # Keep this as-is. See EditorInspector::update_tree().
else:
current_group = ""
current_subgroup = ""
if "." in msg: # Strip feature tag.
msg = msg.split(".", 1)[0]
for part in msg.split("/"):
_add_message(processor.process_name(part), msg_plural, msgctx, location, translator_comment)
elif extract_type == ExtractType.GROUP:
_add_message(msg, msg_plural, msgctx, location, translator_comment)
current_group = captures["prefix"]
current_subgroup = ""
elif extract_type == ExtractType.SUBGROUP:
_add_message(msg, msg_plural, msgctx, location, translator_comment)
current_subgroup = captures["prefix"]
translator_comment = ""
l = f.readline()

View File

@@ -69,8 +69,9 @@ message_patterns = {
re.compile(r'PNAME\("(?P<message>[^"]+)"\)'): ExtractType.PROPERTY_PATH,
}
theme_property_patterns = {
re.compile(r'set_(constant|font|font_size|stylebox|color|icon)\("(?P<message>[^"]+)", '): ExtractType.PROPERTY_PATH,
re.compile(r'set_(?P<theme_item>constant|font|font_size|stylebox|color|icon)\("(?P<message>[^"]+)", '): ExtractType.PROPERTY_PATH,
}
class_name_pattern = re.compile(r"^\w+ (?P<class_name>\w+)::\w+\(")
def _is_block_translator_comment(translator_line):
@@ -118,12 +119,17 @@ def process_file(f, fname):
translator_comment = ""
current_group = ""
current_subgroup = ""
current_class = ""
patterns = message_patterns
if os.path.basename(fname) == "default_theme.cpp":
patterns = {**message_patterns, **theme_property_patterns}
while l:
# Detect class name.
m = class_name_pattern.match(l)
if m:
current_class = m.group("class_name")
# Detect translator comments.
if not reading_translator_comment and l.find("TRANSLATORS:") != -1:
@@ -169,6 +175,13 @@ def process_file(f, fname):
if "PROPERTY_USAGE_DEFAULT" not in usages and "PROPERTY_USAGE_EDITOR" not in usages:
continue
property_path = msg
theme_item = captures.get("theme_item")
if theme_item:
if theme_item == "stylebox":
theme_item = "style"
property_path = "theme_overrides_" + theme_item + "/" + property_path
if current_subgroup:
if msg.startswith(current_subgroup):
msg = msg[len(current_subgroup) :]
@@ -188,6 +201,7 @@ def process_file(f, fname):
if "." in msg: # Strip feature tag.
msg = msg.split(".", 1)[0]
for part in msg.split("/"):
msgctx = processor.get_context(part, property_path, current_class)
_add_message(processor.process_name(part), msg_plural, msgctx, location, translator_comment)
elif extract_type == ExtractType.GROUP:
_add_message(msg, msg_plural, msgctx, location, translator_comment)