<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5HGSQD2L" height="0" width="0" style="display:none;visibility:hidden" title="GTM"></iframe>

Internationalization

SWAN's internationalization (i18n) system allows SWAN components to render with fully translated and accessible text out-of-the-box. This will help to ensure a consistent user experience of our components across all supported locales.

Opting-In to i18n

Note i18n in SWAN is optional in v3 but will become required in v4.

The i18n system is enabled through our existing <SwanProvider>. You need to provide two props: swanLocale and translations.

The key is to dynamically load only the translation file for the user's current locale. The method for doing this varies depending on your application's environment.

What to Do After Opting In

Once you've configured the <SwanProvider>, SWAN components will automatically receive their translated and accessible text from the i18n context. You can now clean up your code by removing all the deprecated accessibleText... and other localized text props. The custom SWAN ESLint plugin will warn you of any existing usage of these deprecated props that you can now remove so ensure you also upgrade it to the latest version (more on linter configuration below).

Note for UBIK Consumers: The documentation for UBIK Fragments below says "No action is required" because the i18n system is enabled for you automatically. However, you can and should still perform this cleanup step. We highly recommend reviewing your code and removing any redundant text props to get the full benefit of the new system.

Setting Up Your Project For i18n

No action is required. The UBIK platform's implementation of <SwanProvider>will handle loading and providing the locale and translations for you automatically. Your SWAN components will be internationalized by default.

In server-side rendering environments like Next.js, you can fetch the translations in getServerSideProps based on the incoming request's locale.

In a CSR application (like one using Vite or Create React App), you can fetch the translations in a useEffect hook and hold them in state. Note bundlers like Vite and Rollup don’t support dynamic imports with 3rd party libraries so you need to use something similar to thegetSwanTranslations example below. If you don’t need localization support in your application, such as for internal tooling applications, you can just pass en-us to the translations instead.

In Gatsby, you create localized pages in gatsby-node.js and then use the wrapPageElement browser/SSR API to provide the i18n context.

i18n and the SWAN ESLint Plugin

Our ESLint plugin (@vp/eslint-plugin-swan) includes a rule called required-accessibility-props. This rule was created to enforce certain accessibility props (like accessibleTextFor...) that are optional in a component's TypeScript interface but are functionally required for an accessible experience.

When you opt-in to SWAN's i18n system, the i18n provider handles these props for you automatically. However, the ESLint rule doesn't know you've opted in and will continue to report them as missing, creating unnecessary errors.

To solve this, the rule includes an ignoreI18nSupportedProps option. If your project has opted into the SWAN i18n system, you should enable this option in your .eslintrc.json file to prevent these false positives.

When ignoreI18nSupportedProps is set to true, the rule will stop reporting errors for props that are now handled by the i18n provider (e.g. accessibleTextForIcon on AlertBox). Importantly, it will still enforce other required accessibility props that are not related to i18n (e.g. headingLevel on Collapsible), so you don't lose any of the rule's other benefits.

Overriding Default Translations

Sometimes a default string doesn't fit your specific context. You can override any translation by using the exported <SwanI18nProvider>.

For example, a Carousel's default accessible text for previous and next slide button is “Go to previous slide” and “Go to next slide”. When displaying multiple Carousel’s on the same page, it is necessary to override these strings making them more specific for each one to maintain accessibility. For a Carousel of product categories, you might use something like “Explore all categories: Go to next category” etc.

To do this, wrap the specific part of your component tree with <SwanI18nProvider> and pass a translations prop containing only the keys you want to change mapped to the localized string value.

Finding Translation Keys

To override a translation, you need to know its key (e.g. carousel.accessible-text-for-next-button). You can find the SWAN translation keys and values in the en-us translation file.

Placeholders/Variables

Note some translations require placeholders, please ensure any override strings contain the required placeholders matching the placeholder name and placing them inside double curly braces. You can find these in the existing translations in the link above.