Exemplo n.º 1
0
def translate_cmd(scriptname, command, argv):
    """Translate a single string or .po file of strings."""
    if '-' not in argv:
        # Don't print version stuff if we're reading from stdin.
        print '%s version %s' % (scriptname, __version__)

    parser = build_parser(
        'usage: %prog tramslate '
        '[- | -s STRING <STRING> ... | FILENAME <FILENAME> ...]',
        description='Translates a string or a .po file into Pirate.',
        epilog='Note: Translating files is done in-place replacing '
        'the original file.',
        sections=[
            (format_types(), True),
            (format_pipeline_parts(), True),
        ])
    # FIXME: move printing of available types to epilog. also rename
    # types to something more accurate like "variable formats".
    parser.add_option(
        '-t', '--types',
        dest='types',
        help='Comma-separated list of variable types. See Available Types.',
        metavar='TYPES',
        default='python')
    parser.add_option(
        '-p', '--pipeline',
        dest='pipeline',
        help='Translate pipeline. See Available Pipeline Parts.',
        metavar='PIPELINE',
        default='html,pirate')
    parser.add_option(
        '-s', '--string',
        action='store_true',
        dest='strings',
        help='translates specified string args')

    (options, args) = parser.parse_args(argv)

    if not args:
        parser.print_help()
        return 1

    translator = Translator(
        options.types.split(','), options.pipeline.split(','))

    if options.strings:
        # Args are strings to be translated
        for arg in args:
            print_utf8(translator.translate_string(arg))

    elif len(args) == 1 and args[0] == '-':
        # Read everything from stdin, then translate it
        print_utf8(translator.translate_string(sys.stdin.read()))

    else:
        # Args are filenames
        for arg in args:
            translator.translate_file(arg)

    return 0
Exemplo n.º 2
0
    def test_pirate_translate(self):
        trans = Translator(['pysprintf', 'pyformat'], ['pirate'])
        eq_(trans.translate_string(u'hello'),
            u'\'ello ahoy\u2757')

        # Note, this doesn't work right because it's not accounting
        # for html. But it's correct for this test.
        eq_(trans.translate_string(u'<b>hello</b>'),
            u'<b>\'ello</b> prepare to be boarded\u2757')
Exemplo n.º 3
0
    def test_pirate_translate(self):
        trans = Translator(['python-format', 'python-brace-format'],
                           ['pirate'])
        assert (trans.translate_string(u'hello') == u'\'ello ahoy\u2757')

        # Note, this doesn't work right because it's not accounting
        # for html. But it's correct for this test.
        assert (trans.translate_string(u'<b>hello</b>') ==
                u'<b>\'ello</b> prepare to be boarded\u2757')
Exemplo n.º 4
0
 def test_shouty_html_pirate_translate(self):
     trans = Translator(
         ['python-format', 'python-brace-format'],
         ['shouty', 'html', 'pirate']
     )
     assert (
         trans.translate_string('<b>hello.</b>\n') ==
         '<b>HELLO aye\u2757.</b>'
     )
Exemplo n.º 5
0
 def test_html_pirate_translate(self):
     trans = Translator(
         ['python-format', 'python-brace-format'],
         ['html', 'pirate']
     )
     assert (
         trans.translate_string('<b>hello</b>') ==
         '<b>\'ello ahoy\u2757</b>'
     )
Exemplo n.º 6
0
    def test_pirate_translate(self):
        trans = Translator(["python-format", "python-brace-format"], ["pirate"])
        assert trans.translate_string("hello") == "'ello ahoy\u2757"

        # Note, this doesn't work right because it's not accounting
        # for html. But it's correct for this test.
        assert (
            trans.translate_string("<b>hello</b>")
            == "<b>'ello</b> prepare to be boarded\u2757"
        )
Exemplo n.º 7
0
 def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
     text = getattr(instance, src_field)
     if text:
         pipeline = ['shouty', 'anglequote']
         translated = Translator([], pipeline).translate_string(text)
         setattr(instance, dst_field, translated)
         instance.save()
