| name | i18n-l10n |
| description | Internationalization and localization patterns for multi-locale support. Use when implementing translations, formatting dates/numbers/currency, or handling locale-specific content. Keywords - i18n, l10n, translations, locale, formatting, dates, currency, numbers. |
| license | MIT |
| metadata | {"version":"2.0.0","author":"jpmorgan-payments","lastUpdated":"2025-12-24","priority":"high"} |
i18n/l10n Guidelines
Internationalization and localization patterns for multi-locale support. All components must support multiple locales with proper formatting for dates, numbers, and currency.
When to Apply
Reference these patterns when:
- Implementing translations
- Formatting dates, numbers, or currency
- Handling locale-specific content
- Creating form labels and error messages
Supported Locales
- en-US (English - United States) - Default
- fr-CA (French - Canada)
- es-US (Spanish - United States)
⚠️ CRITICAL: TypeScript Overload Issue
Always use type assertion when calling t() function:
const t = useTranslation() as (key: string, options?: any) => string;
const title = t('transactions.title', { defaultValue: 'Transactions' });
Quick Reference
Core Principles
- Always use
useTranslation hook - Never hardcode text strings
- Locale-aware formatting - Respect current locale for dates/numbers/currency
- Default values - Always provide
defaultValue for fallback
- Type assertion - Use
as to avoid TypeScript overload issues
Essential Patterns
- Translation keys: Hierarchical, descriptive (e.g.,
transactions.columns.date)
- Locale detection: Use
useLocale() hook
- Date formatting: Use
toLocaleDateString(locale, {...})
- Currency formatting: Use
formatNumberToCurrency(amount, currency, locale)
Translation Pattern
import { useTranslation } from 'react-i18next';
export const MyComponent: FC = () => {
const t = useTranslation() as (key: string, options?: any) => string;
return (
<div>
<h1>{t('component.title', { defaultValue: 'Default Title' })}</h1>
<p>{t('component.description', {
defaultValue: 'Description text',
name: userName
})}</p>
</div>
);
};
Translation Keys
Namespace Organization
- Each component/feature: own namespace (
transactions, accounts)
- Common translations:
common namespace
- Validation:
validation namespace
Key Structure
t('transactions.columns.date', { defaultValue: 'Date' })
t('transactions.errors.failedToLoadTitle', { defaultValue: 'Failed to load transactions' })
t('transactions.toolbar.allStatuses', { defaultValue: 'All statuses' })
t('date')
t('error')
t('all')
Default Values
Always provide defaultValue:
t('transactions.title', { defaultValue: 'Transactions' })
t('transactions.title')
Locale Detection
Use the useLocale() hook:
import { useLocale } from '@/lib/hooks';
const MyComponent = () => {
const locale = useLocale();
};
Language Code vs Locale String
- Language Code: i18next format ('enUS', 'frCA')
- Locale String: Intl API format ('en-US', 'fr-CA')
- Convert using
getLocaleFromLanguage() utility
Date Formatting
Rules
- Always use locale parameter (never hardcode 'en-US')
- Use appropriate format (Date vs DateTime)
- Handle undefined/null with fallback
Pattern
import { useLocale } from '@/lib/hooks';
const MyComponent = () => {
const t = useTranslation() as (key: string, options?: any) => string;
const locale = useLocale();
const naText = t('common:na', { defaultValue: 'N/A' });
const formatDate = (date?: string): string => {
if (!date) return naText;
return new Date(date).toLocaleDateString(locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
});
};
const formatDateTime = (date?: string): string => {
if (!date) return naText;
return new Date(date).toLocaleString(locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
};
};
Do's and Don'ts
const locale = useLocale();
const formatted = new Date(date).toLocaleDateString(locale, {...});
if (!date) return naText;
new Date(date).toLocaleDateString('en-US', {...});
new Date(date).toLocaleDateString(locale, {...});
Currency Formatting
Rules
- Use
formatNumberToCurrency utility
- Always pass locale
- Handle undefined amounts
Pattern
import { formatNumberToCurrency } from './utils';
import { useLocale } from '@/lib/hooks';
const MyComponent = () => {
const locale = useLocale();
const t = useTranslation() as (key: string, options?: any) => string;
const naText = t('common:na', { defaultValue: 'N/A' });
const formattedAmount = transaction.amount
? formatNumberToCurrency(
transaction.amount,
transaction.currency ?? 'USD',
locale
)
: naText;
};
Utility Function
export const formatNumberToCurrency = (
amount: number,
currency: string,
locale: string = 'en-US'
): string => {
const formatter = new Intl.NumberFormat(locale, {
style: 'currency',
currency,
});
return formatter.format(amount);
};
Number Formatting
const formatNumber = (value: number, locale: string = 'en-US'): string => {
return new Intl.NumberFormat(locale).format(value);
};
const formatPercentage = (value: number, locale: string = 'en-US'): string => {
return new Intl.NumberFormat(locale, {
style: 'percent',
minimumFractionDigits: 0,
maximumFractionDigits: 2,
}).format(value);
};
const formatDecimal = (
value: number,
decimals: number = 2,
locale: string = 'en-US'
): string => {
return new Intl.NumberFormat(locale, {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
}).format(value);
};
Pluralization
{
"items": "{{count}} item",
"items_plural": "{{count}} items"
}
const itemCount = t('items', {
defaultValue: '{{count}} item',
count: items.length
});
Interpolation
{
"welcome": "Welcome, {{name}}!",
"balance": "Your balance is {{amount}}"
}
const welcome = t('welcome', {
defaultValue: 'Welcome, {{name}}!',
name: userName
});
const balance = t('balance', {
defaultValue: 'Your balance is {{amount}}',
amount: formatNumberToCurrency(amount, 'USD', locale)
});
Context-Specific Translations
{
"status_pending": "Pending",
"status_completed": "Completed",
"status_failed": "Failed"
}
const getStatusText = (status: Status): string => {
return t(`status_${status.toLowerCase()}`, {
defaultValue: status
});
};
Best Practices
- Type assertion - Always use
as for t function
- Default values - Required for all translation calls
- Locale parameter - Never hardcode, use
useLocale()
- Fallback text - Handle undefined/null values
- Namespace organization - Group related translations
- Descriptive keys - Use hierarchical, clear keys
- Consistent formatting - Use utility functions
Common Patterns
Form Labels and Placeholders
const t = useTranslation() as (key: string, options?: any) => string;
<Input
label={t('form.email.label', { defaultValue: 'Email Address' })}
placeholder={t('form.email.placeholder', { defaultValue: 'Enter your email' })}
error={t('form.email.error', { defaultValue: 'Invalid email address' })}
/>
Error Messages
const t = useTranslation() as (key: string, options?: any) => string;
<ServerErrorAlert
error={apiError}
customErrorMessage={{
"400": t('errors.badRequest', {
defaultValue: 'Please check your information.'
}),
"401": t('errors.unauthorized', {
defaultValue: 'Please log in again.'
}),
"500": t('errors.serverError', {
defaultValue: 'An unexpected error occurred.'
}),
default: t('errors.default', {
defaultValue: 'An error occurred.'
}),
}}
/>
Table Columns
const columns = [
{
key: 'date',
label: t('columns.date', { defaultValue: 'Date' }),
render: (row) => formatDate(row.date, locale),
},
{
key: 'amount',
label: t('columns.amount', { defaultValue: 'Amount' }),
render: (row) => formatNumberToCurrency(row.amount, row.currency, locale),
},
];
Testing with Locales
import { renderWithProviders } from '@test-utils';
test('displays formatted date in French', () => {
renderWithProviders(<Component />, { locale: 'fr-CA' });
expect(screen.getByText(/janv/i)).toBeInTheDocument();
});
test('displays formatted currency in Canadian dollars', () => {
renderWithProviders(<Component />, { locale: 'fr-CA' });
expect(screen.getByText(/\$\s*1\s*234,56/)).toBeInTheDocument();
});
Anti-Patterns
❌ Hardcoded text strings
❌ Hardcoded 'en-US' locale
❌ Missing default values
❌ No fallback for undefined values
❌ Not using type assertion for t function
❌ Flat, ambiguous translation keys
How to Use
For detailed instructions, examples, and patterns:
- Full guide:
AGENTS.md - Complete i18n/l10n documentation
References