-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: take locale on hook, use custom dictionary
- Loading branch information
1 parent
9176c53
commit ca8dd63
Showing
64 changed files
with
134 additions
and
208 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
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,19 +1,24 @@ | ||
<script lang='ts'> | ||
import { locale, locales, t } from '$lib/translations' | ||
import { page } from '$app/stores' | ||
import { supportedLocales } from '$lib/translations' | ||
type SelectEvent = Event & { currentTarget: EventTarget & HTMLInputElement } | ||
const locale = $page.data.locale | ||
let selected = locale | ||
const handleChange = ({ currentTarget }: SelectEvent) => { | ||
const handleChange = ({ currentTarget }: { currentTarget: HTMLSelectElement }) => { | ||
const { value } = currentTarget | ||
document.cookie = `lang=${value} ;` | ||
window.location.href = `/${value}` | ||
} | ||
</script> | ||
|
||
<div> | ||
<select bind:value={$locale} on:change={() => handleChange}> | ||
{#each $locales as value} | ||
<option value={value} selected={$locale === value}>{$t(`lang.${value}`)}</option> | ||
{/each} | ||
</select> | ||
</div> | ||
<select bind:value={selected} onchange={handleChange}> | ||
{#each supportedLocales as value} | ||
<option value={value} selected={selected === value}>{value}</option> | ||
{/each} | ||
</select> | ||
|
||
<style> | ||
select { | ||
margin: 0 2em; | ||
} | ||
</style> |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export type Dictionary = typeof en | ||
|
||
export const en = { | ||
about: undefined, | ||
character: undefined, | ||
error: { | ||
404: 'Page not found.', | ||
500: 'Server internal error.', | ||
title: 'Oh, dear...', | ||
default: 'Here we go... Or is that a feature?', | ||
}, | ||
header: { | ||
menu: { | ||
home: 'Home', | ||
about: 'About', | ||
characters: 'Characters', | ||
}, | ||
}, | ||
home: undefined, | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,88 +1,17 @@ | ||
import I18n from 'sveltekit-i18n' | ||
import type { Config } from 'sveltekit-i18n' | ||
import lang from './lang.json' | ||
import { dev } from '$app/environment' | ||
import { ru } from '$lib/translations/ru' | ||
import { type Dictionary, en } from '$lib/translations/en' | ||
|
||
export type Locale = 'en' | 'ru' | ||
|
||
export const defaultLocale = 'en' | ||
|
||
export const config: Config = { | ||
log: { | ||
level: dev ? 'warn' : 'error', | ||
}, | ||
translations: { | ||
en: { lang }, | ||
ru: { lang }, | ||
}, | ||
loaders: [ | ||
{ | ||
locale: 'en', | ||
key: 'header', | ||
loader: async () => (await import('./en/header.json')).default, | ||
}, | ||
{ | ||
locale: 'en', | ||
key: 'error', | ||
loader: async () => (await import('./en/error.json')).default, | ||
}, | ||
{ | ||
locale: 'en', | ||
key: 'home', | ||
routes: ['/'], | ||
loader: async () => (await import('./en/home.json')).default, | ||
}, | ||
{ | ||
locale: 'en', | ||
key: 'about', | ||
routes: ['/about'], | ||
loader: async () => (await import('./en/about.json')).default, | ||
}, | ||
{ | ||
locale: 'en', | ||
key: 'character', | ||
routes: ['/character'], | ||
loader: async () => (await import('./en/character.json')).default, | ||
}, | ||
{ | ||
locale: 'ru', | ||
key: 'header', | ||
loader: async () => (await import('./ru/header.json')).default, | ||
}, | ||
{ | ||
locale: 'ru', | ||
key: 'error', | ||
loader: async () => (await import('./ru/error.json')).default, | ||
}, | ||
{ | ||
locale: 'ru', | ||
key: 'home', | ||
routes: ['/'], | ||
loader: async () => (await import('./ru/home.json')).default, | ||
}, | ||
{ | ||
locale: 'ru', | ||
key: 'about', | ||
routes: ['/about'], | ||
loader: async () => (await import('./ru/about.json')).default, | ||
}, | ||
{ | ||
locale: 'ru', | ||
key: 'character', | ||
routes: ['/character'], | ||
loader: async () => (await import('./ru/character.json')).default, | ||
}, | ||
], | ||
} | ||
export const supportedLocales = ['en', 'ru'] as Locale[] | ||
|
||
export const { | ||
t, | ||
locale, | ||
locales, | ||
loading, | ||
addTranslations, | ||
loadTranslations, | ||
translations, | ||
setRoute, | ||
setLocale, | ||
} = new I18n(config) | ||
const translations = { | ||
en, | ||
ru, | ||
} | ||
|
||
loading.subscribe(($loading) => $loading && console.log('Loading translations...')) | ||
export function dictionary(locale: Locale): Dictionary { | ||
return translations[locale] | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Dictionary } from '$lib/translations/en' | ||
|
||
export const ru = { | ||
about: undefined, | ||
character: undefined, | ||
error: { | ||
404: 'Страница не найдена.', | ||
500: 'Произошла ошибка на сервере.', | ||
title: 'О, Боже...', | ||
default: 'Ну вот... Или это фича?', | ||
}, | ||
header: { | ||
menu: { | ||
home: 'Главная', | ||
about: 'Об игре', | ||
characters: 'Персонажи', | ||
}, | ||
}, | ||
home: undefined, | ||
} satisfies Dictionary |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.