Auth0 Universal Components
    Preparing search index...

    Function useTranslator

    • Custom hook for accessing the i18n service from CoreClient.

      This hook provides access to the i18n service from the CoreClient context, including enhanced translation functions with Trans component support and language change capabilities.

      Parameters

      • namespace: string

        The translation namespace (e.g., 'mfa', 'common')

      • Optionaloverrides: Record<string, unknown>

        Optional translation overrides for the namespace

      Returns {
          changeLanguage: (
              language: string,
              fallbackLanguage?: string,
          ) => Promise<void>;
          currentLanguage: string;
          fallbackLanguage: string | undefined;
          t: EnhancedTranslationFunction;
      }

      An object containing the enhanced translator function and changeLanguage function

      // Basic usage with namespace only
      const { t, changeLanguage } = useTranslator('common');

      // Usage with overrides
      const { t } = useTranslator('mfa', {
      title: 'Custom Title',
      'sms.title': 'Text Message'
      });

      // Using the basic translator
      const title = t('title');
      const message = t('welcome', { name: 'John' });

      // Using the trans method for safe HTML rendering
      const elements = t.trans('help.message', {
      components: {
      link: (children) => <a href="/help" target="_blank">{children}</a>,
      strong: (children) => <strong>{children}</strong>
      },
      vars: { name: 'John' }
      });

      // Render the elements
      return (
      <div>
      {elements.map((element, index) => (
      <Fragment key={index}>{element}</Fragment>
      ))}
      </div>
      );

      // Changing language
      await changeLanguage('es-ES');