diff --git a/docs/_sidebar.md b/docs/_sidebar.md index f1448adf6..ae51a30bd 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -4,6 +4,7 @@ - [Usage](/getting-started/usage) - [Themes](/getting-started/themes) - [Customizing](/getting-started/customizing) + - [Localization](/getting-started/localization) - Frameworks - [React](/frameworks/react) diff --git a/docs/getting-started/localization.md b/docs/getting-started/localization.md new file mode 100644 index 000000000..a409847cb --- /dev/null +++ b/docs/getting-started/localization.md @@ -0,0 +1,136 @@ +# Localization + +Components can be localized by importing the appropriate translation file and setting the desired [`lang` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang) on the `` element. Here's an example that renders Shoelace components in Spanish. + +```html + +
+ + + + + ... + + +``` + +Through the magic of a mutation observer, changing the `lang` attribute will automatically update all localized components to use the new locale. + +?> Shoelace provides a localization mechanism for component internals. This is not designed to be used as localization tool for your entire application. You should use a more appropriate tool such as [i18next](https://www.i18next.com/) if you need to localize content in your app. + +## Available Translations + +Shoelace ships with the following translations. The default is English (US), which also serves as the fallback locale. As such, you do not need to import the English translation. + +- `en` - English (US) +- `de` - German +- `es` - Spanish (Latin America) +- `ja` - Japanese +- `pl` - Polish +- `pt` - Portuguese +- `ru` - Russian + +The location of translations depends on how you're consuming Shoelace. + +- If you're using the CDN, [import them from the CDN](https://www.jsdelivr.com/package/npm/@shoelace-style/shoelace?path=dist%2Ftranslations) +- If you're using a bundler, import them from `@shoelace-style/shoelace/dist/translations/[lang].js` + +You do not need to load translations up front. You can import them dynamically even after updating the `lang` attribute. Once a translation is registered, localized components will update automatically. + +```js +// Same as setting +document.documentElement.lang = 'de'; + +// Import the translation +import('/path/to/shoelace/dist/translations/de.js'); +``` + +### Translation Resolution + +The locale set by `` is the default locale for the document. If a country code is provided, e.g. `es-PE` for Peruvian Spanish, the localization library will resolve it like this: + +1. Look for `es-PE` +2. Look for `es` +3. Fall back to `en` + +Shoelace uses English as a fallback to provide a better experience than rendering nothing or throwing an error. + +### Submitting New Translations or Improvements + +To contribute new translations or improvements to existing translations, please submit a pull request on GitHub. Translations are located in [`src/translations`](https://github.com/shoelace-style/shoelace/blob/next/src/translations) and can be edited directly on GitHub if you don't want to clone the repo locally. + +Regional translations are welcome! For example, if a German translation (`de`) exists it's perfectly acceptable to submit a German (Switzerland) (`de-CH`) translation. + +If you have any questions, please start a [discussion](https://github.com/shoelace-style/shoelace/discussions) or ask in the [community chat](https://discord.gg/mg8f26C). + +## Multiple Locales Per Page + +You can use a different locale for an individual component by setting its `lang` attribute. Here's a contrived example to demonstrate. + +```html + + ... + + +