Design proposal
All translations will be done through a translation function, which will receive a string key and optional placeholder values, and return formatted translation:
type TranslationFunc = (key: string, params?: {[name: string]: any}) => string
Since retrieval of translated texts is widely used, it should be as laconic as possible.
Consumption
There will be three ways to obtain the translation function:
-
Pure components connected through connectWithShell will get it in props, under the name t:
const { t } = props
return <span>{t('LangMenu_Manager_Panel_Title')}</span>
-
Directly from the Shell object:
shell.translate('LangMenu_Manager_Panel_Title')
-
Through ShellContext (useful in a non-connected component):
<ShellContext.Consumer>{shell =>
<span>{shell.translate('LangMenu_Manager_Panel_Title')}</span>
}</ShellContext.Consumer>
Injection
Assumptions
- single locale per page
- locale cannot be changed without reloading the page
Proposal
Translation data will be scoped per Shell (which means per Entry Point)
- Every entry point will be able to contribute a different translation data
- This allows slicing of translation data per team
If translation data is of default structure (defined below), default translation function can be used. For data of custom structure, custom translation function must be used.
- For default structure, an Entry Point has to contribute translation data:
shell.getAPI(AppHostAPI).contributeTranslations(myTranslationData)
- For custom structure, an Entry Point has to setup a custom translation function:
shell.getAPI(AppHostAPI).useTranslationFunction(myTranslationFunc)
In the latter case translation data is not passed: myTranslationFunc must be bound to its data
Default structure of translation data
{
"first_key": "translation 1",
"second_key": "translation with named {param-name} parameters",
"third_key": "translation with indexed {0} parameters {1}"
}
Design proposal
All translations will be done through a translation function, which will receive a string key and optional placeholder values, and return formatted translation:
Since retrieval of translated texts is widely used, it should be as laconic as possible.
Consumption
There will be three ways to obtain the translation function:
Pure components connected through
connectWithShellwill get it in props, under the namet:Directly from the
Shellobject:Through
ShellContext(useful in a non-connected component):Injection
Assumptions
Proposal
Translation data will be scoped per
Shell(which means per Entry Point)If translation data is of default structure (defined below), default translation function can be used. For data of custom structure, custom translation function must be used.
myTranslationFuncmust be bound to its dataDefault structure of translation data
{ "first_key": "translation 1", "second_key": "translation with named {param-name} parameters", "third_key": "translation with indexed {0} parameters {1}" }