Skip to content

Commit

Permalink
fix: caseы
Browse files Browse the repository at this point in the history
  • Loading branch information
Valexr committed Apr 19, 2024
1 parent 460bd56 commit b96c2b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
8 changes: 2 additions & 6 deletions src/lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { derived, readable, writable } from 'svelte/store';
import { cacheable } from './cacheable';
import jsonp from './jsonp';
import { random } from './utils';
import { random, local } from './utils';

export const start = cacheable('startDate', '', true)
export const date = readable(new Date().toLocaleDateString("ru"))
Expand Down Expand Up @@ -44,8 +43,7 @@ function createQuote() {
return {
subscribe, set, update,
async load() {
const local = navigator.language.includes('ru') ? 'ru' : 'en'
const url = `./assets/quotes_${local}.json`
const url = `./assets/quotes_${local()}.json`
const res = await fetch(url);
const json = await res.json()
const quote = json[random(json.length)];
Expand All @@ -54,5 +52,3 @@ function createQuote() {
}
}

export const local = writable(navigator.languages[1] === 'ru' ? 'ru' : 'en')

36 changes: 23 additions & 13 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@ export function random(length: number) {
return Math.floor(Math.random() * length)
}

export function t(en: string, ru: string, count?: number) {
console.log(navigator.language)
return navigator.language.includes('ru') ? ru : en
export function t(en: string, ru: string) {
return local() === 'ru' ? ru : en
}

const trans = {
years: 'лет',
months: 'месяцев',
days: 'дней',
hours: 'часов'
export function local() {
return navigator.language.includes('ru') ? 'ru' : 'en'
}

export function convert(en: keyof typeof trans, count = 0) {
const RU = navigator.language.includes('ru')
if (!count || count % 5) {
return RU ? trans[en] : en
type Trans = {
years: string;
months: string;
days: string;
hours: string;
}

export function convert(en: keyof Trans, count = 0) {
const RU = local() === 'ru'
const FIRSTABLE = count === 1 || count > 20 && String(count).endsWith('1')
const SECONDABLE = [2, 3, 4].includes(count)

const trans = {
years: FIRSTABLE ? 'год' : SECONDABLE ? 'года' : 'лет',
months: !count || count > 4 ? 'месяцев' : SECONDABLE ? 'месяца' : 'месяц',
days: FIRSTABLE ? 'день' : SECONDABLE ? 'дня' : 'дней',
hours: FIRSTABLE ? 'час' : SECONDABLE ? 'часа' : 'часов'
}
return RU ? trans[en] : en

return RU ? trans[en] : count ? en.substring(0, en.length - 1) : en
// 1 - year/год | month/месяц | day/день | hour/час
// 2-4 - years/года | months/месяца | days/дня | hours/часа
// 0, 5-20 - years/лет | months/месяцев | days/дней | hours/часов
Expand Down

0 comments on commit b96c2b9

Please sign in to comment.