Overview
You will learn
- How to add localization to your UI5 micro-frontend
Prerequisites
- You have completed Build Luigi Micro-Frontend with UI5 and have a running UI5 micro-frontend connected to the Luigi Core application.
Steps
Intro
In this tutorial, you will add German localization to the UI5 micro-frontend by reading the current language from the Luigi Client and providing translated content for the “Order History” view.
In this step, you will add a function to get the current language from Luigi Client and then update it, so that the language of the UI5 micro-frontend can be changed accordingly.
- Open the
ui5-mf/webapp/controller/Main.controller.jsfile and replace its content with the code below:
sap.ui.define([
"luigi/ui5/controller/BaseController",
"sap/ui/model/json/JSONModel",
"sap/base/i18n/Localization"
], function (Controller, JSONModel, Localization) {
"use strict";
return Controller.extend("luigi.ui5.controller.Main", {
onInit: function () {
const oModel = new JSONModel();
oModel.loadData(sap.ui.require.toUrl("luigi/ui5/model/products.json"));
this.getView().setModel(oModel);
const updateCurrentLanguage = () => {
const currentLanguage = LuigiClient.uxManager().getCurrentLocale();
Localization.setLanguage(currentLanguage || "en-US");
};
this._initListener = LuigiClient.addInitListener(updateCurrentLanguage);
this._contextUpdateListener = LuigiClient.addContextUpdateListener(updateCurrentLanguage);
},
onExit: function () {
LuigiClient.removeInitListener(this._initListener);
LuigiClient.removeContextUpdateListener(this._contextUpdateListener);
},
onListItemPress: async function (oEvent) {
const id = oEvent.getSource().getBindingContext().getProperty("id");
// Works with both synchronous and asynchronous ResourceModels.
const bundle = await this.getView().getModel("i18n").getResourceBundle();
const title = bundle.getText("ModalText");
LuigiClient.linkManager().openAsModal('/home/products/' + id, { title: title, size: 'm' });
}
});
});In this step, you will create files with the text that is to be changed within the UI5 micro-frontend.
Find the
i18n folder inside webapp. Open (or create) a file calledi18n_de.propertieswith the following content:JSONModalText = Produktdetails Quantity = Anzahl appTitle = ui5 appDescription = DeutschOpen (or create) a file called
i18n_en.propertieswith the following content:JSONModalText = Product Details appTitle = ui5 appDescription = English Quantity = QuantityReplace the content of the default
ui5-mf/webapp/i18n/i18n.propertieswith the English content from step 2 so that the new keys also exist in the fallback bundle.In
ui5-mf/webapp/manifest.json, set thesap.ui5.models.i18nmodel to the following. Preserve the other models and manifest settings:JSON"i18n": { "type": "sap.ui.model.resource.ResourceModel", "settings": { "bundleName": "luigi.ui5.i18n.i18n", "supportedLocales": ["", "en", "de"], "fallbackLocale": "en", "async": true } }This enables both language bundles, even if the generated manifest initially restricts
supportedLocalesto the default bundle.
Edit the
ui5-mf/webapp/index.htmlfile by adding the default language (EN):HTML<script id="sap-ui-bootstrap" src="resources/sap-ui-core.js" data-sap-ui-language='en-US' data-sap-ui-resource-roots='{ "luigi.ui5": "./" }' data-sap-ui-on-init="module:sap/ui/core/ComponentSupport" data-sap-ui-compat-version="edge" data-sap-ui-frame-options="allow" data-sap-ui-preload="" data-sap-ui-async="true" ></script>
This step involves the standard process in UI5 for providing translation.
Edit the
ui5-mf/webapp/view/Main.view.xmlfile by marking the translated target text. Replace the<ObjectAttribute>tag with:XML<ObjectAttribute text="{i18n>Quantity}: {orderQuantity}" />
Now, your app should be complete and you can run it locally to see if everything works. First, open a terminal/command prompt window and navigate to your project folder.
Navigate to the React app and run it:
Shellcd react-core-mfand:
Shellnpm startIn another window, navigate to the UI5 micro-frontend and run it:
Shellcd ui5-mfand:
Shellnpm startSee your completed app at
http://localhost:3000/. Check these tutorial outcomes:- Home switches between English and German.
- Products and Order History navigation labels are translated.
- Clicking a product opens its details with quantity, availability and price.
- Add to cart displays the translated toast; Back returns to Products.
- Order History loads the UI5 product list. Clicking an entry opens the React detail view in a Luigi modal with a translated title.
The reference implementation is Luigi Micro Frontend Application. This tutorial builds the same example functionality, not a production shopping application. If a check fails, compare the relevant files and dependency versions with the reference; a successful build alone does not verify browser interactions.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.