公司的业务发展,肯定需要做国际化操作,在国际化的过程中最头疼的就是写国际化语言,建立对应的string.xml文件 能够做国际化的应用每个国家的语言对应翻译应该有1000行左右,要是自己一个一个对应的copy整个人都会疯掉的.
版本写的是用bash脚本处理国际化语言
于是学会了一个偷懒的方式,写了一个脚本偷懒一下,可能脚本还不够完善不过大家可以根据自己的需求进行修改.
1.bash方式(Python在下面)
原来的内容
生成后的内容
1.国际化的具体翻译工作有专门的人去做,做好之后是excel表格如下
2.首先要将我们得到的xml另存为csv后缀的文件
3.执行如下脚本 translation.sh 赋予可执行权限
#!/bin/bash
bak=$IFS
#检查是否有文件输入
if [ $# -ne 1 ];then
echo "Usage $0 filename"
exit
fi
#检查输入的是否是文件
if [ ! -f $1 ];then
echo "the $1 is not a file"
exit
fi
IFS=$'\n'
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>">> string.xml
echo "<resources>">> string.xml
for line in `cat $1`
do
read1=`echo $line|cut -d "," -f 1`
read1=${read1##*' '} #切割key前面的空格
if [ -z "$read1" ];then #去除掉空字符串
continue
fi
read2=`echo $line|cut -d "," -f 3`
echo "<string name=\"$read1\""">"$read2"</string>" #打印要写入的内容
echo "<string name=\"$read1\""">"$read2"</string>" >> string.xml #将内容写入
done
echo "</resources>">> string.xml
IFS=$bak
在终端执行 ./translation.sh 翻译文件.csv
会看到终端输出如下: 等输出完成xml也就生成完成 (注意打开方式选着gedit ,原因是很多的翻译中包含xml不能解析的内容)
2.Python方式
#! /usr/bin/python
# coding=utf-8
import os, re
import xlrd
import sys, getopt
import operator
################国际化多语言替换start
def intl(file):
print ('-------------------- Start intl--------------------')
#print(sys.argv[0])
#print(sys.argv[1])
file_03_excel = "/Users/xulei/Desktop/int/012_i18n_android_part1_en_1027.xlsx"
print (file)
print (file_03_excel)
# 1、打开文件
x1 = xlrd.open_workbook(file_03_excel)
# 2、获取sheet对象
print ('sheet_names:', x1.sheet_names()) # 获取所有sheet名字
print ('sheet_number:', x1.nsheets ) # 获取sheet数量
print ('sheet_object:', x1.sheets() ) # 获取所有sheet对象
print ('By_name:', x1.sheet_by_name("Sheet1") ) # 通过sheet名查找
print ('By_index:', x1.sheet_by_index(0)) # 通过索引查找
# 读取第一个工作表
table = x1.sheets()[0]
# 统计行数
n_rows = table.nrows
print ('n_rows:', n_rows)
f1 = open('/Users/xulei/Desktop/int/intl.txt', 'w')
data = []
# 微信文章属性:wechat_name wechat_id title abstract url time read like number
for v in range(1, n_rows-1):
# 每一行数据形成一个列表
values = table.row_values(v)
# 列表形成字典
# data.append('<string name="',values[0],'">',values[1],'</string>')
key = values[0]
value = values[2]
string = '<string name=\"%s\">%s</string>'% (key, value)
f1.writelines(string)
f1.writelines('\n')
################国际化多语言替换end
def usage():
print ('Auto publish dmall')
print ('Usage:')
print (' --help Show help information')
print (' --publish=[release|debug] Auto publish build, release will auto git commit, debug will not')
print (' --patch Auto patch build')
print (' --upload Auto upload patch')
print (sys.argv)
intl(sys.argv[1])