-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (149 loc) · 5.57 KB
/
script.js
File metadata and controls
178 lines (149 loc) · 5.57 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
// 导入语言数据
import languageData from './languageData.js';
import languageCodes from './languageCodes.js';
// 全局变量
let currentLang = 'zh-CN';
// DOM元素
let languageCards, searchInput, description, noResults, currentLanguageText, languageToggle, languageDropdown;
// 初始化应用
async function initApp() {
try {
// 获取DOM元素
languageCards = document.getElementById('languageCards');
searchInput = document.getElementById('searchInput');
description = document.getElementById('description');
noResults = document.getElementById('noResults');
currentLanguageText = document.getElementById('currentLanguage');
languageToggle = document.getElementById('languageToggle');
languageDropdown = document.getElementById('languageDropdown');
// 显示加载状态
languageCards.innerHTML = `
<div class="col-span-full text-center py-8">
<i class="fas fa-spinner fa-spin text-3xl text-primary mb-4"></i>
<p>加载中...</p>
</div>
`;
// 从localStorage获取语言偏好
const savedLang = localStorage.getItem('appLanguage');
if (savedLang && languageData[savedLang]) {
currentLang = savedLang;
}
// 使用页面设置的默认语言
currentLang = window.defaultLang || 'zh-CN';
// 更新SEO元标签
document.title = languageData[currentLang].metaTitle;
document.querySelector('meta[name="description"]').setAttribute('content', languageData[currentLang].metaDescription);
document.querySelector('meta[name="keywords"]').setAttribute('content', languageData[currentLang].metaKeywords);
document.querySelector('meta[property="og:title"]').setAttribute('content', languageData[currentLang].metaTitle);
document.querySelector('meta[property="og:description"]').setAttribute('content', languageData[currentLang].metaDescription);
// 设置HTML lang属性
document.documentElement.lang = currentLang;
// 渲染页面
renderPage();
// 绑定事件
bindEvents();
} catch (error) {
console.error('加载数据失败:', error);
languageCards.innerHTML = `
<div class="col-span-full text-center py-8">
<i class="fas fa-exclamation-triangle text-3xl text-red-500 mb-4"></i>
<h3 class="text-xl font-medium mb-2">加载数据失败</h3>
<p class="text-gray-500">加载失败: ${error.message}</p>
</div>
`;
}
}
// 渲染页面
function renderPage() {
// 更新页面文本
description.textContent = languageData[currentLang].description;
searchInput.placeholder = languageData[currentLang].searchPlaceholder;
if (currentLang === 'zh-CN') {
currentLanguageText.textContent = '中文'
}
if (currentLang === 'en-US') {
currentLanguageText.textContent = 'English'
}
if (currentLang === 'th-TH') {
currentLanguageText.textContent = 'ไทย'
}
if (currentLang === 'id-ID') {
currentLanguageText.textContent = 'Indonesia'
}
if (currentLang === 'es-ES') {
currentLanguageText.textContent = 'Español'
}
// 执行搜索
performSearch();
}
// 执行搜索
function performSearch() {
const searchTerm = searchInput.value.toLowerCase();
const filteredCodes = searchTerm
? languageCodes.filter(code =>
code.code.toLowerCase().includes(searchTerm)
)
: languageCodes;
// 显示结果或无结果提示
if (filteredCodes.length === 0) {
languageCards.innerHTML = '';
noResults.classList.remove('hidden');
} else {
noResults.classList.add('hidden');
renderLanguageCards(filteredCodes);
}
}
// 渲染语言卡片
function renderLanguageCards(codes) {
languageCards.innerHTML = codes.map(code => `
<div class="bg-white rounded-xl shadow-md overflow-hidden smooth-transition hover:card-hover">
<div class="p-6">
<div class="flex justify-between items-start mb-4">
<div>
<h3 class="text-lg font-semibold text-dark">${currentLang === 'zh-CN' ? code.zh : currentLang === 'th-TH' ? code.th : currentLang === 'id-ID' ? code.id : code.en}</h3>
<p class="text-sm text-gray-500 mt-1">${code.code}</p>
</div>
<div class="bg-primary/10 text-primary px-3 py-1 rounded-full text-xs font-medium">
ISO 639
</div>
</div>
${currentLang !== 'en-US' ? `
<div class="flex justify-between text-sm text-gray-600">
<span>${'English Name'}</span>
<span class="font-medium">${code.en}</span>
</div>
` : ''}
${(() => {
const langConfig = languageData[currentLang];
const langCode = currentLang.split('-')[0];
if (langConfig && langConfig.localName && code[langCode]) {
return `
<div class="flex justify-between text-sm text-gray-600 mt-2">
<span>${langConfig.localName}</span>
<span class="font-medium">${code[langCode]}</span>
</div>
`;
}
return '';
})()}
</div>
</div>
`).join('');
}
// 绑定事件处理程序
function bindEvents() {
// 搜索输入事件
searchInput.addEventListener('input', performSearch);
// 语言切换下拉菜单
languageToggle.addEventListener('click', () => {
languageDropdown.classList.toggle('hidden');
});
// 点击页面其他地方关闭下拉菜单
document.addEventListener('click', (e) => {
if (!languageToggle.contains(e.target) && !languageDropdown.contains(e.target)) {
languageDropdown.classList.add('hidden');
}
});
}
// 启动应用
document.addEventListener('DOMContentLoaded', initApp);