Skip to main content
Go to Latest
class LocaleKit
import { LocaleKit } from "https://deno.land/x/localekit@0.4.0/mod.ts";

The main translation/language class. This handles storage of languages, translation/replacement of dynamic substrings, and the ability to add new languages.

Constructors

new
LocaleKit(unnamed 0?: { languages?: { [key: string]: Record<string, unknown>; }; fallback_language?: string; fallback_key?: string; })

Construct a new LangaugeService class with the given languages and fallbacks

Properties

fallback_key: string

A key to fall back to if the key is not found in the given language. If the fallback key is not found, the fallback language will be checked. If the fallback key isn't found in the fallback language either, the value returned will be "NOT_FOUND"

fallback_language: string

The language code to fall back to if the language is not supported

hydrated_languages: string[]

A list of languages that have been hydrated/updated post class initialisation

languages: { [key: string]: { [key: string]: string; }; }

A map of language codes and their given translations

Methods

addLanguage(code: string, lang: Record<string, unknown>)

Add a new language into the languages object

allSupported(): string[]

Returns an array of all supported languages

getKey(lang_code: string, key: string): string

Returns the value for a specific language and key

getTranslationFunc(lang_code: string)

Returns a new function for translating to a specific language replacing the t method

hydrateLanguage(code: string, lang: Record<string, unknown>)

Merge a new set of translations and keys into the language object

isHydrated(code: string)

Checks to see if a language is hydrated/updated

isSupported(code: string): boolean

Checks if a language is supported

setHydrated(code: string)

Sets a language as hydrated

t(key: string, opts?: Record<string, unknown>)

Translates a key to a specific language, replacing substrings with necessary values as needed