-
-
Save yuanoook/39d8813c7eea9fe31ad0e20e2972211a to your computer and use it in GitHub Desktop.
for keepwork angular i18n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const walk = require('walk') | |
const fs = require('fs') | |
const path = require('path') | |
const cheerio = require('cheerio') | |
const targetDir = "../wikicraft/www/wiki/html"; | |
const options = { | |
followLinks: false, | |
filters: ["articles", "templates", "tutorial"] | |
}; | |
const emptyCall = () => {}; | |
var all_matchs = []; | |
var all_matched_strs = []; | |
var all_target_strs = []; | |
var all_manual_target_strs = []; | |
var all_auto_handle_target_strs = []; | |
walkerRun((filepath, data, next) => { | |
var result = data; | |
var search_line_regex = /\<([^\>\s]+)([^\>]*)\>([^\<\>]*[\u4e00-\u9fff]+[^\<\>]*)\<\/\1\>/g; | |
var search_line_regex_single = /\<([^\>\s]+)([^\>]*)\>([^\<\>]*[\u4e00-\u9fff]+[^\<\>]*)\<\/\1\>/; | |
var match = result.match(search_line_regex); | |
if (!match) return next(); | |
all_matchs = all_matchs.concat(match); | |
result = result.replace(search_line_regex, matchedStr => { | |
var matchs = matchedStr.match(search_line_regex_single); | |
var tag = matchs[1]; | |
var attrs = matchs[2]; | |
var target_str = matchs[3]; | |
all_target_strs.push(target_str); | |
all_matched_strs.push(matchedStr); | |
if (/[\{\'\"]/.test(target_str)) { | |
all_manual_target_strs.push(target_str); | |
return matchedStr; | |
} else { | |
all_auto_handle_target_strs.push(target_str); | |
} | |
return matchedStr.replace(search_line_regex_single, `<${ tag }${ attrs } translate="${ target_str }"></${ tag }>`); | |
}); | |
fs.writeFile(filepath, result, next); | |
}, () => { | |
console.log("walker all done"); | |
fs.writeFile('./all_matchs.json', JSON.stringify(all_matchs, null, 2), emptyCall); | |
fs.writeFile('./all_matched_strs.json', JSON.stringify(all_matched_strs, null, 2), emptyCall); | |
fs.writeFile('./all_target_strs.json', JSON.stringify(all_target_strs, null, 2), emptyCall); | |
fs.writeFile('./all_manual_target_strs.json', JSON.stringify(all_manual_target_strs, null, 2), emptyCall); | |
fs.writeFile('./all_auto_handle_target_strs.json', JSON.stringify(all_auto_handle_target_strs, null, 2), emptyCall); | |
}); | |
function walkerRun(callback, endCallback) { | |
var walker = walk.walk(targetDir, options); | |
walker.on("file", function (root, fileStats, next) { | |
const filepath = path.join(root, fileStats.name); | |
if (!/\.html$/.test(filepath)) return next(); | |
if (/^(test|testcdn)\.html$/.test(fileStats.name)) return next(); | |
fs.readFile(filepath, 'utf-8', function (err, data) { | |
if (err) { | |
console.log(root); | |
console.log(err); | |
console.log(fileStats); | |
} | |
callback ? callback(filepath, data, next) : next(); | |
}) | |
}); | |
walker.on("errors", function (root, nodeStatsArray, next) { | |
console.log(nodeStatsArray); | |
next(); | |
}); | |
walker.on("end", function () { | |
endCallback && endCallback(); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment