开发者

Django custom locale directory

开发者 https://www.devze.com 2022-12-24 20:32 出处:网络
I\'m developing a project with two different sites, divided by language. Maybe I was terribly wrong, but now my directory structure

I'm developing a project with two different sites, divided by language. Maybe I was terribly wrong, but now my directory structure looks like:

/ruapp/settings.py # SITE_ID = 1 
/ruapp/man开发者_如何学JAVAage.py 
/enapp/settings.py # SITE_ID = 2 
/enapp/manage.py 
/common/urls.py 
/common/ # almost every other file 
/common/templates/ # templates with {% trans %} 
/locale/ # with locales ru-ru and en-us, generated by calling 
makemessages from the root of all this structure

How to tell django about the locale? It does not seem like it will find the /locale/ folder by itself


AFAIK, python-gettext can't use different folder, so... Use Symlinks, Luke!


Make sure your directory structure for localization files look like that:

locale/en/LC_MESSAGES/django.mo 
locale/ru/LC_MESSAGES/django.mo

If you name them something else, or put them elsewhere, it probably won't work.

You also need to pust aproperiate l18n information in your settings file. LANGUAGE_CODE = 'en' in one, and LANGUAGE_CODE = 'ru' in the other.

Edit: Have you created two separate apps or two separate project? Apps usually don't have their own settings.py and manage.py...

You can have one project that have multiple settings files and runs multiple websites. It would be much more django-ish (and much easier to deal with) for your directory structure to look like this:

/settings_ru.py # SITE_ID = 1 
/settings_en.py # SITE_ID = 2 
/manage.py # use --settings option to distinguish between settings files.
/urls.py 
/templates/ # templates with {% trans %} 
/locale/ # locale/en/LC_MESSAGES/django.mo and locale/ru/LC_MESSAGES/django.mo 
(+ other common code, inside their respective apps)
0

精彩评论

暂无评论...
验证码 换一张
取 消