最近在django项目中使用django-filer管理后台文件,就算我把django的setting中额语言设置成为了LANGUAGE_CODE = 'zh-Hans',页面仍然汉化无效。我在django-filer的依赖安装目录页发现了zh-Hans的翻译包。
根据博客https://stackoverflow.com/questions/12101811/trans-string-not-working-on-templates-but-trans-variable-does/12223367我找到了django拉取翻译包的代码。
def to_locale(language):
"""Turn a language name (en-us) into a locale name (en_US)."""
language, _, country = language.lower().partition('-')
if not country:
return language
# A language with > 2 characters after the dash only has its first
# character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
# A language with 2 characters after the dash has both characters
# capitalized; e.g. en-us becomes en_US.
country, _, tail = country.partition('-')
country = country.title() if len(country) > 2 else country.upper()
if tail:
country += '-' + tail
return language + '_' + country
实际上,LANGUAGE_CODE = 'zh-Hans',拉取的是zh_Hans包里面的翻译内容,但是django-filer的翻译包的中文目录名称是zh-Hans,修改名称后果然解决问题