0.5.0 • Published 3 years ago

@procore/labs-i18n-extensions v0.5.0

Weekly downloads
421
License
SEE LICENSE IN LI...
Repository
github
Last release
3 years ago

i18n-extensions

Disclaimer: Meant for Procore internal use only.

Utility to provide translations for the decoupled frontend applications, aka "hydra clients".
As part of the initiative to move outside the monolith, this package provides functionality to fetch translations file for a given client.

The package encapsulated core-react/I18n so the consuming application code should not change much.

Note: Consuming application should not wrap itself within <I18n.Provider> from @procore/core-react

As of now, the provider accepts locale and companyId to as optional props and fallbacks on of the window.Procore.Environment for fetching locale and company_id. In the future, intentions are to remove this dependency and accept them as config props.

P.S.: Loads global translations by default.

Install

yarn add @procore/labs-i18n-extensions

Usage

Context

import { render } from 'react-dom';
import { I18nExtensions } from '@procore/labs-i18n-extensions'; // replaces I18n
import { Application } from './Application';

render(
  <I18nExtensions clientName="${CLIENT_NAME}">
    <Application />
  </I18nExtensions>,
  document.getElementById('root')
);
Changes
import { render } from 'react-dom';
- import { I18n } from '@procore/core-react'; // removed
+ import { I18nExtensions } from '@procore/labs-i18n-extensions'; // replaces I18n
import { Application } from './Application';

render(
- <I18n.Provider I18n={I18nObject}>
+ <I18nExtensions clientName="${CLIENT_NAME}">
    <Application />
- </I18n.Provider>,
+ </I18nExtensions>,
  document.getElementById('root')
);

Hook

import { useI18nFetcher } from '@procore/labs-i18n-extensions';

// in component
const { loadTranslation, getTranslationStatus } = useI18nFetcher({
  companyId,
  locale
});

// loads translation
loadTranslation('${CLIENT_NAME}');

// returns translation status: loaded, loading, failed
const translationStatus = getTranslationStatus('${CLIENT_NAME'});

Props

Context

PropsDescriptionTypeDefault
clientNameName of the client(s) to pull translations for.string | string[]N/A (Required)
companyIdcompanyId to be passed along to the translations fetch endpoint. (optional)stringwindow.Procore.Environment.companyId
configUrlEndpoint to fetch the translations file location from. (optional)string/rest/v1.0/internal/i18n/translation_file_url
loadingIndicatorCustom loading indicator to replace default loading indicator (Spinner). (optional)ReactNodecore-react Spinner
localelocale for the translations. (optional)Localewindow.Procore.Environment.locale
noDefaultsDisable injection of default translations. (optional)booleanfalse

Hook

Params
ParamDescriptionType
companyIdcompanyId to be passed along to the translations fetch endpoint.string
configUrlEndpoint to fetch the translations file location from. (optional)string
localelocale for the translations.Locale
Returns
KeyDescriptionTypeParamReturns
loadTranslationLoad translation for a given client.Functionstringvoid
N/AI18nI18n object.N/AN/A
getTranslationStatusGet translation status for a given client.FunctionstringLoading Status: 'loaded', 'loading', 'failed'