Exemplo n.º 8
0
    def test_pirate_translate(self):
        trans = Translator(
            ['python-format', 'python-brace-format'],
            ['pirate']
        )
        assert (
            trans.translate_string('hello') ==
            '\'ello ahoy\u2757'
        )

        # Note, this doesn't work right because it's not accounting
        # for html. But it's correct for this test.
        assert (
            trans.translate_string('<b>hello</b>') ==
            '<b>\'ello</b> prepare to be boarded\u2757'
        )
Exemplo n.º 9
0
def translate(ctx, varformat, pipeline, strings, path):
    """
    Translate a single string or .po file of strings.

    If you want to pull the string from stdin, use "-".

    Note: Translating files is done in-place replacing the original
    file.

    """
    if not (path and path[0] == '-'):
        # We don't want to print this if they're piping to stdin
        out('dennis version {version}'.format(version=__version__))

    if not path:
        raise click.UsageError('nothing to work on. Use --help for help.')

    try:
        translator = Translator(varformat.split(','), pipeline.split(','))
    except InvalidPipeline as ipe:
        raise click.UsageError(ipe.args[0])

    if strings:
        # Args are strings to be translated
        for arg in path:
            data = translator.translate_string(arg)
            if PY2:
                data = data.encode('utf-8')
            out(data)

    elif path[0] == '-':
        # Read everything from stdin, then translate it
        data = click.get_binary_stream('stdin').read()
        data = translator.translate_string(data)
        out(data)

    else:
        # Check all the paths first
        for arg in path:
            if not os.path.exists(arg):
                raise click.UsageError(u'File {fn} does not exist.'.format(
                    fn=click.format_filename(arg)))

        for arg in path:
            click.echo(translator.translate_file(arg))

    ctx.exit(0)
Exemplo n.º 10
0
 def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
     text = getattr(instance, src_field)
     if text:
         pipeline = ['shouty', 'anglequote']
         translated = Translator([], pipeline).translate_string(text)
         setattr(instance, dst_field, translated)
         instance.save()
         self.log_info(instance=instance, action='translate', msg='success')
Exemplo n.º 11
0
def translate(ctx, varformat, pipeline, strings, path):
    """
    Translate a single string or .po file of strings.

    If you want to pull the string from stdin, use "-".

    Note: Translating files is done in-place replacing the original
    file.

    """
    if not (path and path[0] == '-'):
        # We don't want to print this if they're piping to stdin
        out('dennis version {version}'.format(version=__version__))

    if not path:
        raise click.UsageError('nothing to work on. Use --help for help.')

    try:
        translator = Translator(varformat.split(','), pipeline.split(','))
    except InvalidPipeline as ipe:
        raise click.UsageError(ipe.args[0])

    if strings:
        # Args are strings to be translated
        for arg in path:
            data = translator.translate_string(arg)
            out(data)

    elif path[0] == '-':
        # Read everything from stdin, then translate it
        data = click.get_binary_stream('stdin').read()
        data = translator.translate_string(data)
        out(data)

    else:
        # Check all the paths first
        for arg in path:
            if not os.path.exists(arg):
                raise click.UsageError('File {fn} does not exist.'.format(
                    fn=click.format_filename(arg)))

        for arg in path:
            click.echo(translator.translate_file(arg))

    ctx.exit(0)
Exemplo n.º 12
0
def translate(ctx, varformat, pipeline, strings, path):
    """
    Translate a single string or .po file of strings.

    If you want to pull the string from stdin, use "-".

    Note: Translating files is done in-place replacing the original
    file.

    """
    if not (len(path) == 1 and path[0] == '-'):
        out('dennis version {version}'.format(version=__version__))

    if not path:
        err('Nothing to work on. Use --help for help.')
        ctx.exit(1)

    translator = Translator(
        varformat.split(','), pipeline.split(','))

    if strings:
        # Args are strings to be translated
        for arg in path:
            data = translator.translate_string(arg)
            if PY2:
                data = data.encode('utf-8')
            out(data)

    elif len(path) == 1 and path[0] == '-':
        # Read everything from stdin, then translate it
        data = click.get_binary_stream('stdin').read()
        data = translator.translate_string(data)
        out(data)

    else:
        # Args are filenames
        for arg in path:
            translator.translate_file(arg)

    ctx.exit(0)
