Skip to content

Commit

Permalink
fix: navigator.language Safari detect
Browse files Browse the repository at this point in the history
  • Loading branch information
Valexr committed Apr 19, 2024
1 parent 17f7f9f commit 0e231cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/lib/components/County.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" context="module">
import Button from "./Button.svelte";
import { t } from "$lib/utils";
import { convert, t } from "$lib/utils";
</script>

<script lang="ts">
Expand All @@ -16,35 +16,35 @@

<ul>
{#if type === "months"}
<li id={t("months", "месяцы")}>
<li id={convert("months", county.full.months)}>
<Button id="" bind:type>
{county.full.months}
</Button>
</li>
{:else if type === "days"}
<li id={t("days", "дни")}>
<li id={convert("days", county.full.days)}>
<Button id="" bind:type>
{county.full.days}
</Button>
</li>
{:else if type === "hours"}
<li id={t("hours", "часы")}>
<li id={convert("hours", county.full.hours)}>
<Button id="" bind:type>
{county.full.hours}
</Button>
</li>
{:else}
<li id={t("years", "годы")}>
<li id={convert("years", county.years)}>
<Button id="hours" bind:type>
{county.years}
</Button>
</li>
<li id={t("months", "месяцы")}>
<li id={convert("months", county.months)}>
<Button id="months" bind:type>
{county.months}
</Button>
</li>
<li id={t("days", "дни")}>
<li id={convert("days", county.days)}>
<Button id="days" bind:type>
{county.days}
</Button>
Expand Down
26 changes: 23 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ export function random(length: number) {
return Math.floor(Math.random() * length)
}

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

const trans = {
years: 'лет',
months: 'месяцев',
days: 'дней',
hours: 'часов'
}

export function convert(en: keyof typeof trans, count = 0) {
const RU = navigator.language.includes('ru')
if (!count || count % 5) {
return RU ? trans[en] : en
}
return RU ? trans[en] : en
// 1 - year/год | month/месяц | day/день | hour/час
// 2-4 - years/года | months/месяца | days/дня | hours/часа
// 0, 5-20 - years/лет | months/месяцев | days/дней | hours/часов
}

0 comments on commit 0e231cf

Please sign in to comment.