add i18n step1

This commit is contained in:
wjqserver 2025-07-01 07:04:39 +08:00
parent 34d553a890
commit 79e3db6078
23 changed files with 2309 additions and 450 deletions

View file

@ -2,13 +2,12 @@
import { state } from './state.js';
import { api } from './api.js';
import { theme, activateNav } from './common.js';
import { initializePage } from './common.js';
import { t } from './locale.js';
import { notification } from './notifications.js';
import { initCaddyStatus } from './caddy.js';
import { DOMElements, switchView, renderConfigList, addKeyValueInput, addSingleInput, fillForm, showRenderedConfig, updateCaddyStatusView, updateSegmentedControl, updateServiceModeView, updateMultiUpstreamView, createPresetSelectionModal } from './ui.js';
const POLLING_INTERVAL = 5000;
let caddyStatusInterval;
// --- 事件处理与逻辑流 (所有 handle* 函数保持不变) ---
function getFormStateAsString() {
const formData = new FormData(DOMElements.configForm);
@ -27,14 +26,18 @@ function getFormStateAsString() {
}
async function attemptExitForm() {
if (getFormStateAsString() !== state.initialFormState) {
if (await notification.confirm('您有未保存的更改。确定要放弃吗?')) switchView(DOMElements.configListPanel);
} else switchView(DOMElements.configListPanel);
if (await getFormStateAsString() !== state.initialFormState) {
if (await notification.confirm(t('dialogs.unsaved_changes_msg'), t('dialogs.unsaved_changes_title'))) {
switchView(DOMElements.configListPanel);
}
} else {
switchView(DOMElements.configListPanel);
}
}
async function handleLogout() {
if (!await notification.confirm('您确定要退出登录吗?')) return;
notification.toast('正在退出...', 'info');
if (!await notification.confirm(t('dialogs.logout_msg'))) return;
notification.toast(t('toasts.logout_processing'), 'info');
setTimeout(() => { window.location.href = `/v0/api/auth/logout`; }, 500);
}
@ -42,7 +45,7 @@ async function loadAllConfigs() {
try {
const filenames = await api.get('/config/filenames');
renderConfigList(filenames);
} catch (error) { if (error.message) notification.toast(`加载配置列表失败: ${error.message}`, 'error'); }
} catch (error) { if (error.message) notification.toast(t('toasts.load_configs_error', { error: error.message }), 'error'); }
}
async function handleEditConfig(originalFilename) {
@ -53,22 +56,22 @@ async function handleEditConfig(originalFilename) {
]);
state.isEditing = true;
switchView(DOMElements.configFormPanel);
DOMElements.formTitle.textContent = '编辑配置';
DOMElements.formTitle.textContent = t('pages.configs.form_title_edit');
fillForm(config, originalFilename);
showRenderedConfig(rendered, originalFilename);
const mode = config.upstream_config?.enable_upstream ? 'reverse_proxy' : (config.file_server_config?.enable_file_server ? 'file_server' : 'none');
updateServiceModeView(mode);
state.initialFormState = getFormStateAsString();
} catch (error) { notification.toast(`加载配置详情失败: ${error.message}`, 'error'); }
state.initialFormState = await getFormStateAsString();
} catch (error) { notification.toast(t('toasts.load_config_detail_error', { error: error.message }), 'error'); }
}
async function handleDeleteConfig(filename) {
if (!await notification.confirm(`确定要删除配置 "${filename}" 吗?`)) return;
if (!await notification.confirm(t('dialogs.delete_config_msg', { filename }), t('dialogs.delete_config_title'))) return;
try {
await api.delete(`/config/file/${filename}`);
notification.toast('配置已成功删除。', 'success');
notification.toast(t('toasts.delete_success'), 'success');
loadAllConfigs();
} catch (error) { notification.toast(`删除失败: ${error.message}`, 'error'); }
} catch (error) { notification.toast(t('toasts.delete_error', { error: error.message }), 'error'); }
}
async function handleSaveConfig(e) {
@ -76,7 +79,7 @@ async function handleSaveConfig(e) {
const formData = new FormData(DOMElements.configForm);
const domain = formData.get('domain');
if (!domain) {
notification.toast('域名不能为空。', 'error');
notification.toast(t('toasts.error_domain_empty'), 'error');
return;
}
const getHeadersMap = (keyName, valueName) => {
@ -116,23 +119,23 @@ async function handleSaveConfig(e) {
try {
const result = await api.put(`/config/file/${domain}`, configData);
state.isEditing = false;
notification.toast(result.message || '配置已成功保存。', 'success');
notification.toast(result.message || t('toasts.save_success'), 'success');
setTimeout(() => {
switchView(DOMElements.configListPanel);
loadAllConfigs();
}, 500);
} catch (error) { notification.toast(`保存失败: ${error.message}`, 'error'); }
} catch (error) { notification.toast(t('toasts.save_error', { error: error.message }), 'error'); }
}
async function openPresetModal(targetType) {
const applicablePresets = state.headerPresets.filter(p => p.target === targetType || p.target === 'any');
if (applicablePresets.length === 0) {
notification.toast(`没有适用于此区域的预设。`, 'info');
notification.toast(t('toasts.no_presets_available'), 'info');
return;
}
const presetId = await createPresetSelectionModal(applicablePresets);
if (!presetId) return; // 用户取消了选择
const presetId = await createPresetSelectionModal(applicablePresets, t);
if (!presetId) return;
let targetContainer, keyName, valueName;
if (targetType === 'global') {
@ -147,18 +150,17 @@ async function openPresetModal(targetType) {
return;
}
// 在获取到presetId后再执行填充逻辑
try {
notification.toast('正在加载预设...', 'info', 1000);
notification.toast(t('toasts.loading_preset'), 'info', 1000);
const preset = await api.get(`/config/headers-presets/${presetId}`);
if (!preset.headers) {
notification.toast('此预设无数据。', 'info');
notification.toast(t('toasts.preset_no_data'), 'info');
return;
}
const choice = await notification.confirm('如何填充预设?', '选择填充方式', { confirmText: '追加', cancelText: '替换' });
const choice = await notification.confirm(t('dialogs.preset_fill_msg'), t('dialogs.preset_fill_title'), { confirmText: t('common.append'), cancelText: t('common.replace') });
if (choice === false) { // 用户选择了“替换”
if (choice === false) {
targetContainer.innerHTML = '';
}
@ -167,19 +169,16 @@ async function openPresetModal(targetType) {
addKeyValueInput(targetContainer, keyName, valueName, key, value);
});
});
notification.toast(`已成功填充预设 "${preset.name}"`, 'success');
notification.toast(t('toasts.preset_fill_success', { presetName: preset.name }), 'success');
} catch (error) {
notification.toast(`加载预设失败: ${error.message}`, 'error');
notification.toast(t('toasts.load_preset_error', { error: error.message }), 'error');
}
}
function init() {
theme.init(DOMElements.themeToggleInput);
notification.init(DOMElements.toastContainer, DOMElements.dialogContainer, DOMElements.modalContainer);
activateNav('configs');
initCaddyStatus();
// --- 初始化与事件绑定 ---
function pageInit() {
// 这个函数包含所有特定于 app.js 的初始化逻辑
loadAllConfigs();
api.get('/config/headers-presets')
@ -187,16 +186,13 @@ function init() {
state.headerPresets = presets || [];
})
.catch(err => {
if (err.message) notification.toast(`加载Header预设失败: ${err.message}`, 'error');
if (err.message) notification.toast(t('toasts.load_presets_error', { error: err.message }), 'error');
});
DOMElements.menuToggleBtn.addEventListener('click', () => DOMElements.sidebar.classList.toggle('is-open'));
DOMElements.mainContent.addEventListener('click', () => DOMElements.sidebar.classList.remove('is-open'));
DOMElements.addNewConfigBtn.addEventListener('click', () => {
DOMElements.addNewConfigBtn.addEventListener('click', async () => {
state.isEditing = false;
switchView(DOMElements.configFormPanel);
DOMElements.formTitle.textContent = '创建新配置';
DOMElements.formTitle.textContent = t('pages.configs.form_title_create');
DOMElements.configForm.reset();
const noneButton = DOMElements.serviceModeControl.querySelector('[data-mode="none"]');
@ -204,7 +200,7 @@ function init() {
updateServiceModeView('none');
updateMultiUpstreamView(false);
state.initialFormState = getFormStateAsString();
state.initialFormState = await getFormStateAsString();
DOMElements.headersContainer.innerHTML = '';
DOMElements.upstreamHeadersContainer.innerHTML = '';
DOMElements.multiUpstreamContainer.innerHTML = '';
@ -247,7 +243,7 @@ function init() {
DOMElements.addMultiUpstreamBtn.addEventListener('click', (e) => {
e.preventDefault();
addSingleInput(DOMElements.multiUpstreamContainer, 'upstream_servers', '例如: 127.0.0.1:8081');
addSingleInput(DOMElements.multiUpstreamContainer, 'upstream_servers', t('form.upstream_server_placeholder'));
});
DOMElements.serviceModeControl.addEventListener('click', (e) => {
@ -265,4 +261,5 @@ function init() {
});
}
init();
// 使用通用初始化函数启动页面
initializePage({ pageId: 'configs', pageInit: pageInit });