|
| 1 | +import { Meta } from '@storybook/addon-docs/blocks'; |
| 2 | + |
| 3 | +<Meta title="Internationalization" /> |
| 4 | + |
| 5 | +# Setup Internationalization (i18n) |
| 6 | + |
| 7 | +UI5 Web Components (for React) aim to be feature rich and with a minimal code footprint at the same time. |
| 8 | +In order to achieve this, most UI5 Web Components packages ship their assets as `.json` files while also providing a public module import for them. |
| 9 | + |
| 10 | +**Prerequisite: your `@ui5/webcomponents-react` dependency has to be at least at `0.10.0-rc.2` or newer.** |
| 11 | + |
| 12 | +In order to make your app translatable into various languages, you need to import the following assets: |
| 13 | + |
| 14 | +```js |
| 15 | +import '@ui5/webcomponents/dist/Assets.js'; |
| 16 | +import '@ui5/webcomponents-fiori/dist/Assets.js'; |
| 17 | +import '@ui5/webcomponents-react/dist/Assets'; |
| 18 | +``` |
| 19 | + |
| 20 | +That's it! You can now test whether the translations work correctly by adding e.g. `?sap-ui-language=de` to your URL for German translations. |
| 21 | + |
| 22 | +<br /> |
| 23 | +<br /> |
| 24 | + |
| 25 | +## How to add custom translations |
| 26 | + |
| 27 | +_Please also read the [UI5 Web Components i18n documentation](https://github.com/SAP/ui5-webcomponents/blob/master/docs/i18n.md)._ |
| 28 | +_This chapter requires `@ui5/webcomponents@>1.0.0-rc.8`._ |
| 29 | + |
| 30 | +**1. Start by creating some `i18n` resources in a directory that can be served, for example:** |
| 31 | + |
| 32 | +| File | Content | |
| 33 | +| ------------------------------------ | -------------------------- | |
| 34 | +| `assets/messagebundle_de.properties` | `PLEASE_WAIT=Bitte warten` | |
| 35 | +| `assets/messagebundle_fr.properties` | `PLEASE_WAIT=Patientez.` | |
| 36 | +| `assets/messagebundle_es.properties` | `PLEASE_WAIT=Espere` | |
| 37 | +| `assets/messagebundle_en.properties` | `PLEASE_WAIT=Please wait` | |
| 38 | + |
| 39 | +**2. Import the following `i18n`-related modules to your app:** |
| 40 | + |
| 41 | +```js |
| 42 | +import '@ui5/webcomponents-base/dist/features/PropertiesFormatSupport.js'; |
| 43 | +import { registerI18nBundle, fetchI18nBundle, getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle.js'; |
| 44 | +``` |
| 45 | + |
| 46 | +The first one provides support for `.properties` files, as used in the example and the second one contains the functions |
| 47 | +that will allow you to take advantage of the `i18n` functionality. |
| 48 | + |
| 49 | +**3. Register your message bundles:** |
| 50 | + |
| 51 | +```js |
| 52 | +registerI18nBundle('myApp', { |
| 53 | + de: './assets/messagebundle_de.properties', |
| 54 | + es: './assets/messagebundle_es.properties', |
| 55 | + fr: './assets/messagebundle_fr.properties', |
| 56 | + en: './assets/messagebundle_en.properties' |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +The first argument is an ID that will be used to reference this message bundle and the second, an object, |
| 61 | +providing the URLs where the `i18n` assets can be found. |
| 62 | + |
| 63 | +_Note:_ This is just asset registration, no data will be fetched at that point. |
| 64 | + |
| 65 | +**4. Use your translated texts in your components** |
| 66 | + |
| 67 | +Add the following import statement to the component where you want to use translated texts: |
| 68 | + |
| 69 | +```js |
| 70 | +import { useI18nText } from '@ui5/webcomponents-react-base/lib/hooks'; |
| 71 | +``` |
| 72 | + |
| 73 | +Now, you can use the `useI18nText` hook in your functional components in order to get your translated texts. |
| 74 | + |
| 75 | +The hook has the following signature: |
| 76 | + |
| 77 | +```js |
| 78 | +const translatedTextsArray = useI18nText((messageBundleId: string), (...textsToTranslate: (string | string[])[])); |
| 79 | +``` |
| 80 | + |
| 81 | +In case you have texts without placeholder values you can use the simple `string` arguments, but if you have parameters |
| 82 | +in your message bundle you will have to use the `array` notation (_please see step 6_). |
| 83 | +You can mix `string` and `array` arguments in the same call. |
| 84 | + |
| 85 | +Each parameter will be translated one by one and returned in an array in the same order. |
| 86 | + |
| 87 | +**Example:** |
| 88 | + |
| 89 | +```jsx |
| 90 | +const MyTranslatedTextComponent = () => { |
| 91 | + const [pleaseWaitText, anotherText] = useI18nText('myApp', 'PLEASE_WAIT', 'ANOTHER_TEXT_TO_TRANSLATE'); |
| 92 | + |
| 93 | + return ( |
| 94 | + <div> |
| 95 | + <span>{pleaseWaitText}</span> |
| 96 | + <p>{anotherText}</p> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +**5. Use texts with placeholder values** |
| 103 | + |
| 104 | +In case you have texts with placeholders in your message bundle, you can pass an array as text parameter to receive |
| 105 | +translated text with parameters. In this case, the first entry in the array is the translation key, followed by an |
| 106 | +arbitrary number of parameters which should be inserted into the translation. |
| 107 | + |
| 108 | +**Example:** |
| 109 | + |
| 110 | +You have this text in your message bundle: |
| 111 | + |
| 112 | +```properties |
| 113 | +CAROUSEL_DOT_TEXT=Item {0} of {1} displayed |
| 114 | +``` |
| 115 | + |
| 116 | +Your hook call would now look like this: |
| 117 | + |
| 118 | +```js |
| 119 | +const [carouselText, pleaseWaitText] = useI18nText('myApp', ['CAROUSEL_DOT_TEXT', 5, 20], 'PLEASE_WAIT'); |
| 120 | +``` |
| 121 | + |
| 122 | +This would resolve this text:<br /> |
| 123 | +`Item 5 of 20 displayed` |
0 commit comments