2023-02-17 09:21:02 +00:00
|
|
|
|
import type { i18n } from 'i18next'
|
|
|
|
|
import type { Accessor, JSX } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
2023-05-01 18:32:32 +00:00
|
|
|
|
import i18next, { changeLanguage, t } from 'i18next'
|
2023-10-18 10:56:41 +00:00
|
|
|
|
import TimeAgo from 'javascript-time-ago'
|
|
|
|
|
import en from 'javascript-time-ago/locale/en'
|
|
|
|
|
import ru from 'javascript-time-ago/locale/ru'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
import Cookie from 'js-cookie'
|
2024-02-04 11:25:21 +00:00
|
|
|
|
import { Show, createContext, createEffect, createMemo, createSignal, useContext } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
|
|
import { useRouter } from '../stores/router'
|
2023-10-18 10:56:41 +00:00
|
|
|
|
|
|
|
|
|
TimeAgo.addLocale(en)
|
|
|
|
|
TimeAgo.addLocale(ru)
|
2023-02-17 09:21:02 +00:00
|
|
|
|
|
|
|
|
|
type LocalizeContextType = {
|
|
|
|
|
t: i18n['t']
|
|
|
|
|
lang: Accessor<Language>
|
|
|
|
|
setLang: (lang: Language) => void
|
2023-10-18 10:56:41 +00:00
|
|
|
|
formatTime: (date: Date, options?: Intl.DateTimeFormatOptions) => string
|
|
|
|
|
formatDate: (date: Date, options?: Intl.DateTimeFormatOptions) => string
|
|
|
|
|
formatTimeAgo: (date: Date) => string
|
2023-02-17 09:21:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Language = 'ru' | 'en'
|
|
|
|
|
|
|
|
|
|
const LocalizeContext = createContext<LocalizeContextType>()
|
|
|
|
|
|
|
|
|
|
export function useLocalize() {
|
|
|
|
|
return useContext(LocalizeContext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const LocalizeProvider = (props: { children: JSX.Element }) => {
|
|
|
|
|
const [lang, setLang] = createSignal<Language>(i18next.language === 'en' ? 'en' : 'ru')
|
2023-12-20 08:07:57 +00:00
|
|
|
|
const { searchParams, changeSearchParams } = useRouter<{
|
2023-10-18 10:56:41 +00:00
|
|
|
|
lng: string
|
|
|
|
|
}>()
|
2023-02-17 09:21:02 +00:00
|
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
|
if (!searchParams().lng) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lng: Language = searchParams().lng === 'en' ? 'en' : 'ru'
|
|
|
|
|
|
2023-05-01 18:32:32 +00:00
|
|
|
|
changeLanguage(lng)
|
2023-02-17 09:21:02 +00:00
|
|
|
|
setLang(lng)
|
|
|
|
|
Cookie.set('lng', lng)
|
2023-12-20 08:07:57 +00:00
|
|
|
|
changeSearchParams({ lng: null }, true)
|
2023-02-17 09:21:02 +00:00
|
|
|
|
})
|
|
|
|
|
|
2023-10-18 10:56:41 +00:00
|
|
|
|
const formatTime = (date: Date, options: Intl.DateTimeFormatOptions = {}) => {
|
|
|
|
|
const opts = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
hour: '2-digit',
|
2023-11-14 15:10:00 +00:00
|
|
|
|
minute: '2-digit',
|
2023-10-18 10:56:41 +00:00
|
|
|
|
},
|
2023-11-14 15:10:00 +00:00
|
|
|
|
options,
|
2023-10-18 10:56:41 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return date.toLocaleTimeString(lang(), opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const formatDate = (date: Date, options: Intl.DateTimeFormatOptions = {}) => {
|
|
|
|
|
const opts = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
month: 'long',
|
|
|
|
|
day: 'numeric',
|
2023-11-14 15:10:00 +00:00
|
|
|
|
year: 'numeric',
|
2023-10-18 10:56:41 +00:00
|
|
|
|
},
|
2023-11-14 15:10:00 +00:00
|
|
|
|
options,
|
2023-10-18 10:56:41 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
let result = date.toLocaleDateString(lang(), opts)
|
|
|
|
|
if (lang() === 'ru') {
|
2023-11-06 19:15:13 +00:00
|
|
|
|
result = result.replace(' г.', '').replace('г.', '')
|
2023-10-18 10:56:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const timeAgo = createMemo(() => new TimeAgo(lang()))
|
|
|
|
|
|
|
|
|
|
const formatTimeAgo = (date: Date) => timeAgo().format(date)
|
|
|
|
|
|
|
|
|
|
const value: LocalizeContextType = { t, lang, setLang, formatTime, formatDate, formatTimeAgo }
|
2023-02-17 09:21:02 +00:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LocalizeContext.Provider value={value}>
|
|
|
|
|
<Show when={lang()} keyed={true}>
|
|
|
|
|
{props.children}
|
|
|
|
|
</Show>
|
|
|
|
|
</LocalizeContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|