UNPKG

@revgaming/global

Version:

application bundle to globalize with ioc-like loader and localizations

57 lines (50 loc) 1.22 kB
import { locale, setLocale, translate, languages, langName, mergeTranslations, getCollection, detectLang, setFallBack, hasTranslation, } from '@revgaming/languages' const globalizeLang = key => { if (key === false) return if (typeof key !== 'string') throw 'global function name must be string' else if (window[key] !== undefined) throw `global ${key} for translate is already exists` else window[key] = translate } export const bootLanguages = opts => { if (opts.hasOwnProperty('locale')) { if (opts.locale === 'detect') { detectLang() } else if (typeof opts.locale === 'object') { detectLang(opts.locale) } else setLocale(opts.locale) } else detectLang() if (opts.hasOwnProperty('fallback')) { setFallBack(opts.fallback) } if (opts.hasOwnProperty('translations')) { mergeTranslations(opts.translations) } else { mergeTranslations(opts) } globalizeLang(opts.global ?? '__') return { locale: locale, setLocale: setLocale, translate: translate, languages: languages, langName: langName, langHas: hasTranslation, mergeTranslations: mergeTranslations, translations: getCollection, } }