Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / chrome_messages_json.py
1 #!/usr/bin/env python
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.
5
6 """Formats as a .json file that can be used to localize Google Chrome
7 extensions."""
8
9 from json import JSONEncoder
10 import re
11 import types
12
13 from grit import util
14 from grit.node import message
15
16 def Format(root, lang='en', output_dir='.'):
17   """Format the messages as JSON."""
18   yield '{\n'
19
20   encoder = JSONEncoder();
21   format = ('  "%s": {\n'
22             '    "message": %s\n'
23             '  }')
24   first = True
25   for child in root.ActiveDescendants():
26     if isinstance(child, message.MessageNode):
27       id = child.attrs['name']
28       if id.startswith('IDR_') or id.startswith('IDS_'):
29         id = id[4:]
30
31       loc_message = encoder.encode(child.ws_at_start + child.Translate(lang) +
32                                    child.ws_at_end)
33
34       if not first:
35         yield ',\n'
36       first = False
37       yield format % (id, loc_message)
38
39   yield '\n}\n'