nodejs版實現properties后綴文件解析


1、propertiesParser.js

let readline = require('readline');
let fs = require('fs');
// properties文件路徑 let local
= 'zh', url = local == 'zh' ? /public/messages_zh.properties' : '/public/messages_en.properties';
// Unicode 碼轉為中文 let toGB2312
= function(str) { return unescape(str.replace(/\\u/gi, '%u')); }
// properties文件解析 let parseProps
= function() { let fRead = fs.createReadStream(url), readlineObj = readline.createInterface({ input: fRead }); readlineObj.on('line', (line) => { var tmp = line.toString(), index = tmp.indexOf('#'); // 拆分key、value if (index != 0) { let strIdx = tmp.indexOf('='), key = tmp.substr(0, strIdx), value = tmp.substr(strIdx + 1); // 將拆分key、value數據存儲到node的全局變量global下的local屬性 global.local[key] = local == 'zh' ? toGB2312(value) : value; } })
// 文件讀取結束 readlineObj.on(
'close', () => { console.log('readline close... parsed properties!'); }) } module.exports = { parseProps }

 

網上也有jquery.i18n.properties等方案,感覺引入過多冗余資源,索性手擼一個純js方法實現解析,希望也能幫到遇到類似問題的人 ^_^ 。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM