Skip to content

Commit

Permalink
feat(i18n): support i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
sunYanxl committed Aug 6, 2018
1 parent b7633dc commit 275f110
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 26 deletions.
9 changes: 5 additions & 4 deletions bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ process.on('uncaughtException', function(e) {


const logger = require('logger');
const lang = require('i18n/lang.js');
const http = require('http');
const https = require('https');
const util = require('util');
Expand Down Expand Up @@ -406,15 +407,15 @@ function afterCpu80(cpuUsed) {
timeout: 5000
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
const key = `cpu80.v4:${serverInfo.intranetIp}`;
let content = `<strong>单核CPU${serverInfo.cpu}使用率为${cpuUsed}超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
let content = `<strong>${lang.__('mail.singleCoreCpu')}${serverInfo.cpu}${lang.__('mail.CPUUsageRate')}${cpuUsed}${lang.__('mail.UsageRateGreaterThan80')}${lang.__('mail.fiveSecondsCpuProfiler')}</strong>`;
let str = '';

if (data) {
str = data;
str = str.replace(/</g, '&gt;');
str = str.replace(/\r\n|\r|\n/g, '<br>');

content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
content += `<p><strong>${lang.__('mail.processSnapshot')}:</strong></p><pre style="font-size:12px">${str}</pre>`;
}


Expand All @@ -427,8 +428,8 @@ function afterCpu80(cpuUsed) {
'to': config.mailTo,
'cc': config.mailCC,
'runtimeType': 'CPU',
'msgInfo': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为${cpuUsed}超过80%`,
'title': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为${cpuUsed}超过80%`,
'msgInfo': `${business.module}[CPU]${serverInfo.intranetIp}${lang.__('mail.singleCoreCpu')}${serverInfo.cpu}${lang.__('mail.CPUUsageRate')}${cpuUsed}${lang.__('mail.UsageRateGreaterThan80%')}`,
'title': `${business.module}[CPU]${serverInfo.intranetIp}${lang.__('mail.singleCoreCpu')}${serverInfo.cpu}${lang.__('mail.CPUUsageRate')}${cpuUsed}${lang.__('mail.UsageRateGreaterThan80%')}`,
'content': content,
'attachment': result ? {
fileType: true,
Expand Down
3 changes: 2 additions & 1 deletion bin/proxy/http.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


const logger = require('logger');
const lang = require('i18n/lang.js');
const domain = require('domain');
const serverInfo = require('serverInfo.js');
const config = require('./config.js');
Expand Down Expand Up @@ -225,7 +226,7 @@ module.exports = function(req, res) {
}

const key = err.message;
const content = `<p><strong>错误堆栈</strong></p><p><pre><code>${err.stack}</code></pre></p>`;
const content = `<p><strong>${lang.__('mail.errorStack')}</strong></p><p><pre><code>${err.stack}</code></pre></p>`;
mail.SendMail(key, 'js', 600, {
'title': key,
'runtimeType': 'Error',
Expand Down
9 changes: 5 additions & 4 deletions bin/proxy/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


const logger = require('logger');
const lang = require('i18n/lang.js');
const config = require('./config.js');
const cluster = require('cluster');
const cpuUtil = require('util/cpu.js');
Expand Down Expand Up @@ -57,7 +58,7 @@ process.on('warning', function(warning) {
process.on('unhandledRejection', (errorOrReason, currPromise) => {
const errStr = String(errorOrReason && errorOrReason.stack || JSON.stringify(errorOrReason));
const key = String(errorOrReason && errorOrReason.message);
const content = `<p><strong>错误堆栈</strong></p><p><pre><code>${errStr}</code></pre></p>`;
const content = `<p><strong>${lang.__('mail.errorStack')}</strong></p><p><pre><code>${errStr}</code></pre></p>`;

// 恢复上下文
if (currPromise && currPromise.domain) {
Expand Down Expand Up @@ -285,9 +286,9 @@ function checkWorkerAlive() {
'to': config.mailTo,
'cc': config.mailCC,
'runtimeType': 'Memory',
'msgInfo': `${serverInfo.intranetIp} 内存超限,服务已重启。请开发人员关注是否存在内存泄露`,
'title': `${serverInfo.intranetIp} 内存超限告警`,
'content': `<p><strong>${serverInfo.intranetIp} 内存超限,服务已重启。请开发人员关注是否存在内存泄露</strong></p>`
'msgInfo': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}`,
'title': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingWaring')}`,
'content': `<p><strong>${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}</strong></p>`
});

restartWorker(worker);
Expand Down
3 changes: 2 additions & 1 deletion bin/proxy/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const WebSocket = require('ws');
const WSServer = require('./webSocketServer');
const logger = require('logger');
const lang = require('i18n/lang.js');
const domain = require('domain');
const Context = require('runtime/Context');
const contextMod = require('context.js');
Expand Down Expand Up @@ -111,7 +112,7 @@ function bind_listen(server) {

if (err && err.stack && err.message) {
const key = err.message;
const content = `<p><strong>错误堆栈</strong></p><p><pre><code>${err.stack}</code></pre></p>`;
const content = `<p><strong>${lang.__('mail.errorStack')}</strong></p><p><pre><code>${err.stack}</code></pre></p>`;
mail.SendMail(key, 'js', 600, {
'title': key,
'runtimeType': 'Error',
Expand Down
5 changes: 3 additions & 2 deletions bin/tsw/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


const logger = require('logger');
const lang = require('i18n/lang.js');
const Deferred = require('util/Deferred');
const http = require('http');
const https = require('https');
Expand Down Expand Up @@ -1103,12 +1104,12 @@ Ajax.prototype.doRequest = function(opt) {
});

const key = [window.request.headers.host, context.mod_act, parseErr.message].join(':');
const content = `<p><strong>错误堆栈</strong></p><p><pre><code>${parseErr.stack}</code></pre></p>`;
const content = `<p><strong>${lang.__('mail.errorStack')}</strong></p><p><pre><code>${parseErr.stack}</code></pre></p>`;

require('util/mail/mail.js').SendMail(key, 'js data', 1800, {
'title': key,
'content': content,
'msgInfo': '错误堆栈:\n' + parseErr.stack
'msgInfo': `${lang.__('mail.errorStack')}:\n` + parseErr.stack
});

return;
Expand Down
32 changes: 32 additions & 0 deletions bin/tsw/i18n/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* !
* Tencent is pleased to support the open source community by making Tencent Server Web available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

const i18n = require('i18n');
const fs = require('fs');
const config = require('config');
const language = config.language || 'cn';
const forder = __dirname + '/../../../locales';

const getSupportLang = function() {
const langList = (fs.readdirSync(forder) || []).map(function(item) {
return (item.split('.'))[0];
});
return langList;
};

i18n.configure({
locales: getSupportLang(),
defaultLocale: 'cn',
extension: '.json',
objectNotation: true,
directory: forder,
register: this
});

i18n.setLocale(language);

23 changes: 12 additions & 11 deletions bin/tsw/runtime/CCFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

const config = require('config');
const logger = require('logger');
const lang = require('i18n/lang.js');
const httpUtil = require('util/http');
const serverInfo = require('serverInfo.js');
const isTST = require('util/isTST.js');
Expand Down Expand Up @@ -240,25 +241,25 @@ this.check = function (req, res) {
let title = '';

if (config.CCIPLimitAutoBlock) {
title = `[IP聚集自动拉黑周知][${cache.ipCacheLast.StdX10}%]${max.ip}`;
title = `[${lang.__('mail.IPAggregationNotice')}][${cache.ipCacheLast.StdX10}%]${max.ip}`;
this.addBlackList(max.ip);
} else {
title = `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${max.ip}`;
title = `[${lang.__('mail.IPAggregationWarning')}][${cache.ipCacheLast.StdX10}%]${max.ip}`;
}

mail.SendMail(key, 'TSW', 3600, {
'to': config.mailTo,
'cc': config.mailCC,
'title': title,
'content': '<p><strong>IP聚集相关信息,详情请参阅文档: </strong> https://tswjs.org/doc/api/ipCCFinder </p>'
+'<p><strong>服务器IP:</strong>' + serverInfo.intranetIp + '</p>'
+ '<p><strong>恶意IP:</strong>' + max.ip + '</p>'
+ '<p><strong>自动拉黑:</strong>' + (config.CCIPLimitAutoBlock ? '是' : '否') + '</p>'
+ '<p><strong>IP聚集度:</strong>' + cache.ipCacheLast.StdX10 + '%</p>'
+ '<p><strong>告警阈值:</strong>' + CCIPLimit + '</p>'
+ '<p><strong>正常值:</strong>5-50</p>'
+ '<p><strong>检测耗时:</strong>' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start) / 1000, 10) + 's</p>'
+ '<p><strong>证据列表:</strong></p>'
'content': `<p><strong>${lang.__('mail.viewDocsForIPAggregation')}: </strong> https://tswjs.org/doc/api/ipCCFinder </p>`
+ `<p><strong>${lang.__('mail.serverIP')}:</strong>${serverInfo.intranetIp}</p>`
+ `<p><strong>${lang.__('mail.maliciousIP')}:</strong>${max.ip}</p>`
+ `<p><strong>${lang.__('mail.autoIntoBlackList')}:</strong>` + (config.CCIPLimitAutoBlock ? `${'mail.yes'}` : `${'mail.no'}`) + '</p>'
+ `<p><strong>${lang.__('mail.IPAggregationDegree')}:</strong>${cache.ipCacheLast.StdX10}%</p>`
+ `<p><strong>${lang.__('mail.warningThreshold')}:</strong>${CCIPLimit}</p>`
+ `<p><strong>${lang.__('mail.normalValue')}:</strong>5-50</p>`
+ `<p><strong>${lang.__('mail.testingTimeConsuming')}:</strong>${parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start) / 1000, 10)}s</p>`
+ `<p><strong>${lang.__('mail.evidenceList')}:</strong></p>`
+ content
});

Expand Down
25 changes: 25 additions & 0 deletions locales/cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"mail": {
"errorStack": "错误堆栈",
"memoryExceedingWaring": " 内存超限告警",
"memoryExceedingTips": "内存超限,服务已重启,请开发人员关注是否存在内存泄露。",
"singleCoreCpu": "单核CPU",
"CPUUsageRate": "CPU使用率",
"UsageRateGreaterThan80%": "使用率超过80%",
"fiveSecondsCpuProfiler": "最近5秒钟CPU Profiler见附件",
"processSnapshot": "进程快照",
"IPAggregationNotice": "IP聚集自动拉黑周知",
"IPAggregationWarning": "IP聚集告警",
"viewDocsForIPAggregation": "IP聚集相关信息,详情请参阅文档",
"serverIP": "服务器IP",
"maliciousIP": "恶意IP",
"autoIntoBlackList":"自动拉黑",
"IPAggregationDegree":"IP聚集度",
"warningThreshold":"告警阈值",
"normalValue": "正常值",
"testingTimeConsuming":"检测耗时",
"evidenceList": "证据列表",
"yes": "",
"no": ""
}
}
25 changes: 25 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"mail": {
"errorStack": "Error stack",
"memoryExceedingWaring": "Memory exceeding waring",
"memoryExceedingContent": "Memory exceeding,the server has been restarted,please pay attention to whether the memory is leaked",
"singleCoreCpu": "Mononuclear CPU",
"CPUUsageRate": "CPU usage rate",
"UsageRateGreaterThan80%": "usage rate of over 80 per cent",
"fiveSecondsCpuProfiler": "you can view the last 5 seconds CPU Profiler in attachment",
"processSnapshot": "Process snapshot",
"IPAggregationNotice": "Notice:Auto pull aggregated IP into blackList",
"IPAggregationWarning": "IP aggregation warning",
"viewDocsForIPAggregation": "You can view the documents to get related information and detail of IP aggregation",
"serverIP": "Server IP",
"maliciousIP": "Malicious IP",
"autoIntoBlackList":"Auto pull aggregated IP into blackList",
"IPAggregationDegree":"Degree of IP aggregation",
"warningThreshold":"Threshold of warning",
"normalValue": "Normal value",
"testingTimeConsuming":"Time consuming for testing",
"evidenceList": "Evidence list",
"yes": "yes",
"no": "no"
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
"url": "git+https://github.com/tencent/TSW.git"
},
"dependencies": {
"archiver": "~0.16.0",
"cookie": "~0.3.1",
"i18n": "^0.8.3",
"memcached": "~0.2.8",
"qs": "~6.5.1",
"ws": "~2.2.1",
"archiver": "~0.16.0",
"memcached": "~0.2.8"
"ws": "~2.2.1"
},
"devDependencies": {
"@commitlint/cli": "^6.2.0",
Expand Down

0 comments on commit 275f110

Please sign in to comment.