-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_ImportManual.js
110 lines (104 loc) · 3.58 KB
/
static_ImportManual.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"use strict";
const cheerio = require("cheerio")
const glob = require("glob")
const path = require("path");
const fs = require("fs")
let settings = require("../setting.json")
let translation_directory = "../language/" + settings.group + "/www/"
let manual_directory = "../GMS2-Robohelp-en/"
let export_directory = "../build/"
let json_global = require("../language/" + settings.group + "/global.json");
if (fs.existsSync("logs.txt")) {
fs.rmSync("logs.txt")
}
function removeHtml(str) {
if (str === null) {
str=""
} else {
str=str.replace(/(<([^>]+)>)/ig, "{}");
str=str.replace(/\r\n/g, '\n');
str=str.replace(/\n/g, '');
str=str.replace(/ {2,}/g, ' ');
}
return str
}
function retHtml(str) {
var regex = /(<([^>]+)>)/ig
return str.match(regex);
}
function importTranslate(page, json, attr, fileName) {
let html
if(!attr){
html = page.html()
} else {
html = page.attr(attr)
}
let key = removeHtml(html)
let val = json[key]
if (val !== undefined && val.length) {
let f = retHtml(html)
let i = 0
if (f) f.forEach((v, k) => {
let tmp=val.replace("{"+ i +"}",v)
if(tmp === val){
val = val.replace("{}",v)
}else{
val = tmp
}
i++
})
if(!attr){
page.html(val)
} else {
page.attr(attr, val)
}
} else if (key !== val) {
let unTranslatedLog = "\"" + key + "\" is not translated in " + fileName + ".\n"
fs.appendFileSync("logs.txt", unTranslatedLog)
}
}
// 从记忆库替换导入翻译
glob(manual_directory + '**/*.htm', {}, (err, files) => {
if (err) {
console.log("错误:" + err)
} else {
for (let index = 0; index < files.length; index++) {
let filename = path.dirname(files[index]).split("/").splice(2).join("/") + "/" + path.basename(files[index], ".htm")
let normalizeName = path.normalize(files[index])
// console.log(filename)
let $ = cheerio.load(fs.readFileSync(files[index]).toString())
let json
if(fs.existsSync(files[index]) && fs.existsSync(translation_directory + filename + ".json")){
json = require(translation_directory + filename + ".json")
} else {
continue
}
let translation_lists = "p,h1,h2,h3,td:nth-child(3),li,a,div.dropspotnote,figcaption,.expandtext"
let translation_ignore = "#rh-topic-header,p.code"
$(translation_lists).each(function(){
importTranslate($(this).not(translation_ignore), json, null, filename)
})
$("div.footer a,h4,caption").each(function(){
importTranslate($(this), json_global, null, filename)
})
$("th,.warning,.important,.optional").each(function(){
importTranslate($(this), json_global, null, filename)
})
$(".tooltip").each(function(){
importTranslate($(this), json_global, "title", filename)
})
let generateDep
if (fs.existsSync(normalizeName + ".head")) {
generateDep = fs.readFileSync(normalizeName + ".head").toString()
} else {
generateDep = ""
}
$("head").prepend(generateDep)
fs.writeFile(export_directory + filename + ".htm", $.html(), (err) => {
if (err) {
console.log(err);
}
})
}
}
})