);
};
```
## π Language Switcher
The language switcher component is automatically included in the header. You can also use it manually:
```typescript
import LanguageSwitcher from '@/components/LanguageSwitcher';
import React from 'react';
const Header: React.FC = () => {
return (
{/* or */}
);
};
```
### Language Switcher Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `className` | `string` | - | Additional CSS classes |
| `type` | `'dropdown' \| 'button'` | `'dropdown'` | Display type |
| `size` | `'small' \| 'middle' \| 'large'` | `'middle'` | Component size |
## π Translation Keys
Translation keys follow a hierarchical structure using dot notation:
```
common.save // Save button text
auth.login.title // Login page title
map.ship.status.online // Ship online status
validation.required // Validation message
```
### Adding New Translations
1. **Vietnamese** (`src/locales/vi-VN.ts`):
```typescript
export default {
// Add new key
'my.new.key': 'Nα»i dung mα»i',
};
```
2. **English** (`src/locales/en-US.ts`):
```typescript
export default {
// Add corresponding English translation
'my.new.key': 'New content',
};
```
## β Best Practices
### 1. **Consistent Key Naming**
- Use dot notation for hierarchical structure
- Group related translations together
- Use descriptive, meaningful names
- Example: `'map.ship.status.online'` instead of `'online'`
### 2. **Variable Interpolation**
- Use descriptive variable names
- Provide context for variables
- Example: `'validation.minLength': 'Minimum {min} characters required'`
### 3. **Component Organization**
- Group translations by feature/module
- Keep related keys together
- Use consistent prefixes for modules
### 4. **Performance Considerations**
- Use the `useTranslation` hook for better performance
- Avoid inline `formatMessage` calls in render loops
- Cache translated strings when used frequently
### 5. **Translation Maintenance**
- Keep all translations in sync
- Add new keys to both language files
- Review translations regularly for consistency
## π Troubleshooting
### Common Issues
#### 1. **Translation not appearing**
```typescript
// β Wrong
// β Correct - check if key exists in locale files
```
#### 2. **Locale not changing**
- Ensure `localStorage.setItem('umi_locale', locale)` is called
- Check that locale is properly configured in `.umirc.ts`
- Verify that translation files exist for the target locale
#### 3. **Missing Ant Design translations**
```typescript
// In .umirc.ts
locale: {
antd: true, // Ensure this is enabled
}
```
#### 4. **TypeScript errors**
```typescript
// Ensure proper import
import { useIntl } from '@umijs/max';
// Check if translation key exists in locale files
const intl = useIntl();
const text = intl.formatMessage({ id: 'existing.key' });
```
### Debug Mode
To debug translation issues, add logging to your component:
```typescript
import { useIntl } from '@umijs/max';
import React from 'react';
const DebugComponent: React.FC = () => {
const intl = useIntl();
console.log('Current locale:', intl.locale);
console.log('Available messages:', intl.messages);
return
Check console for debug info
;
};
```
## π Testing Translations
To test your i18n implementation:
1. **Manual Testing**:
- Switch languages using the language switcher
- Verify all text updates correctly
- Check forms with validation messages
2. **Component Testing**:
```typescript
import { render } from '@testing-library/react';
import { I18nProvider } from '@umijs/max';
import MyComponent from './MyComponent';
// Test with different locales
render(
);
```
3. **Translation Coverage**:
- Ensure all UI text is translatable
- Check that no hardcoded strings remain
- Verify all keys exist in both language files
## π Additional Resources
- [Umi Max i18n Documentation](https://umijs.org/docs/max/i18n)
- [React Intl Documentation](https://formatjs.io/docs/react-intl/)
- [Ant Design i18n Guide](https://ant.design/docs/react/internationalization)
## π Summary
Your i18n setup is now complete! You have:
β **Configuration**: Umi Max i18n properly configured
β **Translations**: Vietnamese and English translation files
β **Components**: Language switcher and example components
β **Custom Hook**: Simplified i18n API
β **Best Practices**: Guidelines for maintainable translations
The system is ready for multi-language support and can be easily extended with additional languages or translations as needed.
---
**Last Updated**: 2025-01-20
**Version**: 1.0.0