2 Star 1 Fork 0

RogerAbyss / 国际化

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LocalizableStringsFileUtil.py 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
RogerAbyss 提交于 2018-06-21 16:42 . init
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from Log import Log
import codecs
import re
import io
def removeComments(s):
for x in re.findall(r'("[^\n]*"(?!\\))|(//[^\n]*$|/(?!\\)\*[\s\S]*?\*(?!\\)/)',s,8):s=s.replace(x[1],'')
return s
class LocalizableStringsFileUtil:
'iOS Localizable.strings file util'
@staticmethod
def writeToFile(keys,values,directory,additional):
if not os.path.exists(directory):
os.makedirs(directory)
Log.info("Creating iOS file:" + directory+"Localizable.strings")
fo = open(directory+"Localizable.strings", "wb")
for x in range(len(keys)):
if values[x] is None or values[x] == '':
Log.error("Key:" + keys[x] + "\'s value is None. Index:" + str(x + 1))
continue
key = keys[x].strip()
value = values[x]
content = "\"" + key + "\" " + "= " + "\"" + value + "\";\n"
fo.write(content);
if additional is not None:
fo.write(additional)
fo.close()
@staticmethod
def getKeysAndValues(path):
if path is None:
Log.error('file path is None')
return
# 1.Read localizable.strings
#file = codecs.open(path, 'r', 'utf-16')
#file = io.open(path, 'r', encoding='utf-8')
encodings = ['utf-8', 'utf-16']
for e in encodings:
try:
file = codecs.open(path, 'r', encoding=e)
string = file.read()
file.close()
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
print('opening the file with encoding: %s ' % e)
break
# string = file.read()
# file.close()
# 2.Remove comments
string = removeComments(string)
# 3.Split by ";
localStringList = string.split('\";')
list = [x.split(' = ') for x in localStringList]
# 4.Get keys & values
keys = []
values = []
for x in range(len(list)):
keyValue = list[x]
if len(keyValue) > 1:
key = keyValue[0].split('\"')[1]
value = keyValue[1][1:]
keys.append(key)
values.append(value)
return (keys,values)
1
https://gitee.com/rogerabyss/internationalization.git
git@gitee.com:rogerabyss/internationalization.git
rogerabyss
internationalization
国际化
master

搜索帮助