Commit 887e9e6f authored by Nikita Podguzov's avatar Nikita Podguzov Committed by Commit Bot

Add translations to schema keys descriptions.

Bug: 960274
Change-Id: I01aa63287ecd9b492a112eafe7f2d5b8186418dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1609907Reviewed-by: default avatarLutz Justen <ljusten@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Nikita Podguzov <nikitapodguzov@google.com>
Cr-Commit-Position: refs/heads/master@{#661107}
parent 1d4d7315
...@@ -27,20 +27,38 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase): ...@@ -27,20 +27,38 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase):
def testPolicyTranslation(self): def testPolicyTranslation(self):
# Create test policy_templates.json data. # Create test policy_templates.json data.
caption = "The main policy" caption = "The main policy"
caption_translation = "Die Hauptrichtilinie" caption_translation = "Die Hauptrichtlinie"
message = \ message = \
"Red cabbage stays red cabbage and wedding dress stays wedding dress" "Red cabbage stays red cabbage and wedding dress stays wedding dress"
message_translation = \ message_translation = \
"Blaukraut bleibt Blaukraut und Brautkleid bleibt Brautkleid" "Blaukraut bleibt Blaukraut und Brautkleid bleibt Brautkleid"
schema_key_description = "Number of users"
schema_key_description_translation = "Anzahl der Nutzer"
policy_json = """ policy_json = """
{ {
"policy_definitions": [ "policy_definitions": [
{ {
'name': 'MainPolicy', 'name': 'MainPolicy',
'type': 'main', 'type': 'main',
'schema': { 'type': 'boolean' }, 'schema': {
'properties': {
'default_launch_container': {
'enum': [
'tab',
'window',
],
'type': 'string',
},
'users_number': {
'description': '''%s''',
'type': 'integer',
},
},
'type': 'object',
},
'supported_on': ['chrome_os:29-'], 'supported_on': ['chrome_os:29-'],
'features': { 'features': {
'can_be_recommended': True, 'can_be_recommended': True,
...@@ -59,19 +77,24 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase): ...@@ -59,19 +77,24 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase):
'text': '''%s''' 'text': '''%s'''
} }
} }
}""" % (caption, message) }""" % (schema_key_description, caption, message)
# Create translations. The translation IDs are hashed from the English text. # Create translations. The translation IDs are hashed from the English text.
caption_id = grit.extern.tclib.GenerateMessageId(caption); caption_id = grit.extern.tclib.GenerateMessageId(caption);
message_id = grit.extern.tclib.GenerateMessageId(message); message_id = grit.extern.tclib.GenerateMessageId(message);
schema_key_description_id = grit.extern.tclib.GenerateMessageId(
schema_key_description)
policy_xtb = """ policy_xtb = """
<?xml version="1.0" ?> <?xml version="1.0" ?>
<!DOCTYPE translationbundle> <!DOCTYPE translationbundle>
<translationbundle lang="de"> <translationbundle lang="de">
<translation id="%s">%s</translation> <translation id="%s">%s</translation>
<translation id="%s">%s</translation> <translation id="%s">%s</translation>
<translation id="%s">%s</translation>
</translationbundle>""" % (caption_id, caption_translation, </translationbundle>""" % (caption_id, caption_translation,
message_id, message_translation) message_id, message_translation,
schema_key_description_id,
schema_key_description_translation)
# Write both to a temp file. # Write both to a temp file.
tmp_dir_name = tempfile.gettempdir() tmp_dir_name = tempfile.gettempdir()
...@@ -132,7 +155,22 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase): ...@@ -132,7 +155,22 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase):
'type': 'main', 'type': 'main',
'example_value': True, 'example_value': True,
'supported_on': ['chrome_os:29-'], 'supported_on': ['chrome_os:29-'],
'schema': {'type': 'boolean'}, 'schema': {
'properties': {
'default_launch_container': {
'enum': [
'tab',
'window',
],
'type': 'string',
},
'users_number': {
'description': '''%s''',
'type': 'integer',
},
},
'type': 'object',
},
}, },
], ],
'messages': { 'messages': {
...@@ -141,7 +179,8 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase): ...@@ -141,7 +179,8 @@ class PolicyTemplatesJsonUnittest(unittest.TestCase):
}, },
}, },
}""" % (caption_translation, message_translation) }""" % (caption_translation, schema_key_description_translation,
message_translation)
self.assertEqual(expected, output) self.assertEqual(expected, output)
......
...@@ -42,8 +42,8 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer): ...@@ -42,8 +42,8 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer):
for node2 in node1.childNodes: for node2 in node1.childNodes:
example_text.append(node2.toxml()) example_text.append(node2.toxml())
else: else:
raise Exception('Unexpected element inside a placeholder: ' + raise Exception('Unexpected element inside a placeholder: ' +
node2.toxml()) node2.toxml())
if example_text == []: if example_text == []:
# In such cases the original text is okay for an example. # In such cases the original text is okay for an example.
example_text = text example_text = text
...@@ -154,6 +154,31 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer): ...@@ -154,6 +154,31 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer):
else: else:
raise Exception('Unexpected type %s' % item_type) raise Exception('Unexpected type %s' % item_type)
def _AddSchemaKeys(self, obj, depth):
obj_type = type(obj)
if obj_type == dict:
self._AddNontranslateableChunk('{\n')
for key in sorted(obj.keys()):
self._AddIndentedNontranslateableChunk(depth + 1, "'%s': " % key)
if key == 'description' and type(obj[key]) == str:
self._AddNontranslateableChunk("'''")
self._ParseMessage(obj[key], 'Description of schema property')
self._AddNontranslateableChunk("''',\n")
elif type(obj[key]) in (bool, int, str):
self._AddSchemaKeys(obj[key], 0)
else:
self._AddSchemaKeys(obj[key], depth + 1)
self._AddIndentedNontranslateableChunk(depth, '},\n')
elif obj_type == list:
self._AddNontranslateableChunk('[\n')
for item in obj:
self._AddSchemaKeys(item, depth + 1)
self._AddIndentedNontranslateableChunk(depth, '],\n')
elif obj_type in (bool, int, str):
self._AddIndentedNontranslateableChunk(depth, "'%s',\n" % obj)
else:
raise Exception('Invalid schema object: %s' % obj)
def _AddPolicyKey(self, item, item_type, parent_item, key, depth): def _AddPolicyKey(self, item, item_type, parent_item, key, depth):
'''Given a policy/enumeration item and a key, adds that key and its value '''Given a policy/enumeration item and a key, adds that key and its value
into the output. into the output.
...@@ -176,6 +201,8 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer): ...@@ -176,6 +201,8 @@ class PolicyJson(skeleton_gatherer.SkeletonGatherer):
item[key], item[key],
self._GetDescription(item, item_type, parent_item, key)) self._GetDescription(item, item_type, parent_item, key))
self._AddNontranslateableChunk("''',\n") self._AddNontranslateableChunk("''',\n")
elif key in ('schema', 'validation_schema', 'description_schema'):
self._AddSchemaKeys(item[key], depth)
else: else:
str_val = item[key] str_val = item[key]
if type(str_val) == types.StringType: if type(str_val) == types.StringType:
......
...@@ -81,6 +81,79 @@ class PolicyJsonUnittest(unittest.TestCase): ...@@ -81,6 +81,79 @@ class PolicyJsonUnittest(unittest.TestCase):
expected = self.GetExpectedOutput(original) expected = self.GetExpectedOutput(original)
self.failUnless(expected == eval(gatherer.Translate('en'))) self.failUnless(expected == eval(gatherer.Translate('en')))
def testSchema(self):
original = ("{"
" 'policy_definitions': ["
" {"
" 'name': 'Policy1',"
" 'schema': {"
" 'type': 'object',"
" 'properties': {"
" 'outer': {"
" 'description': 'outer description',"
" 'type': 'object',"
" 'inner': {"
" 'description': 'inner description',"
" 'type': 'integer',"
" },"
" },"
" },"
" },"
" 'caption': 'nothing special',"
" },"
" ],"
" 'messages': {}"
"}")
gatherer = policy_json.PolicyJson(StringIO.StringIO(original))
gatherer.Parse()
self.failUnless(len(gatherer.GetCliques()) == 3)
expected = self.GetExpectedOutput(original)
self.failUnless(expected == eval(gatherer.Translate('en')))
def testValidationSchema(self):
original = ("{"
" 'policy_definitions': ["
" {"
" 'name': 'Policy1',"
" 'validation_schema': {"
" 'type': 'object',"
" 'properties': {"
" 'description': 'properties description',"
" 'type': 'object',"
" },"
" },"
" },"
" ],"
" 'messages': {}"
"}")
gatherer = policy_json.PolicyJson(StringIO.StringIO(original))
gatherer.Parse()
self.failUnless(len(gatherer.GetCliques()) == 1)
expected = self.GetExpectedOutput(original)
self.failUnless(expected == eval(gatherer.Translate('en')))
def testDescriptionSchema(self):
original = ("{"
" 'policy_definitions': ["
" {"
" 'name': 'Policy1',"
" 'description_schema': {"
" 'type': 'object',"
" 'properties': {"
" 'description': 'properties description',"
" 'type': 'object',"
" },"
" },"
" },"
" ],"
" 'messages': {}"
"}")
gatherer = policy_json.PolicyJson(StringIO.StringIO(original))
gatherer.Parse()
self.failUnless(len(gatherer.GetCliques()) == 1)
expected = self.GetExpectedOutput(original)
self.failUnless(expected == eval(gatherer.Translate('en')))
# Keeping for backwards compatibility. # Keeping for backwards compatibility.
def testSubPolicyOldFormat(self): def testSubPolicyOldFormat(self):
original = ( original = (
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment