Command-line translator using Google Translate

今天更新Google Play 商店裡的App 顯示下面的訊息,叫我要把其他語言裡更新了什麼訊息都補齊,這個請「千萬」不要使用人工手動去翻,純手工去翻是生產力低、無聊又花時間的行為,建議的解法是要shell script +translate-shell

 

shell script 很多,除了python 好用之外,常見的還有 unix 內建的bash shell script. 這次我選擇的是內建的,要用內建需要先學一下 bash shell 的 array 還有 loop 怎麼使用。

鳥哥loop 用法教學:
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#for

鳥哥array 用法教學:
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#array


接下來安裝 translate-shell
https://github.com/soimort/translate-shell

我是使用 Mac OS X, 需要另外再按裝 gawk, 只要使用指令:

brew install gawk

最後我的script 長醬子:

#!/bin/bash
filename="play_app_output.txt"
english="play_app_english.txt"
echo "" > $filename
cat $english >> $filename
echo "" >> $filename

function printit(){
	lang=${1}
	echo "<$lang>" >> $filename
	trans -b :$lang file://$english >> $filename
	echo "</$lang>" >> $filename
}

eat[1]="ar"
eat[2]="be"
max_nu=2

for (( i=1; i<=${max_nu}; i=i+1 ))
do
	printit ${eat[$i]}
done


要被翻的英文:

allow login facebook account by browser.


script 執行完的結果:

<en-US>

allow login facebook account by browser.

</en-US>

<ar>

.حفصتملا قيرط نع كوبسيفلا باسح لوخدلا ليجست حامسلا

</ar>

<be>

дазволіць уліковы запіс Увайсці facebook браўзэра.

</be>


後記:

bash shell script 寫起來是很快,也可以完成「大部份」的需求,但後來想用來解決 android strings 裡裡翻譯,bash shell script用起來綁手綁腳,限制太多,最後還是改寫成python…, 所以一開始就應該用python 寫,才省時間。

改用python,對字串和對檔案的處理就變的很簡單。執行外部指令可以使用:

from subprocess import call
tmp_input_file = "android_trans_input.tmp"
tmp_output_file = "android_trans_output.tmp"
trans_para_pattern = "trans -b -s en :%s -i %s -o %s" % (lang, tmp_input_file, tmp_output_file)
call(trans_para_pattern, shell=True)

我使用的副程式:

 

def google_trans(string_value, lang):
 tmp_input_file = "android_trans_input.tmp"
 outfile = open(tmp_input_file, 'w')
 outfile.write(string_value)
 outfile.close()

from subprocess import call
 tmp_output_file = "android_trans_output.tmp"
 trans_para_pattern = "trans -b -s en :%s -i %s -o %s" % (lang, tmp_input_file, tmp_output_file)
 #call(["trans", trans_para_pattern])
 call(trans_para_pattern, shell=True)
 #call(["ls", "-al"])
 #call("ls android_t*", shell=True)
 #call(["pwd"])

infile = open(tmp_output_file,"r")
 total_line = ""
 for in_line in infile:
 in_line = in_line.strip()
 total_line += in_line
 infile.close()

call("rm android*.tmp", shell=True) 
 return total_line

def convert_trans(filename, lang):
 #filename = "Localizable.strings"
 filename_ext = splitext(filename)[1]
 #filename_output = "%s%s" % (splitext(filename)[0], filename_ext)
 filename_output = filename
 directory_name = "./values-%s/" % (lang)
 directory_name = directory_name.replace("zh-", "zh-r")
 _createFolder(directory_name )

outfile = open(directory_name + filename_output, 'w')
 infile = open(filename)

total_line = ""

process_mode = "ios"
 if filename_ext == ".xml":
 process_mode = "android"

delimiter = "="
 if process_mode == "android":
 delimiter = ">"

string_pattern = '%s = "%s";\n'

for in_line in infile:
 out_line = in_line
 
 if is_translata_target(in_line, process_mode):
 # for translate item.
 if process_mode == "android":
 string_android_tag_name="string"
 if '</item>' in in_line and '<item' in in_line:
 string_android_tag_name="item"
 string_pattern = '%s>%s</'+ string_android_tag_name +'>\n'

string_key = in_line.split(delimiter)[0]
 string_value = find_between(in_line,">","</")
 string_value = encrypt_escape_word(string_value, process_mode)
 string_trans = google_trans(string_value, lang)
 string_trans = decrypt_escape_word(string_trans, process_mode, lang)
 out_line = string_pattern % (string_key.rstrip(),string_trans)
 #print out_line

if not is_skip_translata_key(in_line, process_mode):
 outfile.write(out_line)
 #total_line = u'%s%s' % (total_line,out_line)
 total_line += out_line
 infile.close()
 outfile.close()

心得是效果滿好的,還可以調整那些規則下的文字不要去翻譯。

 

如果你會處理到 lang=”ar” 或 lang=”fa”,記得要安裝一下 FirBiDi:

指令:

brew install fribidi

資料來源:

http://macappstore.org/fribidi/

 

 

相關文章

寫留言

你的電子郵件位址並不會被公開。 必要欄位標記為 *