@exabytellc/utils
Version:
EB react utils to make everything a little easier!
60 lines (57 loc) • 2.7 kB
JavaScript
import TranslationBloc from "../TranslationBloc";
/**
* Custom hook to access translation context values and functions.
*
* This hook provides the following:
* - Current locale and text direction.
* - Available locales and their data.
* - Functions to translate keys, retrieve localized values, and handle direction-based logic.
* - Ability to change the current locale.
*
* @returns {Object} An object with translation-related values and methods:
* @property {string} locale - The current locale (e.g., "en", "fr").
* @property {string} dir - The current text direction (e.g., "ltr" or "rtl").
* @property {Array<string>} localesKeys - An array of available locale keys.
* @property {Object} localesData - The data associated with the available locales.
* @property {Function} t - Function to translate a key.
* @param {string} key - The key to translate.
* @returns {string} The translated value or "N/A" if not found.
* @property {Function} tObj - Function to retrieve a localized value from an object.
* @param {Object} obj - The object containing localized values.
* @param {string} baseKey - The base key to look for.
* @returns {any} The localized value or null if not found.
* @property {Function} forLocale - Function to get a value for the current locale with a fallback.
* @param {Object} values - The object containing locale-specific values.
* @param {any} fallback - A fallback value if the locale-specific value is missing.
* @returns {any} The value for the current locale or the fallback.
* @property {Function} forDir - Function to get a value based on text direction.
* @param {any} ltr - Value for left-to-right layouts.
* @param {any} rtl - Value for right-to-left layouts.
* @returns {any} The value matching the current direction.
* @property {Function} setLocaleApp - Function to change the current locale.
* @param {string|Function} newLocale - The new locale or a function to derive it.
*/
export default function useT() {
const {
locale,
dir,
localesKeys,
localesData,
t,
tObj,
forLocale,
forDir,
changeLocale,
} = TranslationBloc();
return {
locale,
dir,
localesKeys,
localesData,
t,
tObj,
forLocale,
forDir,
changeLocale,
};
}