2021.1.28.
this program require:
googletrans==4.0.0-rc1
Cf.
qiita page <googletrans with google colab>
<google colab で googletrans>
pip install googletrans==4.0.0-rc1
translate2.py
translate2.py
from googletrans import Translator
import sys
filename = 'output.txt'
args= sys.argv
if len(args) < 2:
print('usage:')
print('python3 translate2.py textfile.txt output_textfile.txt')
else:
print('open '+args[1])
with open(args[1]) as f:
line = f.readlines()
f.close()
print(args)
line[:] = [l.rstrip('\n') for l in line]
line[:] = [l.replace('\n',' ') for l in line]
#line_list = []
#for l in line:
# line_list.append(l.strip())
line[:] = [a for a in line if a != '']
##print(line)
if len(args) == 3:
filename = args[2]
translator = Translator()
f = open(filename, 'w')
for l in line:
translated = translator.translate(l, dest="ja");
print(l) # Original text
f.writelines(l)
f.write('\n')
print(translated.text)
f.writelines(translated.text)
f.write('\n')
print()
f.write('\n')
print('EOF')
f.close()
else:
translator = Translator()
for l in line:
translated = translator.translate(l, dest="ja");
print(l) # Original text
print(translated.text) # text
print()
print('EOF')
LANGUAGES |
---|
'af': 'afrikaans' |
'sq': 'albanian' |
'am': 'amharic' |
'ar': 'arabic' |
'hy': 'armenian' |
'az': 'azerbaijani' |
'eu': 'basque' |
'be': 'belarusian' |
'bn': 'bengali' |
'bs': 'bosnian' |
'bg': 'bulgarian' |
'ca': 'catalan' |
'ceb': 'cebuano' |
'ny': 'chichewa' |
'zh-cn': 'chinese (simplified)' |
'zh-tw': 'chinese (traditional)' |
'co': 'corsican' |
'hr': 'croatian' |
'cs': 'czech' |
'da': 'danish' |
'nl': 'dutch' |
'en': 'english' |
'eo': 'esperanto' |
'et': 'estonian' |
'tl': 'filipino' |
'fi': 'finnish' |
'fr': 'french' |
'fy': 'frisian' |
'gl': 'galician' |
'ka': 'georgian' |
'de': 'german' |
'el': 'greek' |
'gu': 'gujarati' |
'ht': 'haitian creole' |
'ha': 'hausa' |
'haw': 'hawaiian' |
'iw': 'hebrew' |
'he': 'hebrew' |
'hi': 'hindi' |
'hmn': 'hmong' |
'hu': 'hungarian' |
'is': 'icelandic' |
'ig': 'igbo' |
'id': 'indonesian' |
'ga': 'irish' |
'it': 'italian' |
'ja': 'japanese' |
'jw': 'javanese' |
'kn': 'kannada' |
'kk': 'kazakh' |
'km': 'khmer' |
'ko': 'korean' |
'ku': 'kurdish (kurmanji)' |
'ky': 'kyrgyz' |
'lo': 'lao' |
'la': 'latin' |
'lv': 'latvian' |
'lt': 'lithuanian' |
'lb': 'luxembourgish' |
'mk': 'macedonian' |
mg': 'malagasy' |
'ms': 'malay' |
'ml': 'malayalam' |
'mt': 'maltese' |
'mi': 'maori' |
'mr': 'marathi' |
'mn': 'mongolian' |
'my': 'myanmar (burmese)' |
'ne': 'nepali' |
'no': 'norwegian' |
'or': 'odia' |
'ps': 'pashto' |
'fa': 'persian' |
'pl': 'polish' |
'pt': 'portuguese' |
'pa': 'punjabi' |
'ro': 'romanian' |
'ru': 'russian' |
'sm': 'samoan' |
'gd': 'scots gaelic' |
'sr': 'serbian' |
'st': 'sesotho' |
'sn': 'shona' |
'sd': 'sindhi' |
'si': 'sinhala' |
'sk': 'slovak' |
'sl': 'slovenian' |
'so': 'somali' |
'es': 'spanish' |
'su': 'sundanese' |
'sw': 'swahili' |
'sv': 'swedish' |
'tg': 'tajik' |
'ta': 'tamil' |
'te': 'telugu' |
'th': 'thai' |
'tr': 'turkish' |
'uk': 'ukrainian' |
'ur': 'urdu' |
'ug': 'uyghur' |
'uz': 'uzbek' |
'vi': 'vietnamese' |
'cy': 'welsh' |
'xh': 'xhosa' |
'yi': 'yiddish' |
'yo': 'yoruba'a |
'zu': 'zulu' |