UNPKG

ui5_easy_use

Version:

CLI tool for SAP ui5 and SAPUI5 projects to initialize apps, generate pages, insert form and table components, manage routing, and automate i18n bindings

184 lines (147 loc) 5.69 kB
sap.ui.define([ "sap/ui/model/resource/ResourceModel" ], function (ResourceModel) { "use strict"; return class Language { constructor(component) { this._component = component; this.storageKey = "selectedLanguage"; this.defaultLanguage = "en"; this.supportedLocales = ["en", "ar"]; } async init() { const language = this._getStoredLanguage() || this.defaultLanguage; this._applyCoreLanguage(language); this._component.setModel(this._createI18nModel(), "i18n"); sap.ui.getCore().applyChanges(); } setSelectedLanguage() { this.sNewLanguage = this._getCurrentLanguage() === "ar" ? "en" : "ar"; } getSelectedLanguage() { return this.sNewLanguage; } onChangeLanguage() { this.setSelectedLanguage(); const nextLanguage = this.getSelectedLanguage(); this.setLanguageToModelAndResuource(nextLanguage); return nextLanguage; } // Kept for existing generated callers. Use this method to apply a user-selected language. setLanguageToModelAndResuource(language) { this.setLanguageToModelAndResource(language); } setLanguageToModelAndResource(language) { const nextLanguage = this._normalizeLanguage(language); this._storeLanguage(nextLanguage); this._applyCoreLanguage(nextLanguage); this._refreshI18nModel(); this._publishLocaleChanged(nextLanguage); sap.ui.getCore().applyChanges(); } replaceModelsJson(modelsJson) { const i18nModel = this._component.getModel("i18n"); (modelsJson || []).forEach(function (modelConfig) { const model = this._component.getModel(modelConfig.modelName); if (model) { this.replaceI18nKeys(model, i18nModel); } }, this); } replaceI18nKeys(model, i18nModel) { if (!model || !i18nModel) { return; } const data = model.getData(); this._replaceI18nValues(data, i18nModel.getResourceBundle()); model.refresh(); } waitForI18nModelToLoad() { return new Promise((resolve, reject) => { const i18nModel = this._component.getModel("i18n"); if (!i18nModel) { reject("i18n model is not available"); return; } if (i18nModel.getResourceBundle()) { resolve(); return; } i18nModel.attachRequestCompleted(resolve); i18nModel.attachRequestFailed(reject); }); } _createI18nModel() { return new ResourceModel({ bundleName: "${ez5.appName}.i18n.i18n", supportedLocales: this.supportedLocales, fallbackLocale: this.defaultLanguage }); } _getCurrentLanguage() { const i18nModel = this._component.getModel("i18n"); return i18nModel?.getResourceBundle?.()?.sLocale || this.defaultLanguage; } _normalizeLanguage(language) { return this.supportedLocales.includes(language) ? language : this.defaultLanguage; } _applyCoreLanguage(language) { sap.ui.getCore().getConfiguration().setLanguage(language); sap.ui.getCore().getConfiguration().setFormatLocale(language); } _refreshI18nModel() { const i18nModel = this._component.getModel("i18n"); if (i18nModel) { i18nModel.refresh(true); } } _publishLocaleChanged(language) { sap.ui.getCore().getEventBus().publish("sap.ui", "localeChanged", { newLanguage: language }); } _replaceI18nValues(value, resourceBundle) { if (Array.isArray(value)) { value.forEach(function (item) { this._replaceI18nValues(item, resourceBundle); }, this); return; } if (!value || typeof value !== "object") { return; } Object.keys(value).forEach(function (key) { const currentValue = value[key]; if (this._isI18nKey(currentValue)) { value[key] = this._getBundleText(resourceBundle, currentValue); return; } this._replaceI18nValues(currentValue, resourceBundle); }, this); } _isI18nKey(value) { return typeof value === "string" && value.startsWith("i18n_"); } _getBundleText(resourceBundle, key) { if (!resourceBundle) { return key; } if (typeof resourceBundle.hasText === "function" && !resourceBundle.hasText(key)) { return key; } return resourceBundle.getText(key); } _getStoredLanguage() { return this._getStorage()?.getItem(this.storageKey); } _storeLanguage(language) { this._getStorage()?.setItem(this.storageKey, language); } _getStorage() { if (typeof window === "undefined" || !window.localStorage) { return null; } return window.localStorage; } }; });