设置国际化i18n Django

时间:2019-03-14 17:24:38

标签: python django internationalization django-admin django-i18n

在stackoverflow上贴了很多文章,以及在Django Admin中实现i18n国际化的大量教程之后,我建立了以下步骤:

命令行安装(在右侧的ENV中)

    Using PIP: pip install python-gettext (I tried this)

    Using Conda: conda install gettext

settings.py

    MIDDLEWARE = [
                 ...
                 'django.contrib.sessions.middleware.SessionMiddleware',
                 'django.middleware.locale.LocaleMiddleware', #right place
                 'django.middleware.common.CommonMiddleware',
                 ...
    ]
    ...'context_processors': [
                 ...
                 'django.template.context_processors.i18n', # Indifferent place
                 ...
    ]
    ...
    TIME_ZONE = 'UTC'  # America/Sao_Paulo
    USE_I18N = True
    USE_L10N = True        
    USE_TZ = True        
    ugettext = lambda s: s        
    LANGUAGES = (
            ( 'en-us', ugettext( 'English' )),
            ( 'pt-br', ugettext( 'Portuguese' )),
            ( 'es', ugettext( 'Spanish' )),
    )        
    LANGUAGE_CODE = 'en-us' #default
    ...
    SITE_ROOT = os.path.dirname( os.path.realpath( __file__ ) )
    PROJECT_PATH = os.path.abspath( os.path.dirname( __name__ ) )
    LOCALE_PATHS = ( os.path.join( SITE_ROOT, 'locale' ), ) # translation files will be created into 'locale' folder from root project folder

urls.py

admin.autodiscover()
urlpatterns = [
        url( r'^favicon.ico$',
             RedirectView.as_view( url = staticfiles_storage.url( 'images/favicon.png' ), ),
             ),
        ...
]
<b>urlpatterns += i18n_patterns</b>(

        path( r'admin/',
              admin.site.urls
              ),
       ... 
)
urlpatterns += static(
        settings.STATIC_URL, document_root = settings.STATIC_ROOT ) \
             + static(
        settings.MEDIA_URL, document_root = settings.MEDIA_ROOT )

models.py (任意)

    from django.core.validators import RegexValidator
    from django.db import models
    from django.utils.translation import ugettext as _
    ...
    class BusinessType(models.Model):
        businesstype_name = models.CharField(
             verbose_name = _( 'Business Name' ),
             help_text = _( 'Name, Field Type, Activity' ),
             max_length = 32, )

Windows用户(必须)

Windows用户需要从gnome项目下载两个软件包,以将i18n国际化翻译文件构建到正确的文件夹/目录中-因此请将其下载到正确的Windows 64/32位系统gettext-runtime-(last file version)_win64win32 < i>并下载gettext-tools-(最新文件版本),将所有文件解压缩到同一文件夹(BIN文件夹中的gettext-tools),然后添加Windows PATH变量以识别这些文件。

MAC用户(必须选中此选项)

我读了很多拼写错误的文字,这些文字将大小写/驼峰大写转换为“语言”代码是区分大小写的,因此请小心使用pt- Br 之类的东西:

    LANGUAGES = (
            ( 'en-us', ugettext( 'English' )),
            ( 'pt-br', ugettext( 'Portuguese' )),
            ( 'es', ugettext( 'Spanish' )),
    )    

构建模板文件(.po)(用于翻译)

    At terminal try this to built all specified languages in LANGUAGES and put "-v 3" to show how magic happening 

    python manage.py makemessages --all -v 3

什么也没发生(某人也有同样的无错误)

所有这些步骤将导致创建一个“ locale”文件夹,一个“ python manage.py makemessages”可以正常工作,并且不列出任何内容,不创建,不执行任何操作,甚至不执行任何错误。 对此我真的迷失了,在那之后,我如何检索当前选择的语言代码以创建不可能的i18n翻译的替代代码,例如:

   PHONE REGEX rules, STATE list,...

1 个答案:

答案 0 :(得分:0)

您可以尝试指定要创建的目标语言吗?

例如西班牙语:

python manage.py makemessages -l es