-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add localstorag,fix language reset on refresh
- Loading branch information
1 parent
3bcd78b
commit 160e7b1
Showing
2 changed files
with
30 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
import { browser } from '$app/environment'; | ||
import { writable } from 'svelte/store'; | ||
|
||
export const language = writable<'en' | 'zh'>('en'); | ||
function getSavedLanguage() { | ||
if (browser) { | ||
const saved = localStorage.getItem('language'); | ||
if (saved === 'en' || saved === 'zh') { | ||
return saved; | ||
} | ||
} | ||
return 'en'; | ||
} | ||
|
||
export const language = writable<'en' | 'zh'>(getSavedLanguage()); | ||
|
||
if (browser) { | ||
language.subscribe((value) => { | ||
localStorage.setItem('language', value); | ||
}); | ||
} |