2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 from grit.format.policy_templates.writers import plist_helper
8 from grit.format.policy_templates.writers import template_writer
11 def GetWriter(config):
12 '''Factory method for creating PListStringsWriter objects.
13 See the constructor of TemplateWriter for description of
16 return PListStringsWriter(['mac'], config)
19 class PListStringsWriter(template_writer.TemplateWriter):
20 '''Outputs localized string table files for the Mac policy file.
21 These files are named Localizable.strings and they are in the
22 [lang].lproj subdirectories of the manifest bundle.
25 def WriteComment(self, comment):
26 self._out.append('/* ' + comment + ' */' )
28 def _AddToStringTable(self, item_name, caption, desc):
29 '''Add a title and a description of an item to the string table.
32 item_name: The name of the item that will get the title and the
34 title: The text of the title to add.
35 desc: The text of the description to add.
37 caption = caption.replace('"', '\\"')
38 caption = caption.replace('\n', '\\n')
39 desc = desc.replace('"', '\\"')
40 desc = desc.replace('\n', '\\n')
41 self._out.append('%s.pfm_title = \"%s\";' % (item_name, caption))
42 self._out.append('%s.pfm_description = \"%s\";' % (item_name, desc))
44 def PreprocessPolicies(self, policy_list):
45 return self.FlattenGroupsAndSortPolicies(policy_list)
47 def WritePolicy(self, policy):
48 '''Add strings to the stringtable corresponding a given policy.
51 policy: The policy for which the strings will be added to the
55 if policy['type'] == 'external':
56 # This type can only be set through cloud policy.
58 elif policy['type'] in ('int-enum','string-enum', 'string-enum-list'):
59 # Append the captions of enum items to the description string.
61 for item in policy['items']:
62 item_descs.append(str(item['value']) + ' - ' + item['caption'])
63 desc = '\n'.join(item_descs) + '\n' + desc
65 self._AddToStringTable(policy['name'], policy['label'], desc)
67 def BeginTemplate(self):
68 app_name = plist_helper.GetPlistFriendlyName(self.config['app_name'])
69 if self._GetChromiumVersionString() is not None:
70 self.WriteComment(self.config['build'] + ''' version: ''' + \
71 self._GetChromiumVersionString())
72 self._AddToStringTable(
74 self.config['app_name'],
75 self.messages['mac_chrome_preferences']['text'])
78 # A buffer for the lines of the string table being generated.
81 def GetTemplateText(self):
82 return '\n'.join(self._out)