feat: |Worker| support multi language (#584)

This commit is contained in:
Dream Hunter
2025-02-20 00:37:39 +08:00
committed by GitHub
parent 2426e0b51a
commit 7889d2edea
23 changed files with 911 additions and 850 deletions

View File

@@ -1,5 +1,6 @@
import { Hono } from 'hono'
import i18n from '../i18n';
import { HonoCustomType } from "../types";
import { getBooleanValue, getJsonSetting, checkCfTurnstile, getStringValue } from '../utils';
import { newAddress, handleListQuery, deleteAddressWithData, getAddressPrefix, getAllowDomains } from '../common'
@@ -42,8 +43,10 @@ api.get('/api/mail/:mail_id', async (c) => {
})
api.delete('/api/mails/:id', async (c) => {
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
if (!getBooleanValue(c.env.ENABLE_USER_DELETE_EMAIL)) {
return c.text("User delete email is disabled", 403)
return c.text(msgs.UserDeleteEmailDisabledMsg, 403)
}
const { address } = c.get("jwtPayload")
const { id } = c.req.param();
@@ -59,16 +62,18 @@ api.delete('/api/mails/:id', async (c) => {
api.get('/api/settings', async (c) => {
const { address, address_id } = c.get("jwtPayload")
const user_role = c.get("userRolePayload")
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
if (address_id && address_id > 0) {
try {
const db_address_id = await c.env.DB.prepare(
`SELECT id FROM address where id = ? `
).bind(address_id).first("id");
if (!db_address_id) {
return c.text("Invalid address", 400)
return c.text(msgs.InvalidAddressMsg, 400)
}
} catch (error) {
return c.text("Invalid address", 400)
return c.text(msgs.InvalidAddressMsg, 400)
}
}
// check address id
@@ -78,11 +83,11 @@ api.get('/api/settings', async (c) => {
`SELECT id FROM address where name = ? `
).bind(address).first("id");
if (!db_address_id) {
return c.text("Invalid address", 400)
return c.text(msgs.InvalidAddressMsg, 400)
}
}
} catch (error) {
return c.text("Invalid address", 400)
return c.text(msgs.InvalidAddressMsg, 400)
}
// update address updated_at
try {
@@ -103,13 +108,15 @@ api.get('/api/settings', async (c) => {
})
api.post('/api/new_address', async (c) => {
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
if (getBooleanValue(c.env.DISABLE_ANONYMOUS_USER_CREATE_EMAIL)
&& !c.get("userPayload")
) {
return c.text("New address for anonymous user is disabled", 403)
return c.text(msgs.NewAddressAnonymousDisabledMsg, 403)
}
if (!getBooleanValue(c.env.ENABLE_USER_CREATE_EMAIL)) {
return c.text("New address is disabled", 403)
return c.text(msgs.NewAddressDisabledMsg, 403)
}
// eslint-disable-next-line prefer-const
let { name, domain, cf_token } = await c.req.json();
@@ -117,7 +124,7 @@ api.post('/api/new_address', async (c) => {
try {
await checkCfTurnstile(c, cf_token);
} catch (error) {
return c.text("Failed to check cf turnstile", 500)
return c.text(msgs.TurnstileCheckFailedMsg, 500)
}
// if no name, generate random name
if (!name) {
@@ -143,7 +150,7 @@ api.post('/api/new_address', async (c) => {
});
return c.json(res);
} catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400)
return c.text(`${msgs.FailedCreateAddressMsg}: ${(e as Error).message}`, 400)
}
})

View File

@@ -4,6 +4,7 @@ import { createMimeMessage } from 'mimetext';
import { Resend } from 'resend';
import { WorkerMailer, WorkerMailerOptions } from 'worker-mailer';
import i18n from '../i18n';
import { CONSTANTS } from '../constants'
import { getJsonSetting, getDomains, getIntValue, getBooleanValue, getStringValue, getJsonObjectValue } from '../utils';
import { GeoData } from '../models'
@@ -288,8 +289,10 @@ api.get('/api/sendbox', async (c) => {
})
api.delete('/api/sendbox/:id', async (c) => {
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
if (!getBooleanValue(c.env.ENABLE_USER_DELETE_EMAIL)) {
return c.text("User delete email is disabled", 403)
return c.text(msgs.UserDeleteEmailDisabledMsg, 403)
}
const { address } = c.get("jwtPayload")
const { id } = c.req.param();