File "useTranslate.ts"

Full Path: /home/trinadezambia/public_html/student_panel/src/components/hooks/useTranslate.ts
File size: 934 bytes
MIME-type: text/x-java
Charset: utf-8

'use client';

import { useAppSelector } from '@/components/store';

/**
 * Custom hook to translate text based on current language from Redux store
 * @returns A translate function that takes a key and returns the translated string
 * 
 * @example
 * ```tsx
 * const translate = useTranslate();
 * const homeText = translate('home'); // Returns "Home" in English, "होम" in Hindi, etc.
 * ```
 */
export function useTranslate() {
  // Get translations from Redux store
  const translations = useAppSelector((state) => state.language.translations);

  /**
   * Translate a key to its corresponding translated string
   * @param key - The translation key to look up
   * @returns The translated string, or the key if translation is not found
   */
  const translate = (key: string): string => {
    // Return translated text if available, otherwise return the key
    return translations[key] || key;
  };

  return translate;
}