Django 支持國(guó)際化,多語言撤逢,而本項(xiàng)目做國(guó)際化完全是因?yàn)閭€(gè)人不喜歡在頁面上寫N多中文的描述。
- setting配置
MIDDLEWARE_CLASSES = (
...
'django.middleware.locale.LocaleMiddleware',
)
LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#翻譯文件所在目錄,需要手工創(chuàng)建
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
template中加
'django.template.context_processors.i18n',
生成需要翻譯的文件
python manage.py makemessages -l zh_hans
python manage.py makemessages -l zh_hant編寫文件
locale/zh/LC_MESSAGES/django.po
msgid "Action"
msgstr "動(dòng)作"
msgid "Execute"
msgstr "執(zhí)行"
msgid "Update"
msgstr "更新"
msgid "Release"
msgstr "發(fā)布"
msgid "Sync"
msgstr "同步"
msgid "build_number"
msgstr "構(gòu)建號(hào)"
編譯
django-admin.py compilemessages頁面上使用
var update_btn = '<a href="{% url "devops:pci-update" pk=99991937 %}" class="btn btn-xs m-l-xs btn-info">{% trans "Update" %}</a>'.replace('99991937', cellData);
var sync_btn = '<a href="{% url "devops:pci-build-update" pk=99991937 %}" class="btn btn-xs m-l-xs btn-info">{% trans "Sync" %}</a>'.replace('99991937', cellData);
var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_pci_delete" data-uid="99991937">{% trans "Delete" %}</a>'.replace('99991937', cellData);
顯示- 遇到的錯(cuò)誤
CommandError: Can't find msgfmt.
Make sure you have GNU gettext tools 0.15 or newer installed.
解決方案:安裝gettext-iconv解決茄茁,下載地址:https://mlocati.github.io/articles/gettext-iconv-windows.html