Skip to content

Implement translations #5

@felixb-wix

Description

@felixb-wix

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:

  1. 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>
  2. Directly from the Shell object:

    shell.translate('LangMenu_Manager_Panel_Title')
  3. 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}"  
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    core-featureEssential core functionality that's still missingpublic-apiThis issue shapes the public API of the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions