@lwrjs/i18n
The i18n API lets you add internationalization support to your Lightning Web Runtime on Node.js (LWR-JS) application. Based on a user's locale, you can use this package to:
- Translate strings into different languages
- Load different content on a page
- Translate user-inputted text
For LWR-JS, you have to format your locales as UTS Locale Identifiers. A locale identifier has the structure language-region-script
. The language
value is required while region
and script
are optional.
For example, the en-US
identifier refers to English (en
) spoken in the United States (US
). The en
identifier only refers to English without a specific geographical region.
To set which locales your application supports, create an i18n
section in lwr.config.json
and add a subsection called locales
. To set the default locale for your site's content, create a defaultLocale
subsection and set it to a single locale.
To implement the locale rules that you created in Set Supported Locales in lwr.config.json, you have to set up a URI pattern for handling locales. With LWR-JS, you can choose from path prefix routing and custom routing.
Path prefix routing adds locale information to the user's URL. To use path prefix routing in your site, add "uriPattern": "path-prefix"
to the i18n
section of lwr.config.json
.
For example, let's say the site MyExampleSite
uses the following i18n
configuration.
This site's URL is https://myexamplesite.com
. It has the route /blog
, which supports these URLs.
https://myexamplesite.com/blog
https://myexamplesite.com/fr/blog
https://myexamplesite.com/nl-nl/blog
Although the MyExampleSite
website registers each route with the default locale prefix, users that try to access those URLs (like https://myexamplesite.com/en-us/blog
) get redirected to that URL without the prefix (https://myexamplesite.com/blog
).
If you don't set uriPattern
in lwr.config.json
, you have to use the :locale
token in your routes
configuration to define how URLs represent localized pages.
Here's an example of a config file for a site with a /blog
route that uses custom URI routing.
If you want your API calls to have access to locale information, the following objects have a locale
property that you can reference.
The page information object (page
) includes a locale property that you can use in your template.
The SsrRequestContext
object also contains the locale
property.
When a component is server-side rendered (SSRed), LWR-JS calls the getServerData()
hook. This function returns public properties to the component.
Only in LWR-JS, the lwr/environment
module contains the current locale in the locale
value. locale
doesn't get populated within the Salesforce platform, but you can import it like this: