Skip to content

Commit

Permalink
Respects explicit formatted="false" tag in strings
Browse files Browse the repository at this point in the history
It is valid for Android XML files to have strings that disregard
the formatting characters (like percent) by setting the tag
formatted="false", such as:

   <string name="discount" formatted="false">50% off</string>

This tag was previously not being carried through an export/import
cycle. Now it is tracked properly. In the po file we will store
the flag "no-c-format" to mean formatted="false" in the XML.

Note: this is only implemented for <string> at this time, not
<plurals> and <string-array>.

Closes #24
  • Loading branch information
bkeller-vuzix committed Feb 8, 2023
1 parent 69b36b4 commit 4fb4362
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions android2po/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(self, language=None):
self.language = language
class StringArray(list): pass
class Plurals(dict): pass
Translation = namedtuple('Translation', ['text', 'comments', 'formatted'])
class UnformattedString(str): pass
Translation = namedtuple('Translation', ['text', 'comments', 'formatted', 'print_false_format'])


def get_element_text(tag, name, warnfunc=dummy_warn):
Expand Down Expand Up @@ -381,6 +382,8 @@ def check_formatted(forced_state, detected_state):
force_fmt = True
elif tag.attrib['formatted'] == 'false':
force_fmt = False
else:
warnfunc('%s unrecognized formatted="%s"' % (name, tag.attrib['formatted']), 'warning')

if tag.tag == 'string':
try:
Expand All @@ -390,7 +393,8 @@ def check_formatted(forced_state, detected_state):
name, e.reason), 'info')
else:
formatted = check_formatted(force_fmt, formatted)
translation = Translation(text, comment, formatted)
force_print_format = (force_fmt != None)
translation = Translation(text, comment, formatted, force_print_format)
result[name] = translation

elif tag.tag == 'string-array':
Expand Down Expand Up @@ -420,7 +424,7 @@ def check_formatted(forced_state, detected_state):
(name, e.reason), 'warning')
else:
formatted = check_formatted(force_fmt, formatted)
translation = Translation(text, comment, formatted)
translation = Translation(text, comment, formatted, False)
result[name].append(translation)

elif tag.tag == 'plurals':
Expand All @@ -442,7 +446,7 @@ def check_formatted(forced_state, detected_state):
(name, e.reason), 'warning')
else:
formatted = check_formatted(force_fmt, formatted)
translation = Translation(text, comment, formatted)
translation = Translation(text, comment, formatted, False)
result[name][quantity] = translation

# We now have processed a tag. We either added those comments to
Expand Down Expand Up @@ -628,6 +632,8 @@ def xml2po(resources, translations=None, resfilter=None, warnfunc=dummy_warn):
flags = []
if org_value.formatted:
flags.append('c-format')
elif org_value.print_false_format:
flags.append('no-c-format')

catalog.add(org_value.text, trans_value.text if trans_value else '',
flags=flags, auto_comments=org_value.comments, context=name)
Expand Down Expand Up @@ -870,6 +876,9 @@ def validate_plural_config():
if not message.string and not with_untranslated:
# Untranslated.
continue
if message.flags and 'no-c-format' in message.flags:
# This sub-class will trigger us to write the string as unformatted
value = UnformattedString(value)
xml_tree[message.context] = value

return xml_tree
Expand Down Expand Up @@ -909,6 +918,8 @@ def write_xml(tree, warnfunc=dummy_warn):
string_el = write_to_dom(
'string', value, '"%s"' % name, namespaces_used, warnfunc)
string_el.attrib['name'] = name
if isinstance(value, UnformattedString):
string_el.attrib['formatted'] = "false"
root_tags.append(string_el)

# Generate the root element, define the namespaces that have been
Expand Down

0 comments on commit 4fb4362

Please sign in to comment.