Repository navigation
Expand file tree
/
Copy pathi18n.js
More file actions
389 lines (349 loc) · 12.7 KB
/
Copy pathi18n.js
File metadata and controls
389 lines (349 loc) · 12.7 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// i18n Language Configuration
const translations = {
zh: {
// Navigation
nav: {
features: "特性",
quickstart: "快速开始",
docs: "文档",
github: "GitHub"
},
// Hero Section
hero: {
title: "灵活的Java爬虫框架",
subtitle: "WebMagic是一个简单灵活的爬虫框架,涵盖爬虫的整个生命周期:下载、URL管理、内容提取和持久化",
quickstart: "快速开始",
viewSource: "查看源码"
},
// Features Section
features: {
title: "核心特性",
simple: {
title: "简单易用",
desc: "提供简洁的API,快速上手,几行代码即可构建强大的爬虫程序"
},
flexible: {
title: "高度灵活",
desc: "模块化设计,易于扩展,支持自定义下载器、处理器和管道"
},
multithread: {
title: "多线程支持",
desc: "内置多线程支持,提高爬取效率,支持分布式部署"
},
annotation: {
title: "注解驱动",
desc: "支持注解方式配置POJO爬虫,让代码更加简洁优雅"
},
extraction: {
title: "强大提取",
desc: "支持XPath、CSS选择器、正则表达式等多种内容提取方式"
},
integration: {
title: "易于集成",
desc: "提供多种数据持久化方案"
}
},
// Quick Start Section
quickstart: {
title: "快速开始",
step1: "1. 添加Maven依赖",
step2: "2. 编写PageProcessor",
step3: "3. 启动爬虫"
},
// Docs Section
docs: {
title: "文档与资源",
chineseDocs: {
title: "📚 中文文档",
desc: "完整的中文使用指南和API文档",
link: "查看文档"
},
englishDocs: {
title: "📖 English Docs",
desc: "Complete English documentation and guides",
link: "View Docs"
},
javadoc: {
title: "🔍 JavaDoc",
desc: "详细的API参考文档",
link: "API文档"
},
community: {
title: "💬 社区支持",
desc: "GitHub Issues 和 QQ群讨论",
link: "提交问题"
}
},
// Footer
footer: {
description: "开源的Java爬虫框架",
project: "项目",
documentation: "文档",
community: "社区",
github: "GitHub",
chineseDocs: "中文文档",
english: "English",
issues: "Issues",
qqGroup: "QQ群",
copyright: "© 2024 WebMagic. Licensed under Apache 2.0"
}
},
en: {
// Navigation
nav: {
features: "Features",
quickstart: "Quick Start",
docs: "Docs",
github: "GitHub"
},
// Hero Section
hero: {
title: "Flexible Java Web Crawler Framework",
subtitle: "WebMagic is a scalable crawler framework that covers the whole lifecycle of crawler: downloading, url management, content extraction and persistent",
quickstart: "Quick Start",
viewSource: "View Source"
},
// Features Section
features: {
title: "Core Features",
simple: {
title: "Simple & Easy",
desc: "Simple core with high flexibility. Build powerful crawlers with just a few lines of code"
},
flexible: {
title: "Highly Flexible",
desc: "Modular design for easy extension. Support custom downloaders, processors and pipelines"
},
multithread: {
title: "Multi-thread Support",
desc: "Built-in multi-threading support for better performance and distributed deployment"
},
annotation: {
title: "Annotation Driven",
desc: "Annotation-based POJO crawler configuration for cleaner and more elegant code"
},
extraction: {
title: "Powerful Extraction",
desc: "Support XPath, CSS selectors, regex and more for content extraction"
},
integration: {
title: "Easy Integration",
desc: "multiple data persistence solutions"
}
},
// Quick Start Section
quickstart: {
title: "Quick Start",
step1: "1. Add Maven Dependency",
step2: "2. Write PageProcessor",
step3: "3. Start Spider"
},
// Docs Section
docs: {
title: "Documentation & Resources",
chineseDocs: {
title: "📚 中文文档",
desc: "Complete Chinese documentation and API reference",
link: "View Docs"
},
englishDocs: {
title: "📖 English Docs",
desc: "Complete English documentation and guides",
link: "View Docs"
},
javadoc: {
title: "🔍 JavaDoc",
desc: "Detailed API reference documentation",
link: "API Docs"
},
community: {
title: "💬 Community Support",
desc: "GitHub Issues and community discussions",
link: "Submit Issues"
}
},
// Footer
footer: {
description: "Open source Java web crawler framework",
project: "Project",
documentation: "Documentation",
community: "Community",
github: "GitHub",
chineseDocs: "Chinese Docs",
english: "English",
issues: "Issues",
qqGroup: "QQ Group",
copyright: "© 2024 WebMagic. Licensed under Apache 2.0"
}
}
};
// Logger Class
class Logger {
constructor() {
this.logs = [];
this.maxLogs = 1000;
this.logLevels = {
ERROR: 0,
WARN: 1,
INFO: 2,
DEBUG: 3
};
this.currentLevel = this.logLevels.INFO;
}
log(level, message, data = null) {
if (this.logLevels[level] <= this.currentLevel) {
const timestamp = new Date().toISOString();
const logEntry = {
timestamp,
level,
message,
data,
url: window.location.href,
userAgent: navigator.userAgent
};
this.logs.push(logEntry);
// Keep only the latest logs
if (this.logs.length > this.maxLogs) {
this.logs = this.logs.slice(-this.maxLogs);
}
// Console output
const consoleMessage = `[${timestamp}] ${level}: ${message}`;
switch(level) {
case 'ERROR':
console.error(consoleMessage, data);
break;
case 'WARN':
console.warn(consoleMessage, data);
break;
case 'INFO':
console.info(consoleMessage, data);
break;
case 'DEBUG':
console.debug(consoleMessage, data);
break;
default:
console.log(consoleMessage, data);
}
}
}
error(message, data) { this.log('ERROR', message, data); }
warn(message, data) { this.log('WARN', message, data); }
info(message, data) { this.log('INFO', message, data); }
debug(message, data) { this.log('DEBUG', message, data); }
getLogs() { return this.logs; }
exportLogs() {
const logsJson = JSON.stringify(this.logs, null, 2);
const blob = new Blob([logsJson], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `webmagic-logs-${new Date().toISOString().split('T')[0]}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
setLevel(level) {
if (this.logLevels.hasOwnProperty(level)) {
this.currentLevel = this.logLevels[level];
this.info(`Log level set to ${level}`);
}
}
}
// i18n Class
class I18n {
constructor() {
this.currentLang = this.detectLanguage();
this.translations = translations;
this.logger = new Logger();
this.logger.info('I18n system initialized', { language: this.currentLang });
}
detectLanguage() {
// Check localStorage first
const savedLang = localStorage.getItem('webmagic-lang');
if (savedLang && ['zh', 'en'].includes(savedLang)) {
return savedLang;
}
// Check browser language
const browserLang = navigator.language || navigator.userLanguage;
if (browserLang.startsWith('zh')) {
return 'zh';
}
return 'en'; // Default to English
}
setLanguage(lang) {
if (!this.translations[lang]) {
this.logger.error(`Language ${lang} not supported`);
return false;
}
this.currentLang = lang;
localStorage.setItem('webmagic-lang', lang);
this.logger.info(`Language changed to ${lang}`);
this.updateDOM();
return true;
}
getCurrentLanguage() {
return this.currentLang;
}
t(key) {
const keys = key.split('.');
let value = this.translations[this.currentLang];
for (const k of keys) {
if (value && typeof value === 'object' && value.hasOwnProperty(k)) {
value = value[k];
} else {
this.logger.warn(`Translation key not found: ${key}`);
return key;
}
}
return value || key;
}
updateDOM() {
// Update all elements with data-i18n attribute
const elements = document.querySelectorAll('[data-i18n]');
elements.forEach(element => {
const key = element.getAttribute('data-i18n');
const translation = this.t(key);
if (element.tagName === 'INPUT' && element.type === 'text') {
element.placeholder = translation;
} else {
element.textContent = translation;
}
});
// Update HTML lang attribute
document.documentElement.lang = this.currentLang === 'zh' ? 'zh-CN' : 'en';
// Update page title and meta description
document.title = this.currentLang === 'zh'
? 'WebMagic - 灵活的Java爬虫框架'
: 'WebMagic - Flexible Java Web Crawler Framework';
// Update meta description
const metaDesc = document.querySelector('meta[name="description"]');
if (metaDesc) {
metaDesc.content = this.currentLang === 'zh'
? 'WebMagic是一个简单灵活的Java爬虫框架,支持多线程、分布式爬虫,提供简洁的API和强大的扩展能力'
: 'WebMagic is a scalable Java web crawler framework with simple core, high flexibility, multi-threading support and easy integration capabilities';
}
// Update language toggle button
const langToggle = document.querySelector('.lang-toggle');
if (langToggle) {
langToggle.textContent = this.currentLang === 'zh' ? 'EN' : '中文';
}
this.logger.info('DOM updated for language change', {
newLanguage: this.currentLang,
title: document.title
});
}
getAvailableLanguages() {
return Object.keys(this.translations);
}
}
// Global instances
const logger = new Logger();
const i18n = new I18n();
// Export for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
module.exports = { I18n, Logger, i18n, logger };
} else {
window.i18n = i18n;
window.logger = logger;
}