为什么会有这个操作: 最近老板要求做国际化,虽然不知道意义在哪里
,但因为之前的代码不规范,之前的代码里面showToast啊,setText啊等等,都是直接写的中文,并不是用的“R.string.xxx”这种引用资源的格式,所以,,,,,,要做国际化,就得按照标准来,因为要根据名字读取英文的资源文件这就坑了,这就坑了,pad项目,260多个java类,其中还不包括自定义dialog,自定义fragment,自定义控件和网络请求部分,工作量真的是非常的巨大啊,而且还是干体力活,首先,的把java类里面所以用到中文的地方找出来,其次,把这些中文放到xml资源文件里,然后翻译,最后把java类里面所有用中文的地方都用xml资源文件里对应的替代,尼玛,尼玛,尼玛,,,,,如果真是人工干,特么一个星期都干不完,俗话说“不会偷懒的程序猿不是好程序猿”,还好,有可以偷懒的方法。
同事用java写了个小的抓取工具,把所有java类里面使用中文的地方抓出来了,长下面这样:
很粗暴,但还是一目了然
然后,老大在Sublime上用正则表达式帮我把文件处理成了这样:
接下来就是取名字了,5000多行,要是手工去取,光是干这个事就要好久,于是,在网上找了找生成汉字首字母的方法,然后根据自己的实际需求,就有了下面的python代码:
import codecs
dic = { "柯": "ke1"}
def genDic():
length = len('柯') #测试汉字占用字节数,utf-8,汉字占用3字节.bg2312,汉字占用2字节
'''
intord = ord(ch[0:1])
if (intord >= 48 and intord <= 57):
return ch[0:1]
if (intord >= 65 and intord <=90 ) or (intord >= 97 and intord <=122):
return ch[0:1].lower()
ch = ch[0:length] #多个汉字只获取第一个
'''
global dic
with codecs.open('convert-utf-8.txt','r',encoding='utf-8') as f:
for line in f:
#print(line[0:length], line[length:len(line)-2])
#生成字典,将中文和名字以键值对的形式保存
dic[line[0:length]] = line[length:len(line)-2]
#if ch in line:
#return line[length:len(line)-2]
#return line[length:length+1]
def convert(ch):
if ch in dic:
return dic[ch][0:1]
return None
def initial(chinese):
r = ''
for c in chinese:
cc = convert(c)
if (cc != None):
r = r + cc
#print(r)
return r
genDic()
#print(len(dic))
file = codecs.open('result.xml','r',encoding='utf-8')
line = file.readline()
prefix = ''
while line:
#print("读取到的每一行:"+line)
line = file.readline()
if(len(line.strip()) == 0):
continue
elif("
#prefix 模块名字 JktjPttjPage
prefix = line.strip()
#print(len(line), prefix)
else:
#print(line)
chinese = line[line.find('>') + 1:len(line)]
chinese = chinese[0:chinese.find('
#print(chinese)
ini = initial(chinese)
if len(ini) >12:
#截取生成后的名字 最多保留12个字符
ini = ini[0:12]
name = "name="+prefix+'_'+ini
#print(name)
result = line.replace("name=", name)
print(result)
file.close()
#readline()
秒秒钟干完,哈哈哈哈,效果如下图:
很方便,哈哈
然后就要来替代java类里面的用的中文了,我新建了一个pathon类:
import codecs
import sys
import re
import unittest
dic = { "柯": "ke1"}
def genDic():
global dic
with codecs.open('result.xml','r',encoding='utf-8') as f:
for line in f:
#print(line[0:length], line[length:len(line)-2])
Chinese = line[line.find('">') + 2:]
Chinese =Chinese[0:Chinese.find('
name = line[line.find('"') + 1:]
name = name[0:name.find('"')]
#print(Chinese,name)
'''if (Chinese == '"请先进行体质档案查询!"'):
print(Chinese,name) '''
dic['"'+Chinese+'"'] = name
def hasChinese(s):
for i in range(len(s)):
#丂是字典convert-utf-8.txt里第一个字
if s[i:i+1] > '丂':
return True
return False
genDic()
def findfromDic(Chinese):
global dic
#print("Chinese:"+Chinese)
if Chinese in dic:
return dic[Chinese]
else:
print("No Chinese:"+Chinese)
return Nonefile = codecs.open('JbxxPage05.java','r',encoding='utf-8')line = file.readline()
while line:
#print("读取到的每一行:"+line)
# mContext.showToast("请先进行体质档案查询!");
#print("读取到的每一行:"+line)
line = file.readline()
if ('("' in line ) and hasChinese(line):
name = line.strip()
#print("name====="+name)
if (name.find('"') != -1):
name = name[name.find('"'):len(name)]
name = name[:-2]
#print("打印======:"+name.strip())
#mContext.getString(R.string.jbxxPage03_toast_qcxljzwy)
new_name = "mContext.getString(R.string."+findfromDic(name.strip())+")"
#print("等待替换的==========:"+findfromDic(name.strip()))
result = line.replace(name, new_name)
print(result)
else:
print(line)
file.close()
举个例子:
比如:我的JbxxPage05里面有这样一个toast:mContext.showToast("当前居民没有登记")
替换后的就是这样的:
mContext.showToast(mContext.getString(R.string.jbxxPage_common_toast_dqjmmydj))
运行效果就不上图了
python有批量操作的方法,也就是可以一次替换所有java类里面的用中文的地方,但因为我这里用中文的地方除了showToast和setText这两种格式,还有非常多其他奇怪的格式,为了避免弄错或遗漏,所以我还是一个一个来