@capacitor-trancee/app-language
Version:
Per-app language preferences
60 lines • 2.24 kB
JavaScript
import { WebPlugin } from '@capacitor/core';
import { I18n } from 'i18n-js';
export class AppLanguageWeb extends 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 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) {
options; // TS6133: 'options' is declared but its value is never read.
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.';
//# sourceMappingURL=web.js.map