<commit_before>#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Formats as a .json file that can be used to localize Google Chrome
extensions."""

import re
import types

from grit import util
from grit.node import message

def Format(root, lang='en', output_dir='.'):
  """Format the messages as JSON."""
  yield '{\n'

  format = ('  "%s": {\n'
            '    "message": "%s"\n'
            '  }')
  first = True
  for child in root:
    if isinstance(child, message.MessageNode):
      loc_message = child.Translate(lang)
      loc_message = re.sub(r'\\', r'\\\\', loc_message)
      loc_message = re.sub(r'"', r'\"', loc_message)

      id = child.attrs['name']
      if id.startswith('IDR_'):
        id = id[4:]

      if not first:
        yield ',\n'
      first = False
      yield format % (id, loc_message)

  yield '\n}\n'<commit_msg>Fix chrome_messages_json generator to handle <if> properly.<commit_after>#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Formats as a .json file that can be used to localize Google Chrome
extensions."""

import re
import types

from grit import util
from grit.node import message

def Format(root, lang='en', output_dir='.'):
  """Format the messages as JSON."""
  yield '{\n'

  format = ('  "%s": {\n'
            '    "message": "%s"\n'
            '  }')
  first = True
  for child in root.ActiveDescendants():
    if isinstance(child, message.MessageNode):
      loc_message = child.Translate(lang)
      loc_message = re.sub(r'\\', r'\\\\', loc_message)
      loc_message = re.sub(r'"', r'\"', loc_message)

      id = child.attrs['name']
      if id.startswith('IDR_'):
        id = id[4:]

      if not first:
        yield ',\n'
      first = False
      yield format % (id, loc_message)

  yield '\n}\n'
{'fraction_non_alphanumeric': 0.142578125, 'fraction_numerical': 0.0048828125, 'mean_word_length': 2.691891891891892, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920263', 'n_tokens_mistral': 671, 'n_tokens_neox': 646, 'n_words': 225}