Skip to content

Commit 3c0872b

Browse files
feat(i18n): i18nRedirector
1 parent efb9856 commit 3c0872b

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

.vitepress/pages/Playground.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
<script setup lang="ts">
6969
import { AISCRIPT_VERSION, Parser, Interpreter, utils, errors, type Ast } from '@syuilo/aiscript';
70+
import { inBrowser } from 'vitepress';
7071
import { ref, computed, useTemplateRef, nextTick, onMounted, watch, onUnmounted } from 'vue';
7172
import { createHighlighterCore } from 'shiki/core';
7273
import type { HighlighterCore, LanguageRegistration } from 'shiki/core';
@@ -271,7 +272,7 @@ function clearLog() {
271272
type HashData = {
272273
code: string;
273274
};
274-
const hash = ref<string | null>(import.meta.env.SSR ? null : window.location.hash.slice(1) || localStorage.getItem('ais:playground'));
275+
const hash = ref<string | null>(inBrowser ? null : window.location.hash.slice(1) || localStorage.getItem('ais:playground'));
275276
const hashData = computed<HashData | null>(() => {
276277
if (hash.value == null) return null;
277278
try {

.vitepress/theme/Layout.vue

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<template>
2+
<DefaultTheme.Layout />
3+
<div v-if="!loaded" :class="$style.w"></div>
4+
</template>
5+
6+
<script setup lang="ts">
7+
import { watch, ref } from 'vue';
8+
import { useData, useRoute, inBrowser } from 'vitepress';
9+
import DefaultTheme from 'vitepress/theme';
10+
11+
const data = useData();
12+
const route = useRoute();
13+
const loaded = ref(false);
14+
15+
const locales = Object.keys(data.site.value.locales) as string[];
16+
const localesRegex = new RegExp(`^/(${locales.join('|')})`);
17+
const savedLocale = localStorage.getItem('ais:locale');
18+
19+
if (inBrowser && !localesRegex.test(route.path)) {
20+
if (savedLocale != null && locales.includes(savedLocale)) {
21+
location.replace('/' + savedLocale + location.pathname + location.search);
22+
} else if (locales.includes(navigator.language.split('-')[0])) {
23+
location.replace('/' + navigator.language.split('-')[0] + location.pathname + location.search);
24+
} else {
25+
location.replace('/ja' + location.pathname + location.search);
26+
}
27+
}
28+
29+
if (inBrowser) {
30+
loaded.value = true;
31+
}
32+
33+
watch(data.lang, (lang) => {
34+
if (inBrowser) {
35+
localStorage.setItem('ais:locale', lang.split('-')[0]);
36+
}
37+
}, { immediate: true });
38+
</script>
39+
40+
<style module>
41+
.w {
42+
position: fixed;
43+
top: 0;
44+
left: 0;
45+
width: 100vw;
46+
height: 100vh;
47+
background-color: var(--vp-c-bg);
48+
z-index: 9999;
49+
}
50+
</style>

.vitepress/theme/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ import { h } from 'vue'
33
import type { Theme } from 'vitepress'
44
import DefaultTheme from 'vitepress/theme'
55
import './style.css'
6+
import Layout from './Layout.vue'
67
import CodeBlockPlayground from '../components/CodeBlockPlayground.vue'
78
import Playground from '../pages/Playground.vue'
89

910
export default {
1011
extends: DefaultTheme,
11-
Layout: () => {
12-
return h(DefaultTheme.Layout, null, {
13-
// https://vitepress.dev/guide/extending-default-theme#layout-slots
14-
})
15-
},
12+
Layout,
1613
enhanceApp({ app, router, siteData }) {
1714
app.component('CodeBlockPlayground', CodeBlockPlayground);
18-
1915
app.component('playground', Playground);
20-
}
16+
},
2117
} satisfies Theme

0 commit comments

Comments
 (0)