@@ -6,11 +6,89 @@ const localeRedirects = Object.entries(siteLocales).map(([key, locale]) => ({
66 lang : locale . lang
77} ) )
88
9+ function createLocaleRedirectScript ( ) {
10+ return `(() => {
11+ const redirects = ${ JSON . stringify ( localeRedirects ) } ;
12+ const pathname = window.location.pathname;
13+
14+ if (pathname !== '/' && pathname !== '/index.html') {
15+ return;
16+ }
17+
18+ const params = new URLSearchParams(window.location.search);
19+ const delayValue = Number(params.get('redirectDelay') ?? '0');
20+ const redirectDelay = Number.isFinite(delayValue) && delayValue >= 0
21+ ? delayValue
22+ : 0;
23+
24+ const preferred = [];
25+ const seen = new Set();
26+ const push = (language) => {
27+ if (typeof language !== 'string') {
28+ return;
29+ }
30+
31+ const normalized = language.trim().toLowerCase().replace(/_/g, '-');
32+
33+ if (!normalized || seen.has(normalized)) {
34+ return;
35+ }
36+
37+ seen.add(normalized);
38+ preferred.push(normalized);
39+ };
40+
41+ try {
42+ push(Intl.DateTimeFormat().resolvedOptions().locale);
43+ } catch {}
44+
45+ push(navigator.language);
46+
47+ for (const language of navigator.languages ?? []) {
48+ push(language);
49+ }
50+
51+ push(navigator.userLanguage);
52+ push(navigator.browserLanguage);
53+ push(navigator.systemLanguage);
54+
55+ const resolve = () => {
56+ const normalized = preferred.flatMap((language) => [language, language.split('-')[0]]);
57+
58+ for (const candidate of normalized) {
59+ const matched = redirects.find(({ lang }) => {
60+ const normalizedLang = lang.toLowerCase();
61+ return candidate === normalizedLang || candidate === normalizedLang.split('-')[0];
62+ });
63+
64+ if (matched) {
65+ return matched.path;
66+ }
67+ }
68+
69+ return redirects.find(({ lang }) => lang.toLowerCase().startsWith('en'))?.path
70+ ?? redirects[0]?.path
71+ ?? '/en/';
72+ };
73+
74+ const target = resolve();
75+
76+ if (target && target !== pathname) {
77+ window.setTimeout(() => {
78+ window.location.replace(target);
79+ }, redirectDelay);
80+ }
81+ })();`
82+ }
83+
984export default defineConfig ( {
1085 title : 'DataBackup' ,
1186 description : 'Free and open-source data backup application' ,
1287 base : '/' ,
13- head : [ [ 'link' , { rel : 'icon' , href : '/images/logo.png' } ] ] ,
88+ head : [
89+ [ 'link' , { rel : 'icon' , href : '/images/logo.png' } ] ,
90+ [ 'script' , { } , createLocaleRedirectScript ( ) ]
91+ ] ,
1492 themeConfig : {
1593 logo : '/images/logo.png' ,
1694 localeRedirects,
0 commit comments