1.android的xml文件转换json,需要用到xml2js插件
const xml2js = require('xml2js');
function xmlToJson(path){
const xmlData = fs.readFileSync(path);
let obj = {}
xml2js.parseString(xmlData, (err, result) => {
if (err) {
console.error(err);
return;
}
result.resources.string.forEach(item => {
obj[item.$.name] = item._
});
})
// 去除值里面的“”
for(const [key,val] of Object.entries(obj)){
const item = val.replace(/\"/g,'')
obj[key] = item
}
// obj = JSON.stringify(obj, null, 4)
return obj
}
例如xml文件
<resources>
<string name="ok">"确定"</string>
<string name="version_history">"历史版本记录"</string>
<string name="no_permission">没有权限</string>
</resources>
使用xmlToJson函数传入xml文件的路径,就可以将它转为json对象了
{
"ok":"确定",
"version_history":"历史版本记录",
"no_permission":"没有权限"
}
IOS的strings文件转换为json对象
function stringsToJson(path){
const content = fs.readFileSync(path,'utf-8')
// 去除注释和空行
const cleanedContent = content.replace(/\/\/.*\n/g, '').replace(/\n\s*\n/g, '\n');
// 解析内容为对象
const parsedContent = cleanedContent.split('\n').reduce((res, line) => {
// 匹配ios文件内容的格式
const match = line.match(/^\s*"(.*)"(\s*)?=(\s*)?"(.*)"(\s*)?;\s*$/);
if (match) {
res[match[1]] = match[4];
}
return res;
}, {});
// 将对象转换为 JSON 格式
// let jsonContent = JSON.stringify(parsedContent, null, 2);
return parsedContent
}
例如strings文件
"OK"="确定";
"version_history"="历史版本记录";
"no_permission"="没有权限";
使用stringsToJson函数传入ios文件地址,就可以转换为json对象了
{
"ok":"确定",
"version_history":"历史版本记录",
"no_permission":"没有权限"
}
PC的ts文件转成json对象,需要使用到xml2js插件模块
function tsToJson(path){
const xmlData = fs.readFileSync(path);
let obj = {}
xml2js.parseString(xmlData, (err, result) => {
if (err) {
console.error(err);
return;
}
result.TS.context.forEach(item => {
item.message.forEach((info)=>{
if(info.translation[0]._){
info.translation = info.translation[0]._
obj[info.source] = info.translation
}else{
info.translation = info.translation[0]
obj[info.source] = info.translation
}
})
});
})
// 去除值里面的“”
for(const [key,val] of Object.entries(obj)){
const item = val.replace(/\"/g,'')
obj[key] = item
}
// let CN = JSON.stringify(obj, null, 4)
return obj
}
例如文件格式是
<TS version="xxx" language="zh_CN">
<context>
<name>aaa</name>
<message>
<location filename="xxx" line="106"/>
<location filename="xxx" line="103"/>
<source>ok</source>
<translation>确定</translation>
</message>
<message>
<location filename="aaa" line="113"/>
<source>version_history</source>
<translation>历史版本记录</translation>
</message>
</context>
<context>
<name>ccc</name>
<message>
<location filename="bbb" line="113"/>
<source>no_permission</source>
<translation>没有权限</translation>
</message>
</context>
</TS>
使用tsToJson函数,传入文件地址,则可以转为json对象,具体格式不同,可以调试result.TS.context.forEach… 去更改到你想要拿到的数据格式,下面是我需要的key,value格式
{
"ok":"确定",
"version_history":"历史版本记录",
"no_permission":"没有权限"
}
如果需要转为excel,则只需要将json文件转为excel即可,可以看我上篇文章!
如果需要excel转为它们原本的格式,也只需要使用node-xlsx插件将excel转为json数据,再将json转为xml,strings,ts文件即可
例如,将json转为xml文件
function jsonToXml(json,target){
// console.log(CN)
// 将josn对象转换为XML文件
const builder = new xml2js.Builder({
// rootName:'resources',
renderOpts:{
pretty:true
},
// xmlns:{
// resources:'http"//schemas.android.com/tools'
// },
headless:true,
// attrkey:'name'
});
let xmlString = ''
for(const [key,value] of Object.entries(json)){
const xmlObject = {
string:{
$:{name:key},
_:value
}
}
const xml = builder.buildObject(xmlObject)
xmlString += xml + '\n'
}
xmlString = '<resources xmlns:tools="xxx">'+'\n'+ xmlString + '</resources>'
fs.writeFileSync(`${target}.xml`, xmlString);
}