# Vietnamese Translation Setup Guide

## Current Status ✓

The BMC legal pages now support Vietnamese (and English) with the following completed:

### ✓ What's Done

1. **Templates Updated with i18n Tags**
   - `legal/templates/legal/base.html` - Added `{% load i18n %}` and translation markers
   - `legal/templates/legal/privacy.html` - Added `{% load i18n %}` and 200+ translation strings
   - `legal/templates/legal/terms.html` - Added `{% load i18n %}` and 300+ translation strings

2. **Language Switcher Added**
   - Simple English/Tiếng Việt toggle in header
   - Click links to switch between `/en/` and `/vi/` URLs

3. **Vietnamese Translation File Created**
   - `locale/vi/LC_MESSAGES/django.po` - Complete Vietnamese translations
   - Contains translations for all legal page content

4. **Django Configuration**
   - `i18n_patterns()` in `BMC/urls.py` handles language routing
   - Supports both English (default) and Vietnamese

---

## Testing Translation Toggle

The language switcher now works! Visit:

- **English Version**: http://127.0.0.1:8000/en/legal/privacy/
- **Click "Tiếng Việt"** in the header to switch to Vietnamese

### Current Issue: .mo File Not Compiled

The .po file has been created but needs to be compiled to a .mo (Machine Object) file for Django to use the translations. On Windows, this requires GNU gettext tools.

---

## ✓ How to Compile Translations (Fix for Full Vietnamese Support)

### Option 1: Using Windows Subsystem for Linux (WSL) or Git Bash

```bash
cd d:\Huy\Project\programing\website-bmc
python manage.py compilemessages -l vi
```

If you get "Can't find msgfmt" error, install gettext:

#### For Git Bash:
```bash
pacman -S gettext
```

#### For WSL:
```bash
sudo apt-get install gettext
```

---

### Option 2: Install gettext on Windows (Recommended)

1. Download from: https://mlocati.github.io/articles/gettext-iconv-windows.html
2. Extract to a folder (e.g., `C:\gettext`)
3. Add to Windows PATH:
   - Settings → Environment Variables
   - Add `C:\gettext\bin` to PATH
4. Restart terminal and run:
   ```bash
   cd d:\Huy\Project\programing\website-bmc
   python manage.py compilemessages -l vi
   ```

---

### Option 3: Use Python to Compile (If msgfmt not available)

Create a file `compile_translations.py` in your project root:

```python
import os
import subprocess
import sys

os.chdir(os.path.dirname(os.path.abspath(__file__)))

try:
    result = subprocess.run([sys.executable, 'manage.py', 'compilemessages', '-l', 'vi'], check=True)
    print("Translations compiled successfully!")
except subprocess.CalledProcessError as e:
    print(f"Error: {e}")
    print("Install gettext and try again")
except FileNotFoundError:
    print("Could not find msgfmt. Please install GNU gettext tools.")
```

Then run:
```bash
python compile_translations.py
```

---

## What to Do After Compiling

Once you run `python manage.py compilemessages -l vi`:

1. A file `locale/vi/LC_MESSAGES/django.mo` will be created
2. Visit http://127.0.0.1:8000/vi/legal/privacy/
3. The page should display in Vietnamese
4. Test the language switcher to toggle between English and Vietnamese

---

## File Structure

```
website-bmc/
├── locale/
│   ├── en/
│   │   └── LC_MESSAGES/
│   └── vi/
│       └── LC_MESSAGES/
│           ├── django.po          (Translation source - Vietnamese)
│           └── django.mo          (Compiled translations - NEEDS UPDATE)
├── legal/
│   ├── templates/legal/
│   │   ├── base.html             (Updated with {% load i18n %})
│   │   ├── privacy.html          (Updated with {% trans %} tags)
│   │   └── terms.html            (Updated with {% trans %} tags)
│   └── ...
└── ...
```

---

## Customizing Translations

To modify Vietnamese translations:

1. Edit `locale/vi/LC_MESSAGES/django.po`
2. Find the string you want to translate:
   ```
   msgid "Privacy Policy"
   msgstr "Chính Sách Bảo Mật"
   ```
3. Change the `msgstr` value
4. Save the file
5. Run `python manage.py compilemessages -l vi` to apply changes
6. Restart the Django dev server

---

## Adding More Languages

To add another language (e.g., French):

1. Create locale directory:
   ```bash
   mkdir -p locale/fr/LC_MESSAGES
   ```

2. Extract translatable strings:
   ```bash
   python manage.py makemessages -l fr
   ```

3. Edit `locale/fr/LC_MESSAGES/django.po` with French translations

4. Compile:
   ```bash
   python manage.py compilemessages -l fr
   ```

5. Add language to `BMC/settings.py`:
   ```python
   LANGUAGES = [
       ("en", _("English")),
       ("vi", _("Vietnamese")),
       ("fr", _("French")),
   ]
   ```

6. Add toggle in `legal/templates/legal/base.html`:
   ```html
   <a href="?set_language=fr">Français</a>
   ```

---

## Troubleshooting

### 1. Vietnamese Page Still Shows English

**Problem**: You've compiled translations but Vietnamese still shows English text

**Solution**:
- Make sure you're visiting `/vi/legal/privacy/` (not just `/legal/privacy/`)
- Clear browser cache (Ctrl+Shift+Delete)
- Restart Django server: `python manage.py runserver`
- Check that `locale/vi/LC_MESSAGES/django.mo` exists and is larger than 1KB

### 2. "Can't find msgfmt" Error

**Problem**: Can't compile .po files on Windows

**Solution**:
- Install GNU gettext tools (see Option 2 above)
- Or use WSL/Git Bash with gettext installed
- Or use Option 3 (Python wrapper) if it works

### 3. Language Switcher Links Don't Work

**Problem**: Clicking English/Tiếng Việt buttons doesn't change language

**Solution**:
- You need the `set_language` view in URLs
- Add to `BmcBase/urls.py`:
  ```python
  from django.views.i18n import set_language
  
  urlpatterns = [
      ...
      path('i18n/', set_language, name='set_language'),
  ]
  ```

---

## Quick Checklist for Full Vietnamese Support

- [ ] Install GNU gettext tools
- [ ] Run: `python manage.py compilemessages -l vi`
- [ ] Verify `locale/vi/LC_MESSAGES/django.mo` file exists (should be ~100KB+)
- [ ] Test Vietnamese page: http://127.0.0.1:8000/vi/legal/privacy/
- [ ] Verify all content displays in Vietnamese
- [ ] Test language switcher
- [ ] Deploy with compiled .mo files

---

## Notes for Production

**Important**: Always commit both `.po` and `.mo` files to git:

```bash
git add locale/vi/LC_MESSAGES/django.po
git add locale/vi/LC_MESSAGES/django.mo
git commit -m "Add Vietnamese translations for legal pages"
```

On production server, make sure to run after pulling:

```bash
python manage.py compilemessages
```

---

## References

- [Django i18n Documentation](https://docs.djangoproject.com/en/4.0/topics/i18n/)
- [Django Translation Framework](https://docs.djangoproject.com/en/4.0/topics/i18n/translation/)
- [GNU gettext Tools](https://www.gnu.org/software/gettext/)

---

**Status**: English fully working ✓  
**Vietnamese**: Needs compilation of .po → .mo file  
**Date**: April 18, 2026
