UNPKG

@capacitor-trancee/app-language

Version:
96 lines (89 loc) 3.13 kB
'use strict'; var core = require('@capacitor/core'); var i18nJs = require('i18n-js'); exports.Status = void 0; (function (Status) { /** * Succeeded reading the `LocaleConfig` structure stored in an XML file. * * @since 1.0.0 */ Status[Status["SUCCESS"] = 0] = "SUCCESS"; /** * No `android:localeConfig` tag on pointing to an XML file that stores the `LocaleConfig`. * * @since 1.0.0 */ Status[Status["NOT_SPECIFIED"] = 1] = "NOT_SPECIFIED"; /** * Malformed input in the XML file where the `LocaleConfig` was stored. * * @since 1.0.0 */ Status[Status["PARSING_FAILED"] = 2] = "PARSING_FAILED"; })(exports.Status || (exports.Status = {})); const AppLanguage = core.registerPlugin('AppLanguage', { web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.AppLanguageWeb()), }); class AppLanguageWeb extends core.WebPlugin { get i18n() { return this._i18n; } set i18n(i18n) { this._i18n = i18n; } async initialize(options) { this.i18n = (options === null || options === void 0 ? void 0 : options.i18n) || new i18nJs.I18n(); this.registerOnLanguageChangedListener(); } async getApplicationLocales() { var _a; const result = {}; const locale = (_a = this.i18n) === null || _a === void 0 ? void 0 : _a.defaultLocale; if (locale) result.locales = [locale]; return result; } async setApplicationLocales(options) { var _a; if (this.i18n) { this.i18n.locale = ((_a = options === null || options === void 0 ? void 0 : options.locales) === null || _a === void 0 ? void 0 : _a.shift()) || ''; } } async resetApplicationLocales() { throw this.unimplemented(AppLanguageWeb.errorNotImplemented); } async getSystemLocales() { const result = {}; result.locales = navigator.languages.map((locale) => locale); return result; } async getOverrideLocaleConfig() { throw this.unavailable(AppLanguageWeb.errorNotAvailable); } async setOverrideLocaleConfig(options) { throw this.unavailable(AppLanguageWeb.errorNotAvailable); } async openSettings() { throw this.unimplemented(AppLanguageWeb.errorNotImplemented); } registerOnLanguageChangedListener() { addEventListener(AppLanguageWeb.languageChangeEvent, async () => { const result = await this.getSystemLocales(); const event = { locales: result.locales, }; this.notifyListeners(AppLanguageWeb.languageChangedEvent, event); }); } } AppLanguageWeb.languageChangeEvent = 'languagechange'; AppLanguageWeb.languageChangedEvent = 'languageChanged'; AppLanguageWeb.errorNotAvailable = 'Not available on Web.'; AppLanguageWeb.errorNotImplemented = 'Not implemented on Web.'; var web = /*#__PURE__*/Object.freeze({ __proto__: null, AppLanguageWeb: AppLanguageWeb }); exports.AppLanguage = AppLanguage; //# sourceMappingURL=plugin.cjs.js.map