I18N module for developing browser extensions and userscripts
npm install browser-extension-i18nI18N module for developing browser extensions and userscripts
``bash`
npm i browser-extension-i18nor
pnpm add browser-extension-i18nor
yarn add browser-extension-i18n
`javascript
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 parameters
`
#### 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.
Copyright (c) 2023 Pipecraft. Licensed under the MIT License.

