Overview
TheuseTranslations hook provides access to the application’s translation system. It retrieves the current language setting from the Settings Context and returns the appropriate translation object. This hook is used throughout the application for internationalization (i18n).
Import
Signature
Parameters
This hook takes no parameters.Return Value
Returns a translation object for the current language containing all localized strings. The object structure includes:Common translations used across the app:
errors: Error messages (e.g.,loadData,selectAsset,updatePortfolio)- Other common UI strings
Dashboard-specific translations:
totalPortfolioBalance: “Total Portfolio Balance” labelportfolio24hChange: “24h Change” labelbtcPrice: “Bitcoin Price” labelethPrice: “Ethereum Price” labelperformance: “Performance” labelperiod: Object with time periods (30d,7d, etc.)
Page titles for document metadata:
dashboard: Dashboard page titleportfolio: Portfolio page titlemarket: Market page titletransactions: Transactions page titlesettings: Settings page titleanalysis: Analysis page titlenotFound: 404 page title
Page descriptions for SEO meta tags, matching the structure of
pageTitlesAdditional translation groups for other features and components
Usage Examples
Basic Usage in Components
DashboardPage.jsx:8-12
Error Messages
Navigation and Sidebar
Sidebar.jsx:5-31
Table Headers
MarketTable.jsx:1-4
Settings Page
SettingsPage.jsx:5-15
Implementation Details
Language Detection
The hook uses theuseSettings hook from the Settings Context to retrieve the current language preference:
useTranslations.js:1-7
Fallback Behavior
- If the selected language is not available, the hook falls back to English (
translations.en) - This ensures the application always has valid translations, even if a language is incomplete
Translation Structure
Translations are organized hierarchically:Best Practices
- Call at component top level: Always call
useTranslations()at the beginning of your component - Store in
tvariable: Use the conventionaltvariable name for consistency - Don’t destructure deeply: Access translations via dot notation (e.g.,
t.dashboard.title) rather than destructuring - Check translation keys: Ensure all translation keys exist in all language files to avoid undefined values
Common Translation Keys
| Key | Description | Example |
|---|---|---|
t.common.errors.loadData | Generic data loading error | ”Failed to load data” |
t.pageTitles.[page] | Page title | ”Dashboard” |
t.pageDescriptions.[page] | Page meta description | ”View your crypto portfolio” |
t.dashboard.performance | Performance chart title | ”Performance” |
Related Hooks
- useDocumentTitle - Often used together with translations for page titles
- useDashboardData - Uses translations internally for error messages
Related Context
- SettingsContext: Provides the current language setting via
useSettingshook
Source Location
/workspace/source/src/hooks/useTranslations.js:4