browser-extension-i18n
v0.1.3
Published
I18N module for developing browser extensions and userscripts
Downloads
78
Maintainers
Readme
browser-extension-i18n
I18N module for developing browser extensions and userscripts
Installation
npm i browser-extension-i18n
# or
pnpm add browser-extension-i18n
# or
yarn add browser-extension-i18nUsage
Basic Setup
import { initAvailableLocales, initI18n } from "browser-extension-i18n"
// Define your message maps
const messageMaps = {
en: {
hello: "Hello",
welcome: "Welcome, {1}!",
},
zh: {
hello: "你好",
welcome: "欢迎,{1}!",
},
"zh-tw": {
hello: "您好",
},
}
// Initialize available locales
initAvailableLocales(["en", "zh", "zh-tw"])
// Create translation function
const t = initI18n(messageMaps)
// Use translations
console.log(t("hello")) // Auto-detects browser language
console.log(t("welcome", "John")) // With parametersAPI Reference
initAvailableLocales(locales: string[])
Initialize the list of supported locales.
initI18n(messageMaps: MessageMaps, language?: string): I18nFunction
Create a translation function. Auto-detects browser language if not specified.
getAvailableLocales(): readonly string[]
Get the list of currently available locales.
getBestMatchingLocale(preferredLanguage: string): string | undefined
Find the best matching locale for a given language preference.
isLocale(locale: string | undefined): boolean
Check if a string is a valid available locale.
extractLocaleFromNavigator(): string | undefined
Extract the best matching locale from browser language preferences.
License
Copyright (c) 2023 Pipecraft. Licensed under the MIT License.
