astro.accords-library.com/middleware/lang.ts

28 lines
871 B
TypeScript
Raw Normal View History

2023-09-03 18:25:51 +00:00
import acceptLanguage from "accept-language";
import type { Handler } from "hono";
import { appConstants } from "../constants";
acceptLanguage.languages(appConstants.supportedLanguages);
export const langRewriteHandler: Handler = async (c, next) => {
const { origin, pathname } = new URL(c.req.url);
if (alreadyHasLanguageCode(pathname)) {
c.set("REWRITE", c.req.url);
} else {
const language = acceptLanguage.get(c.req.headers.get("accept-language"));
const newUrl = `${origin}/${language}${pathname}`;
c.set("REWRITE", newUrl);
}
await next();
};
const alreadyHasLanguageCode = (pathname: string): boolean => {
return appConstants.supportedLanguages.some((lang) => {
const prefix = `/${lang}`;
if (pathname.length === prefix.length) {
return pathname === prefix;
}
return pathname.startsWith(`${prefix}/`);
});
};