Overview
You will learn
- How to add localization to the part of your application developed with React
Prerequisites
- You have completed Build Luigi App with React and have a running Luigi Core React application.
Steps
Intro
In this tutorial, you will add German localization to the React part of your Luigi application by translating the Luigi navigation labels and the content rendered by the React micro-frontends.
In this step, you will update the Luigi configuration with the labels for the German language localization that will be implemented in the later steps.
Open
react-core-mf/public/luigi-config.js.Change the
labelattribute of theproductsandordernode as shown below:JavaScriptchildren: [ { pathSegment: "products", //<---Around line 27, change this label---> label: "PRODUCTS", //<------> icon: "product", viewUrl: "/sampleapp.html#/microfrontend/products", keepSelectedForChildren: true, children: [{ pathSegment: ':id', viewUrl: '/sampleapp.html#/microfrontend/productDetail/:id', context: { id: ':id' } }] }, { pathSegment: 'order', //<---Around line 39, change this label---> label: 'ORDERHISTORY', //<------> icon: 'history', viewUrl: 'http://localhost:8080/index.html' } ],Add the following translation for German inside the
myTranslationProviderfunction:JavaScriptvar dict = { //Around line 4, add the following: "en-US": { PRODUCTS: "Products", ORDERHISTORY: "Order History" }, "de-DE": { PRODUCTS: "Produkte", ORDERHISTORY: "Bestellungen" }, //<------> };
In this step, you will add text for your React app in multiple languages by creating a “dictionary” file.
In the previously created language.js file in react-core-mf/src, replace the content with:
export const dict = {
'de-DE': {
ITEMS: 'Produkte',
STOCKS: 'Bestand',
SELECTLANGUAGE: 'Bitte wählen Sie eine Sprache',
PRICE: 'Preis',
WELCOME_LUIGI: 'Willkommen bei Luigi - einem Micro-Frontend Framework',
DESCRIPTION: 'Beschreibung',
PRODUCTADDED: 'Das Produkt wurde hinzugefügt',
AVAILABLE: 'Verfügbar',
AVAILABLEQUANT: 'Verfügbare Anzahl: ',
ADDTOCART: 'Zu Einkaufswagen hinzufügen',
BACK: 'Zurück',
OUTOFSTOCK: 'Nicht auf Lager'
},
"en-US": {
ITEMS: "Products",
STOCKS: "Stocks",
SELECTLANGUAGE: "Please select a language",
PRICE: "Price",
WELCOME_LUIGI: "Welcome to Luigi - a micro-frontend framework",
DESCRIPTION: "Description",
PRODUCTADDED: "Product has been added to cart",
AVAILABLE: "Available",
AVAILABLEQUANT: "Available quantity: ",
ADDTOCART: "Add to cart",
BACK: "Back",
OUTOFSTOCK: "Out of stock",
},
};In the file
react-core-mf/src/views/home.js, update the options state to include both English and German as in the following:JavaScriptconst [options] = useState([{ key: 'en-US', text: 'en-US' }, { key: 'de-DE', text: 'de-DE' }]);
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.