Home > Backend Development > Python Tutorial > Django supports Chinese setting methods

Django supports Chinese setting methods

巴扎黑
Release: 2017-08-17 11:10:38
Original
3441 people have browsed it

I created a Django project today, using the current Django 1.9.6. After creating the project, set Chinese display support in settings.py (the simplest one, such as changing the default English of the admin interface to Chinese), define

TIME_ZONE = 'Asia/Shanghai'
LANGUAGE_CODE = 'zh-cn'
Copy after login

Of course TIME_ZONE only specifies the time zone and has nothing to do with Chinese display. The default value is modified here. As a result, an error occurred when running the project, as follows:

django IOError: No translation files found for default language zh-cn
Copy after login

That is, the localization file corresponding to zh-cn could not be found. Generally speaking, the localization folder corresponding to the zh-cn encoding is zh_CN. I went to the django installation directory to search for zh_CN. The result was indeed not found. It was in the locale folder of a series of subdirectories under the contrib subdirectory of the django installation directory. , there is no zh_CN, but zh_Hans. I don’t know why this version of django doesn’t have the zh_CN folder, maybe it’s a bug. Someone on the Internet said that this problem can be solved by copying the entire zh_Hans directory and naming it zh_CN. I tried it and it was indeed feasible to run it again. Chinese was displayed on the admin interface. As for whether there are any hidden dangers in this relatively copycat method, I don’t know yet. We’ll talk about it later when we encounter it.

But having said that, there is actually an orthodox way to generate internationalized files, the following command:

django-admin.py makemessages -l zh_CN
Copy after login

But this requires the gettext component. If you are developing on Windows, then you need to do it yourself Install this component,

Download the latest versions of gettext-runtime-X.zip and gettext-tools-X.zip at http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ (Note: X is the version number) After decompression, the two bin directories can be merged and put into the system environment variables. However, I have not verified this method. If you are interested, you can try it.

Later I thought again, the old version of django had zh_CN. To say that this is a bug, it is probably relatively low-level and shouldn't be the case. So I checked again and found that zh_Hans may have replaced zh_CN. Han means Chinese characters, s means Simplified. Anyway, I set it to LANGUAGE_CODE = 'zh-hans' and it displays Chinese normally.

zh-cn will expired in Django1.9, the new format is zh-hans, but drf3.1.0 does not support this type of language.

The above means that in django1.9 , zh-cn has expired and can be replaced by zh-hans, but some third-party modules of Django may not have caught up with this change.

In short, benevolent people have different opinions. In fact, there is no need to study it like this. I just suddenly became more interested in this, so I studied it for a while.

ps: There are various ways to write LANGUAGE_CODE = 'zh-cn' in many places on the Internet, including zh_CN, zh-CN, etc. Some people also say that you can only use such and such writing methods, etc. Anyway, I don't have it. After seeing the difference, I tried everything and found no problems.

As an aside, I never knew what USE_I18N and USE_L10N in settings.py meant until I saw the following paragraph:

After the Web service is set up, Can receive visits from users from different countries around the world. This requires developers to adapt the software to different languages, that is, internationalization and localization. Internationalization

--Internationalization, there are 18 letters between i and n, referred to as I18N,. Localization -- localization, there are 10 letters between l and n, abbreviated as L10N. Internationalization means that Web

products have the potential to be applied anywhere, for program developers; localization refers to the process of actually translating an international program for use in a specific region, for program developers For translators.

The above is the detailed content of Django supports Chinese setting methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template