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()
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')
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')
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" )
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)
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)
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>"
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>"
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>')
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>')