Skip to content

Preferred Languages

Reactive Navigator Languages.

๐ŸŽฌ Usage

<script>
    import { preferred_lang } from "@sveu/browser"

    const lang = preferred_lang()
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

โ†ฉ๏ธ Returns

A subscribable store with the value of the Navigator Languages.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import { to_readable, to_writable } from "@sveu/shared"

import { on } from "../event_listener"

/**
 * Reactive Navigator Languages.
 *
 * @returns Readable Store
 */
export function preferred_lang() {
    if (!window) return to_readable(["en"])

    const navigator = window.navigator

    const { subscribe, set } = to_writable<readonly string[]>(
        navigator.languages
    )

    on(window, "languagechange", () => {
        set(navigator.languages)
    })

    return { subscribe }
}

Last update: 2023-03-09