setup i18n

This commit is contained in:
Chenyu Huang
2025-07-31 16:10:32 -07:00
parent 3db994ac52
commit 96b5c6bf8d
8 changed files with 70 additions and 12 deletions
+21 -9
View File
@@ -1,19 +1,29 @@
import escapeHTML from 'escape-html'
import {type Request} from 'express'
import {type Hole, html} from 'uhtml'
export function linkWarningContents(opts: {
type: 'warn' | 'block'
link: string
}): Hole {
export function linkWarningContents(
req: Request,
opts: {
type: 'warn' | 'block'
link: string
},
): Hole {
return html`
<div class="warning-icon">⚠️</div>
<h1>
${opts.type === 'warn' ? 'Potentially Dangerous Link' : 'Blocked Link'}
${opts.type === 'warn'
? req.__('Potentially Dangerous Link')
: req.__('Blocked Link')}
</h1>
<p class="warning-text">
${opts.type === 'warn'
? 'This link may be malicious. You should proceed at your own risk.'
: 'This link has been identified as malicious and has blocked for your safety.'}
? req.__(
'This link may be malicious. You should proceed at your own risk.',
)
: req.__(
'This link has been identified as malicious and has blocked for your safety.',
)}
</p>
<div class="blocked-site">
<p class="site-url">${escapeHTML(opts.link)}</p>
@@ -21,10 +31,12 @@ export function linkWarningContents(opts: {
<div class="button-group">
${opts.type === 'warn'
? html`<a class="button secondary" href="${escapeHTML(opts.link)}"
>Continue Anyway</a
>${req.__('Continue Anyway')}</a
>`
: null}
<a class="button primary" href="https://bsky.app">Return to Bluesky</a>
<a class="button primary" href="https://bsky.app"
>${req.__('Return to Bluesky')}</a
>
</div>
`
}
+15
View File
@@ -0,0 +1,15 @@
import path from 'node:path'
import {fileURLToPath} from 'node:url'
import i18n from 'i18n'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
i18n.configure({
locales: ['en', 'es', 'fr'],
defaultLocale: 'en',
directory: path.join(__dirname, '../locales'),
})
export default i18n
+6 -1
View File
@@ -7,6 +7,7 @@ import {createHttpTerminator, type HttpTerminator} from 'http-terminator'
import {type Config} from './config.js'
import {AppContext} from './context.js'
import i18n from './i18n.js'
import {default as routes, errorHandler} from './routes/index.js'
export * from './config.js'
@@ -17,11 +18,15 @@ export class LinkService {
public server?: http.Server
private terminator?: HttpTerminator
constructor(public app: express.Application, public ctx: AppContext) {}
constructor(
public app: express.Application,
public ctx: AppContext,
) {}
static async create(cfg: Config): Promise<LinkService> {
let app = express()
app.use(cors())
app.use(i18n.init)
const ctx = await AppContext.fromConfig(cfg)
app = routes(ctx, app)
+2 -2
View File
@@ -68,7 +68,7 @@ export default function (ctx: AppContext, app: Express) {
case ToolsOzoneSafelinkDefs.BLOCK:
hole = linkWarningLayout(
'Blocked Link Warning',
linkWarningContents({
linkWarningContents(req, {
type: 'block',
link: url.href,
}),
@@ -79,7 +79,7 @@ export default function (ctx: AppContext, app: Express) {
case ToolsOzoneSafelinkDefs.WARN:
hole = linkWarningLayout(
'Malicious Link Warning',
linkWarningContents({
linkWarningContents(req, {
type: 'warn',
link: url.href,
}),