translate(key: TranslationKeys, options?: TTranslationOptions): string¶
The translate
method allows to translate a given phrase to a currently selected language in the shop.
Input parameters¶
key¶
key
is a mandatory parameter of TranslationKeys type which represents a key that will be translated.
options¶
options
is an optional parameter of TTranslationOptions type which represents options of a translation.
Returned value¶
A returned value has a type of string
which represents a translated phrase based on the provided key and a currently selected language in the shop.
Example¶
In this example we make a TranslationsApi
call to translate a phrase.
useStorefront(async (storefront) => {
const translationsApi = await storefront.getApi('TranslationsApi');
const translatedPhrase = translationsApi.translate('Example phrase');
});
Translations JSON format¶
When creating an own module you can add your custom translations in a JSON format. This JSON should contain flat, one level key value translations in order for this method to read them properly.
Example¶
{
"button": "przycisk",
"accept": "akceptuj",
"You need to check all required conditions": "Musisz zaznaczyć wszystkie wymagane warunki"
}