chore: checkpoint ai search comments and i18n foundation
This commit is contained in:
@@ -4,6 +4,7 @@ import Header from '../components/Header.astro';
|
||||
import Footer from '../components/Footer.astro';
|
||||
import BackToTop from '../components/interactive/BackToTop.svelte';
|
||||
import { api, DEFAULT_SITE_SETTINGS } from '../lib/api/client';
|
||||
import { getI18n, LOCALE_COOKIE_NAME, SUPPORTED_LOCALES } from '../lib/i18n';
|
||||
|
||||
interface Props {
|
||||
title?: string;
|
||||
@@ -11,6 +12,7 @@ interface Props {
|
||||
}
|
||||
|
||||
const props = Astro.props;
|
||||
const { locale, messages } = getI18n(Astro);
|
||||
|
||||
let siteSettings = DEFAULT_SITE_SETTINGS;
|
||||
|
||||
@@ -22,10 +24,11 @@ try {
|
||||
|
||||
const title = props.title || siteSettings.siteTitle;
|
||||
const description = props.description || siteSettings.siteDescription;
|
||||
const i18nPayload = JSON.stringify({ locale, messages });
|
||||
---
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<html lang={locale} data-locale={locale}>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
@@ -162,6 +165,32 @@ const description = props.description || siteSettings.siteDescription;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script is:inline define:vars={{ i18nPayload, locale, localeCookieName: LOCALE_COOKIE_NAME, supportedLocales: SUPPORTED_LOCALES }}>
|
||||
window.__TERMI_I18N__ = JSON.parse(i18nPayload);
|
||||
window.__termiTranslate = function(key, params = {}) {
|
||||
const payload = window.__TERMI_I18N__ || { messages: {} };
|
||||
const template = key.split('.').reduce((current, segment) => {
|
||||
if (!current || typeof current !== 'object') {
|
||||
return undefined;
|
||||
}
|
||||
return current[segment];
|
||||
}, payload.messages);
|
||||
|
||||
if (typeof template !== 'string') {
|
||||
return key;
|
||||
}
|
||||
|
||||
return template.replace(/\{(\w+)\}/g, (_, name) => String(params[name] ?? ''));
|
||||
};
|
||||
|
||||
(function() {
|
||||
document.documentElement.lang = locale;
|
||||
document.documentElement.dataset.locale = locale;
|
||||
localStorage.setItem('locale', locale);
|
||||
document.cookie = `${localeCookieName}=${encodeURIComponent(locale)};path=/;max-age=31536000;samesite=lax`;
|
||||
})();
|
||||
</script>
|
||||
|
||||
<script is:inline>
|
||||
(function() {
|
||||
const theme = localStorage.getItem('theme');
|
||||
|
||||
Reference in New Issue
Block a user