Exemplo n.º 13
0
 def test_shouty_html_pirate_translate(self):
     trans = Translator(['pysprintf', 'pyformat'],
                        ['shouty', 'html', 'pirate'])
     eq_(trans.translate_string(u'<b>hello.</b>\n'),
         u'<b>HELLO aye\u2757.</b>\n')
Exemplo n.º 14
0
 def test_html_pirate_translate(self):
     trans = Translator(['pysprintf', 'pyformat'], ['html', 'pirate'])
     eq_(trans.translate_string(u'<b>hello</b>'),
         u'<b>\'ello ahoy\u2757</b>')
Exemplo n.º 15
0
 def test_html_pirate_translate(self):
     trans = Translator(['python-format', 'python-brace-format'],
                        ['html', 'pirate'])
     assert (trans.translate_string(u'<b>hello</b>') ==
             u'<b>\'ello ahoy\u2757</b>')
Exemplo n.º 16
0
 def test_shouty_html_pirate_translate(self):
     trans = Translator(['python-format', 'python-brace-format'],
                        ['shouty', 'html', 'pirate'])
     assert (trans.translate_string(u'<b>hello.</b>\n') ==
             u'<b>HELLO aye\u2757.</b>')
Exemplo n.º 17
0
 def test_shouty_html_pirate_translate(self):
     trans = Translator(
         ["python-format", "python-brace-format"], ["shouty", "html", "pirate"]
     )
     assert trans.translate_string("<b>hello.</b>\n") == "<b>HELLO aye\u2757.</b>"
Exemplo n.º 18
0
 def test_trololo_translate(self):
     trans = Translator(['python'], ['trololo'])
     eq_(trans.translate_string(u'hello'),
         u'trololo')
Exemplo n.º 19
0
def translate_cmd(scriptname, command, argv):
    """Translate a single string or .po file of strings."""
    if '-' not in argv:
        # Don't print version stuff if we're reading from stdin.
        out('{0} version {1}'.format(scriptname, __version__))

    parser = build_parser(
        'usage: %prog tramslate '
        '[- | -s STRING <STRING> ... | FILENAME <FILENAME> ...]',
        description='Translates a string or a .po file into Pirate.',
        epilog='Note: Translating files is done in-place replacing '
        'the original file.',
        sections=[
            (format_vars(), True),
            (format_pipeline_parts(), True),
        ])
    parser.add_option(
        '--vars',
        dest='vars',
        help=('Comma-separated list of variable types. See Available Variable '
              'Formats.'),
        metavar='VARS',
        default='pysprintf,pyformat')
    parser.add_option(
        '-p', '--pipeline',
        dest='pipeline',
        help='Translate pipeline. See Available Pipeline Parts.',
        metavar='PIPELINE',
        default='html,pirate')
    parser.add_option(
        '-s', '--string',
        action='store_true',
        dest='strings',
        help='translates specified string args')

    (options, args) = parser.parse_args(argv)

    if not args:
        parser.print_help()
        return 1

    translator = Translator(
        options.vars.split(','), options.pipeline.split(','))

    if options.strings:
        # Args are strings to be translated
        for arg in args:
            data = translator.translate_string(arg)
            if PY2:
                data = data.encode('utf-8')
            print(data)

    elif len(args) == 1 and args[0] == '-':
        # Read everything from stdin, then translate it
        data = translator.translate_string(sys.stdin.read())
        if PY2:
            data = data.encode('utf-8')
        print(data)

    else:
        # Args are filenames
        for arg in args:
            translator.translate_file(arg)

    return 0
Exemplo n.º 20
0
 def test_html_pirate_translate(self):
     trans = Translator(["python-format", "python-brace-format"], ["html", "pirate"])
     assert trans.translate_string("<b>hello</b>") == "<b>'ello ahoy\u2757</b>"