React Shared Translations🔗

@volvo-cars/react-shared-translations

@volvo-cars/react-shared-translations
@volvo-cars/react-shared-translations
@volvo-cars/react-shared-translations

This package is meant to be used as a wrapper for the other i18n supported @volvo-cars packages. It helps serve localized translations for all the supported locales in the packages that serve them. It serves translations without affecting your bundle sizes and keeps them to a minimum by only providing the necessary translations. It supports both server-side and client-side rendering.

Installation🔗

💡 This package includes Typescript definitions

Getting started🔗

The idea is that you'll wrap your React application with a TranslatorProvider and feed it a locale and a translator props. When the React is done rendering your application, the translator will then have translator.cache showing all the translations strings used in this rendering in the current locale. If you are doing server-side rendering you'll then use the translator cache and render a TranslatorCache component and serve it on your page. This component will generate JSON data that will be used by your application client-side.

Next.js Example🔗

A complete example of implementing package translations in your Next.js app can be found here.

Express Example🔗

Testing🔗

To avoid having to wait for translations to be asynchronasly import during testing, we provided a MockedProvider components that makes testing much easier. It takes an optional mocks prop of type Record<string, string>, where the key is the key used by the packages interally, ex: react-faqs.showMore. If mocks is not provided, the key will be returned instead.

Package Developers🔗

The following section is for developers that work on @volvo-cars packages and want to localize strings in their packages.

A convention needs to be followed in the packages that will serve translated strings and it goes as follows:

  • useTranslate hook must be called with packageName.itemKey Ex:
    We now know that the required string exists in the @volvo-cars/react-faqs package in the showMore item
  • Translation files must be inside a i18n directory with ${locale}.json as file names inside the package that needs them, Ex:

useTranslate takes an optional second argument that decides if the translation should happen or not:

The above will return undefined as long as disabled is true. This is to allow you to override the translation with something else like a prop provided by the consumers of your package.

2022 © Volvo Car Corporation