Skip to content

Commit

Permalink
Remove plural from parameter list of writestr and rename `new_pl_fm…
Browse files Browse the repository at this point in the history
…t` to `pl_fmt`
  • Loading branch information
Qrox committed Apr 20, 2020
1 parent 0cf3e83 commit eef6437
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions lang/extract_json_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def extract_gun(item):
if "name" in item:
item_name = item.get("name")
if item["type"] in needs_plural:
writestr(outfile, item_name, new_pl_fmt=True)
writestr(outfile, item_name, pl_fmt=True)
else:
writestr(outfile, item_name)
if "description" in item:
Expand All @@ -386,7 +386,7 @@ def extract_gunmod(item):
if "name" in item:
item_name = item.get("name")
if item["type"] in needs_plural:
writestr(outfile, item_name, new_pl_fmt=True)
writestr(outfile, item_name, pl_fmt=True)
else:
writestr(outfile, item_name)
if "description" in item:
Expand Down Expand Up @@ -860,27 +860,27 @@ def gettextify(string, context=None, plural=None):
else:
return "_(%r)\n" % string

def writestr(filename, string, plural=None, context=None, format_strings=False, comment=None, new_pl_fmt=False):
def writestr(filename, string, context=None, format_strings=False, comment=None, pl_fmt=False):
"Wrap the string and write to the file."
if type(string) is list and plural is None:
if type(string) is list:
for entry in string:
writestr(filename, entry, None, context, format_strings, comment)
writestr(filename, entry, context, format_strings, comment, pl_fmt)
return
elif type(string) is dict and plural is None:
elif type(string) is dict:
if "//~" in string:
if comment is None:
comment = string["//~"]
else:
comment = "{}\n{}".format(comment, string["//~"])
ctxt = string.get( "ctxt" )
context = string.get( "ctxt" )
str_pl = None
if new_pl_fmt:
if pl_fmt:
if "str_pl" in string:
str_pl = string["str_pl"]
elif "str_sp" in string:
str_pl = string["str_sp"]
else:
# no "str_pl" entry in json, assuming regular plural form as in item_factory.cpp etc
# no "str_pl" entry in json, assuming regular plural form as in translations.cpp
str_pl = "{}s".format(string["str"])
elif "str_pl" in string or "str_sp" in string:
raise WrongJSONItem("ERROR: 'str_pl' and 'str_sp' not supported here", string)
Expand All @@ -890,26 +890,30 @@ def writestr(filename, string, plural=None, context=None, format_strings=False,
str_singular = string["str_sp"]
else:
raise WrongJSONItem("ERROR: 'str' or 'str_sp' not found", string)
writestr(filename, str_singular, str_pl, ctxt, format_strings, comment)
return
elif type(string) is not str and plural is not None:
raise WrongJSONItem("ERROR: 'name_plural' found but 'name' is not a string", plural)

# don't write empty strings
if not string: return
if new_pl_fmt:
# no "str_pl" entry in json, assuming regular plural form as in item_factory.cpp etc
plural = "{}s".format(string)
elif type(string) is str:
if len(string) == 0:
# empty string has special meaning for gettext, skip it
return
str_singular = string
if pl_fmt:
# no "str_pl" entry in json, assuming regular plural form as in translations.cpp
str_pl = "{}s".format(string)
else:
str_pl = None
elif string is None:
return;
else:
raise WrongJSONItem("ERROR: value is not a string, dict, list, or None", string)

with open(filename, 'a', encoding="utf-8", newline='\n') as fs:
# Append developers comment
if comment:
tlcomment(fs, comment)
# most of the strings from json don't use string formatting.
# we must tell xgettext this explicitly
if not format_strings and "%" in string:
if not format_strings and ("%" in str_singular or (str_pl is not None and "%" in str_pl)):
fs.write("# xgettext:no-python-format\n")
fs.write(gettextify(string,context=context,plural=plural))
fs.write(gettextify(str_singular,context=context,plural=str_pl))

def get_outfile(json_object_type):
return os.path.join(to_dir, json_object_type + "_from_json.py")
Expand Down Expand Up @@ -995,7 +999,7 @@ def extract(item, infilename):
return
if name:
if object_type in needs_plural:
writestr(outfile, name, new_pl_fmt=True, **kwargs)
writestr(outfile, name, pl_fmt=True, **kwargs)
else:
writestr(outfile, name, **kwargs)
wrote = True
Expand All @@ -1014,7 +1018,7 @@ def extract(item, infilename):
if "conditional_names" in item:
for cname in item["conditional_names"]:
c = "Conditional name for {} when {} matches {}".format(name, cname["type"], cname["condition"])
writestr(outfile, cname["name"], comment=c, format_strings=True, new_pl_fmt=True, **kwargs)
writestr(outfile, cname["name"], comment=c, format_strings=True, pl_fmt=True, **kwargs)
wrote = True
if "description" in item:
if name:
Expand Down

0 comments on commit eef6437

Please sign in to comment.