File tree Expand file tree Collapse file tree 1 file changed +22
-16
lines changed Expand file tree Collapse file tree 1 file changed +22
-16
lines changed Original file line number Diff line number Diff line change 1
1
<script context =" module" lang =" ts" >
2
2
import { registerAll , availableLocales } from ' $locales' ;
3
- import { t , waitLocale , init } from ' svelte-intl-precompile' ;
3
+ import { waitLocale , init , locale } from ' svelte-intl-precompile' ;
4
4
import { goto } from ' $app/navigation' ;
5
5
import { page } from ' $app/stores' ;
6
6
registerAll ();
7
7
let defaultLang = ' en' ;
8
- let localeRegex = new RegExp (` ^/(${availableLocales .join (' |' )})(/|$) ` )
8
+ let localeRegex = new RegExp (` ^/(${availableLocales .join (' |' )})(/|$) ` );
9
+ let initialized = false ;
10
+ function extractLanguageFromPath(path ) {
11
+ return (localeRegex .exec (path ) || [null , defaultLang ])[1 ];
12
+ }
9
13
export async function load({ url : { pathname } }) {
10
- const lang = (localeRegex .exec (pathname ) || [null , defaultLang ])[1 ];
11
- init ({
12
- initialLocale: lang ,
13
- fallbackLocale: defaultLang
14
- });
15
- await waitLocale ()
16
- return {
17
- props: {
18
- lang
19
- }
20
- };
14
+ if (! initialized ) {
15
+ const lang = extractLanguageFromPath (pathname );
16
+ init ({
17
+ initialLocale: lang ,
18
+ fallbackLocale: defaultLang
19
+ });
20
+ initialized = true ;
21
+ await waitLocale ()
22
+ }
23
+ return {};
21
24
}
22
25
</script >
23
26
<script lang =" ts" >
24
- export let lang: string
25
-
26
27
const flags = import .meta .globEager (' ../../lib/flags/*.svg' )
27
-
28
+ $ : lang = extractLanguageFromPath ($page .url .pathname );
29
+ $ : {
30
+ if ($locale !== lang ) {
31
+ $locale = lang ;
32
+ }
33
+ }
28
34
function flagFor(lang ) {
29
35
return flags [` ../../lib/flags/${lang }.svg ` ].default
30
36
}
You can’t perform that action at this time.
0 commit comments