Add i18n support for manifest.json

This commit is contained in:
2024-05-25 18:19:11 +08:00
parent 83bc454409
commit 931824d3a9
2 changed files with 24 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import {
import { exists } from "std/fs/exists.ts";
import { get_task_manager } from "../server.ts";
import { build_sw } from "../server/build_sw.ts";
import { i18n_get_lang } from "../server/i18ns.ts";
const STATIC_FILES = ["/common.css", "/scrollBar.css", "/sw.js", "/sw.js.map"];
@@ -49,6 +50,15 @@ export async function handler(req: Request, ctx: FreshContext) {
if (!(await exists(p)) || p === flutter_base) {
p = join(flutter_base, "/index.html");
}
if (u.pathname == "/flutter/manifest.json") {
const lang = i18n_get_lang(req, ["zh-cn"]);
if (lang !== "en") {
const tp = join(flutter_base, `/manifest.${lang}.json`);
if (await exists(tp)) {
p = tp;
}
}
}
const opts: GetFileResponseOptions = {};
opts.range = req.headers.get("range");
opts.if_modified_since = req.headers.get("If-Modified-Since");

View File

@@ -54,6 +54,20 @@ export function get_i18nmap(lang: string, ...modules: MODULE[]) {
return r;
}
export function i18n_get_lang(req: Request, langs: string[]) {
const u = new URL(req.url);
const lang = u.searchParams.get("lang");
if (lang && (lang === "en" || langs.includes(lang))) {
return lang;
} else {
const a = req.headers.get("Accept-Language");
const l =
(a ? pick(langs, a) || pick(langs, a, { loose: true }) : null) ||
"en";
return l;
}
}
export function i18n_handle_request(req: Request) {
const u = new URL(req.url);
const lang = u.searchParams.get("lang");