-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (124 loc) · 3.85 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
"use strict";
/*
* Copyright (c) 2024 [email protected]
* index.ts
*
* <en_us>
* Created on Wed Jan 10 2024 20:09:00
* Feature: Translate file content from Simplified Chinese to Traditional Chinese.</en_us>
*
* <zh_cn>
* 创建:2024年1月10日 20:09:00
* 功能:翻译文件中的简体中文到繁体中文。
* </zh_cn>
*
* <zh_tw>
* 創建:2024年1月10日 20:09:00
* 功能:翻譯文件中的簡體中文到繁體中文。
* </zh_tw>
*/
/* readme.md
### Usage1
```bash
npm i -g @dishanqian/cn2tw
cn2tw zh_cn.txt zh_tw.txt
```
### Usage2
```bash
npm i @dishanqian/cn2tw
```
```javascript
const { convert } = require('@dishanqian/cn2tw');
// convert('sourceFilename', 'goalFilename');
convert('zh_cn.txt', 'zh_tw.txt');
```
### Develop
```bash
git clone git+ssh://[email protected]/anqisoft/js_node_cn2tw
cd js_node_cn2tw
npm i @dishanqian/cn_and_tw
# test
node test/index.js zh_cn.txt zh_tw.txt
```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
const fs = require('fs');
const { cn2tw } = require('@dishanqian/cn_and_tw');
/**
* <en_us>Convert Simplified Chinese to Traditional Chinese in a file</en_us>
* <zh_cn>转换文件中的简体中文到繁体中文</zh_cn>
* <zh_tw>轉換檔案中的簡體中文到繁體中文</zh_tw>
*
* @param sourceFilename string <en_us>Original document, including simplified Chinese</en_us><zh_cn>原始文件,含简体中文</zh_cn><zh_tw>原始文件,含簡體中文</zh_tw>
* @param goalFilename string <en_us>Target traditional Chinese file</en_us><zh_cn>目标繁体文件</zh_cn><zh_tw>目標繁體文件</zh_tw>
*
* @returns boolean <en_us>Conversion is successful or not.</en_us><zh_cn>转换成功与否</zh_cn><zh_tw>轉換成功與否</zh_tw>
*/
function convert(sourceFilename, goalFilename) {
// https://nodejs.org/api/fs.html
const FILE_WRITE_MODE = { mode: 0o777 };
try {
const SOURCE_CONTENT = fs.readFileSync(sourceFilename).toString();
// console.log(SOURCE_CONTENT);
fs.writeFileSync(goalFilename, cn2tw(SOURCE_CONTENT), FILE_WRITE_MODE);
return true;
}
catch (e) {
console.error(e);
return false;
}
}
exports.convert = convert;
(() => {
const { argv, exit } = require('node:process');
const args = argv.slice(2);
let needShowTips = false;
args.forEach(arg => {
switch(arg.toLowerCase()) {
case '/help':
case '/h':
case '/?':
case '-help':
case '-h':
case '-?':
case '--help':
case '--h':
case '--?':
needShowTips = true;
break;
case '--v':
case '--version':
case '-v':
case '-version':
case '/v':
case '/version':
console.log('0.0.5');
exit();
break;
default:
break;
}
});
if(args.length < 2 || needShowTips) {
console.log('Copyright (c) 2024 [email protected]\n'.concat(
'Translate file content from Simplified Chinese to Traditional Chinese.\n\n',
'usage:\n',
'cn2tw sourceFilename goalFilename\n',
'eg:\n',
'cn2tw zh_cn.txt zh_tw.txt\n\n',
'show this tips:\n',
'cn2tw /?|/h|/help|-?|-h|-help|--?|--h|--help\n',
'show version:\n',
'cn2tw /v|/version|-v|-version|--v|--version\n',
));
exit();
}
const START_DATE = new Date();
const [source, goal] = args;
console.log(convert(source, goal));
const END_DATE = new Date();
const USED_MILLISECONDS = END_DATE.getTime() - START_DATE.getTime();
console.log('Used', USED_MILLISECONDS, 'milliseconds, it is equivalent to ', parseFloat((USED_MILLISECONDS / 1000).toFixed(4)));
})();