@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.
💡 This package includes Typescript definitions
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.
A complete example of implementing package translations in your Next.js app can be found here.
_document.tsx
_app.tsx
server.tsx
client.tsx
App.tsx
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.
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:@volvo-cars/react-faqs
package in the showMore
itemi18n
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.