Exemple #1
0
    def test_insert_breaks(self):

        spaces = re.compile(r'\s+|\\n')

        def contains_all_chars(orig, result):

            return re.sub(spaces, '', orig) == re.sub(spaces, '', result)

        sentences = [
            u'Quay Pegman qua bên trái hoặc bên phải 90 độ.',
            u'Foo bar baz this is english that is okay bye.',
            u'If there is a path in the specified direction, \nthen ' +
            u'do some actions.',
            u'If there is a path in the specified direction, then do ' +
            u'the first block of actions. Otherwise, do the second ' +
            u'block of actions.'
        ]

        for sentence in sentences:

            output = common.insert_breaks(sentence, 30, 50)

            self.assertTrue(
                contains_all_chars(sentence, output),
                u'Mismatch between:\n{0}\n{1}'.format(
                    re.sub(spaces, '', sentence), re.sub(spaces, '', output)))
Exemple #2
0
  def test_insert_breaks(self):
    spaces = re.compile(r'\s+|\\n')
    def contains_all_chars(orig, result):
      return re.sub(spaces, '', orig) == re.sub(spaces, '', result)

    sentences = [u'Quay Pegman qua bên trái hoặc bên phải 90 độ.',
                 u'Foo bar baz this is english that is okay bye.',
                 u'If there is a path in the specified direction, \nthen ' +
                 u'do some actions.',
                 u'If there is a path in the specified direction, then do ' +
                 u'the first block of actions. Otherwise, do the second ' +
                 u'block of actions.']
    for sentence in sentences:
      output = common.insert_breaks(sentence, 30, 50)
      self.assert_(contains_all_chars(sentence, output),
                   u'Mismatch between:\n{0}\n{1}'.format(
                       re.sub(spaces, '', sentence),
                       re.sub(spaces, '', output)))
Exemple #3
0
def _process_file(path_to_json, target_lang, key_dict):
    """Creates an .xlf file corresponding to the specified .json input file.

    The name of the input file must be target_lang followed by '.json'.
    The name of the output file will be target_lang followed by '.js'.

    Args:
        path_to_json: Path to the directory of xx.json files.
        target_lang: A IETF language code (RFC 4646), such as 'es' or 'pt-br'.
        key_dict: Dictionary mapping Blockly keys (e.g., Maze.turnLeft) to
            Closure keys (hash numbers).

    Raises:
        IOError: An I/O error occurred with an input or output file.
        InputError: Input JSON could not be parsed.
        KeyError: Key found in input file but not in key file.
    """
    keyfile = os.path.join(path_to_json, target_lang + '.json')
    j = read_json_file(keyfile)
    out_file = _create_xlf(target_lang)
    for key in j:
        if key != '@metadata':
            try:
                identifier = key_dict[key]
            except KeyError, e:
                print('Key "%s" is in %s but not in %s' %
                      (key, keyfile, args.key_file))
                raise e
            target = j.get(key)
            # Only insert line breaks for tooltips.
            if key.lower().find('tooltip') != -1:
                target = insert_breaks(j.get(key), args.min_length,
                                       args.max_length)
            out_file.write(u"""
      <trans-unit id="{0}" datatype="html">
        <target>{1}</target>
      </trans-unit>""".format(identifier, target))
def _process_file(path_to_json, target_lang, key_dict):
    """Creates an .xlf file corresponding to the specified .json input file.

    The name of the input file must be target_lang followed by '.json'.
    The name of the output file will be target_lang followed by '.js'.

    Args:
        path_to_json: Path to the directory of xx.json files.
        target_lang: A IETF language code (RFC 4646), such as 'es' or 'pt-br'.
        key_dict: Dictionary mapping Blockly keys (e.g., Maze.turnLeft) to
            Closure keys (hash numbers).

    Raises:
        IOError: An I/O error occurred with an input or output file.
        InputError: Input JSON could not be parsed.
        KeyError: Key found in input file but not in key file.
    """
    keyfile = os.path.join(path_to_json, target_lang + '.json')
    j = read_json_file(keyfile)
    out_file = _create_xlf(target_lang)
    for key in j:
        if key != '@metadata':
            try:
                identifier = key_dict[key]
            except KeyError, e:
                print('Key "%s" is in %s but not in %s' %
                      (key, keyfile, args.key_file))
                raise e
            target = j.get(key)
            # Only insert line breaks for tooltips.
            if key.lower().find('tooltip') != -1:
                target = insert_breaks(
                    j.get(key), args.min_length, args.max_length)
            out_file.write(u"""
      <trans-unit id="{0}" datatype="html">
        <target>{1}</target>
      </trans-unit>""".format(identifier, target))