Update translation extractor so ternary expressions are not merged.
eg: ("A" if test else "B")
This commit is contained in:
parent
372cf93309
commit
a643d28446
@ -361,27 +361,58 @@ def dump_py_messages_from_files(messages, check_ctxt, files):
|
|||||||
bpy_struct = bpy.types.ID.__base__
|
bpy_struct = bpy.types.ID.__base__
|
||||||
|
|
||||||
# Helper function
|
# Helper function
|
||||||
def extract_strings(node):
|
def extract_strings_ex(node, is_split=False):
|
||||||
"""
|
"""
|
||||||
Recursively get strings, needed in case we have "Blah" + "Blah", passed as an argument in that case it won't
|
Recursively get strings, needed in case we have "Blah" + "Blah", passed as an argument in that case it won't
|
||||||
evaluate to a string. However, break on some kind of stopper nodes, like e.g. Subscript.
|
evaluate to a string. However, break on some kind of stopper nodes, like e.g. Subscript.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if type(node) == ast.Str:
|
if type(node) == ast.Str:
|
||||||
eval_str = ast.literal_eval(node)
|
eval_str = ast.literal_eval(node)
|
||||||
if eval_str:
|
if eval_str:
|
||||||
return eval_str, (node,)
|
yield (is_split, eval_str, (node,))
|
||||||
return None, ()
|
else:
|
||||||
|
is_split = (type(node) in separate_nodes)
|
||||||
eval_str = []
|
|
||||||
nodes = []
|
|
||||||
for nd in ast.iter_child_nodes(node):
|
for nd in ast.iter_child_nodes(node):
|
||||||
if type(nd) not in stopper_nodes:
|
if type(nd) not in stopper_nodes:
|
||||||
estr, nds = extract_strings(nd)
|
yield from extract_strings_ex(nd, is_split=is_split)
|
||||||
eval_str.append(estr)
|
|
||||||
nodes += nds
|
def _extract_string_merge(estr_ls, nds_ls):
|
||||||
if eval_str:
|
return "".join(s for s in estr_ls if s is not None), tuple(n for n in nds_ls if n is not None)
|
||||||
return "".join(s for s in eval_str if s is not None), tuple(n for n in nodes if n is not None)
|
|
||||||
return None, ()
|
def extract_strings(node):
|
||||||
|
estr_ls = []
|
||||||
|
nds_ls = []
|
||||||
|
for is_split, estr, nds in extract_strings_ex(node):
|
||||||
|
estr_ls.append(estr)
|
||||||
|
nds_ls.extend(nds)
|
||||||
|
ret = _extract_string_merge(estr_ls, nds_ls)
|
||||||
|
print(ret)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def extract_strings_split(node):
|
||||||
|
"""
|
||||||
|
Returns a list args as returned by 'extract_strings()',
|
||||||
|
But split into groups based on separate_nodes, this way
|
||||||
|
expressions like ("A" if test else "B") wont be merged but
|
||||||
|
"A" + "B" will.
|
||||||
|
"""
|
||||||
|
estr_ls = []
|
||||||
|
nds_ls = []
|
||||||
|
bag = []
|
||||||
|
for is_split, estr, nds in extract_strings_ex(node):
|
||||||
|
if is_split:
|
||||||
|
bag.append((estr_ls, nds_ls))
|
||||||
|
estr_ls = []
|
||||||
|
nds_ls = []
|
||||||
|
|
||||||
|
estr_ls.append(estr)
|
||||||
|
nds_ls.extend(nds)
|
||||||
|
|
||||||
|
bag.append((estr_ls, nds_ls))
|
||||||
|
|
||||||
|
return [_extract_string_merge(estr_ls, nds_ls) for estr_ls, nds_ls in bag]
|
||||||
|
|
||||||
|
|
||||||
def _ctxt_to_ctxt(node):
|
def _ctxt_to_ctxt(node):
|
||||||
return extract_strings(node)[0]
|
return extract_strings(node)[0]
|
||||||
@ -427,6 +458,8 @@ def dump_py_messages_from_files(messages, check_ctxt, files):
|
|||||||
# Break recursive nodes look up on some kind of nodes.
|
# Break recursive nodes look up on some kind of nodes.
|
||||||
# E.g. we don’t want to get strings inside subscripts (blah["foo"])!
|
# E.g. we don’t want to get strings inside subscripts (blah["foo"])!
|
||||||
stopper_nodes = {ast.Subscript, }
|
stopper_nodes = {ast.Subscript, }
|
||||||
|
# Consider strings separate: ("a" if test else "b")
|
||||||
|
separate_nodes = {ast.IfExp, }
|
||||||
|
|
||||||
# For now only consider functions from UILayout...
|
# For now only consider functions from UILayout...
|
||||||
for func_id, func in bpy.types.UILayout.bl_rna.functions.items():
|
for func_id, func in bpy.types.UILayout.bl_rna.functions.items():
|
||||||
@ -490,16 +523,17 @@ def dump_py_messages_from_files(messages, check_ctxt, files):
|
|||||||
#print(translate_args)
|
#print(translate_args)
|
||||||
# do nothing if not found
|
# do nothing if not found
|
||||||
for arg_kw, arg_pos in translate_args:
|
for arg_kw, arg_pos in translate_args:
|
||||||
estr, nds = None, ()
|
estr_lst = [(None, ())]
|
||||||
if arg_pos < len(node.args):
|
if arg_pos < len(node.args):
|
||||||
estr, nds = extract_strings(node.args[arg_pos])
|
estr_lst = extract_strings_split(node.args[arg_pos])
|
||||||
#print(estr, nds)
|
#print(estr, nds)
|
||||||
else:
|
else:
|
||||||
for kw in node.keywords:
|
for kw in node.keywords:
|
||||||
if kw.arg == arg_kw:
|
if kw.arg == arg_kw:
|
||||||
estr, nds = extract_strings(kw.value)
|
estr_lst = extract_strings_split(kw.value)
|
||||||
break
|
break
|
||||||
#print(estr, nds)
|
#print(estr, nds)
|
||||||
|
for estr, nds in estr_lst:
|
||||||
if estr:
|
if estr:
|
||||||
key = (context, estr)
|
key = (context, estr)
|
||||||
if nds:
|
if nds:
|
||||||
|
@ -306,6 +306,10 @@ PYTHON3_EXEC = "python3"
|
|||||||
# The Blender executable!
|
# The Blender executable!
|
||||||
# This is just an example, you’ll most likely have to edit it in your user_settings.py!
|
# This is just an example, you’ll most likely have to edit it in your user_settings.py!
|
||||||
BLENDER_EXEC = os.path.abspath(os.path.join(TOOLS_DIR, "..", "..", "..", "..", "blender"))
|
BLENDER_EXEC = os.path.abspath(os.path.join(TOOLS_DIR, "..", "..", "..", "..", "blender"))
|
||||||
|
# check for blender.bin
|
||||||
|
if not os.path.exists(BLENDER_EXEC):
|
||||||
|
if os.path.exists(BLENDER_EXEC + ".bin"):
|
||||||
|
BLENDER_EXEC = BLENDER_EXEC + ".bin"
|
||||||
|
|
||||||
# The xgettext tool. You’ll likely have to edit it in your user_settings.py if you’re under Windows.
|
# The xgettext tool. You’ll likely have to edit it in your user_settings.py if you’re under Windows.
|
||||||
GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
|
GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user