Skip to content

Commit

Permalink
Merge pull request #10 from timothyqiu/property-context
Browse files Browse the repository at this point in the history
Add context support for editor property name i18n
  • Loading branch information
akien-mga authored Oct 6, 2023
2 parents 73d5be8 + c74e582 commit 5f62ad2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 53 deletions.
45 changes: 30 additions & 15 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

if not is_inside_stop_words and not self.stop_words:
is_inside_stop_words = "stop_words = " in line
m = remap_re.search(line)
if m:
self.remaps[m.group("from")] = m.group("to")
continue

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().
Expand All @@ -94,6 +97,18 @@ def process_name(self, name: str) -> str:

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 = []
Expand Down
37 changes: 0 additions & 37 deletions scripts/extract_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
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):
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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()
Expand Down
16 changes: 15 additions & 1 deletion scripts/extract_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) :]
Expand All @@ -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)
Expand Down

0 comments on commit 5f62ad2

Please sign in to comment.