Skip to content
On this page

useInternationalization ​

Definition ​

Composable for internationalization management.

Basic usage ​

ts
const { 
 languages,
 currentLanguage,
 currentPrefix,
 getStorefrontUrl,
 getAvailableLanguages,
 changeLanguage,
 getLanguageCodeFromId,
 getLanguageIdFromCode,
 replaceToDevStorefront,
 formatLink 
} = useInternationalization(pathResolver);

Signature ​

ts
export function useInternationalization(
  pathResolver?: Function,
): UseInternationalizationReturn 

Parameters ​

NameTypeDescription
pathResolver
Function

Return type ​

See UseInternationalizationReturn
ts
export type UseInternationalizationReturn = {
  /**
   * StorefrontUrl is needed to specify language of emails
   *
   *  @returns {string} storefront URL
   */
  getStorefrontUrl(): string;
  /**
   * Get available languages from backend
   *
   * @returns {Promise<operations['readLanguages post /language']['body']>} list of languages
   */
  getAvailableLanguages(): Promise<
    operations["readLanguages post /language"]["response"]
  >;
  /**
   * Change current language
   *
   * @param {string} languageId
   * @returns {Promise<Schemas['ContextTokenResponse']>} context object
   */
  changeLanguage(
    languageId: string,
  ): Promise<operations["updateContext patch /context"]["response"]>;
  /**
   * Get language code from backend language id
   *
   * @param {string} languageId
   * @returns {string} language code
   */
  getLanguageCodeFromId(languageId: string): string;
  /**
   * Get backend language id from language code
   *
   * @param {string} languageCode
   * @returns {string} language
   */
  getLanguageIdFromCode(languageCode: string): string;
  /**
   * Replace to dev url if it is set
   *
   * @param {string} url
   * @returns {string} prefixed url
   */
  replaceToDevStorefront(url: string): string;
  /**
   * List of available languages
   */
  languages: Ref<Schemas["Language"][]>;
  /**
   * Currently used language
   */
  currentLanguage: Ref<string>;
  /**
   * Current prefix from the context
   */
  currentPrefix: Ref<string>;
  /**
   * Add prefix to the Url
   * @param {string | RouteObject} link
   */
  formatLink(link: string | RouteObject): string | RouteObject;
};

Properties ​

NameTypeDescription
languages
Ref<Array<>>
List of available languages
currentLanguage
Ref<string>
Currently used language
currentPrefix
Ref<string>
Current prefix from the context

Methods ​

NameTypeDescription
getStorefrontUrl
string
StorefrontUrl is needed to specify language of emails
getAvailableLanguages
Promise<>
Get available languages from backend
changeLanguage
Promise<>
Change current language
getLanguageCodeFromId
string
Get language code from backend language id
getLanguageIdFromCode
string
Get backend language id from language code
replaceToDevStorefront
string
Replace to dev url if it is set
formatLink
string | RouteObject
Add prefix to the Url
useInternationalization has loaded