I'm trying to add multi language support for a django project using Django i18n official documentaion:
https://docs.djangoproject.com/en/4.1/topics/i18n/translation/
When I change the LANGUAGE_CODE to something like 'fa', by default the admin panel changes to RTL.
But the problem is when I use other RTL languages like 'ku' (kurdish) the page remains in ltr.
I know we can change the css manualy, but wonder what is the problem here and how some languages like Arabic or persian does the RTL part automaticaly but others dont.
Thanks in advance
# settings.py
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
TIME_ZONE = 'UTC'
USE_TZ = True
LANGUAGES = (
('en', _('English')),
('ku', _('Kurdish')),
('fa', _('Persian')),
)
LOCALE_PATHS = [
Path(BASE_DIR, 'django_i18n', 'locale'),
]
# url.py
urlpatterns = i18n_patterns(
path('admin/', admin.site.urls),
) +static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
just found the answer here:
https://github.com/django/django/blob/main/django/conf/global_settings.py#L158
So all I had to do was add this to setting.py:
LANGUAGES_BIDI = ["ku",]
There are two working versions of the translation on the site - Russian and English using i18n. How to make sure that everyone has a website loaded in Russian and then, at the request of the user, he can change it to English?
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
LANGUAGE_CODE = 'ru'
LANGUAGES = (
('ru', _('Russian')),
('en', _('English')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_L10N = True
USE_TZ = True
I'm using Django 1.5
I have to enable internationalization in my application. For that, I have added a few things to the settings.py file
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'...local context processrors...'
)
# global_settings.TEMPLATE_CONTEXT_PROCESSORS contains
# TEMPLATE_CONTEXT_PROCESSORS = (
# 'django.contrib.auth.context_processors.auth',
# 'django.core.context_processors.debug',
# 'django.core.context_processors.i18n',
# ...
# )
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'es'
# List of languages available for translation
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('es', ugettext('Spanish'))
)
LOCALE_PATHS = (
os.path.join(PROJECT_ROOT, 'locale/'),
)
The LOCALE_PATHS has the location output as
('/media/path_to_project/workbench/workbench/settings/../locale/',)
But on running ./manage.py makemessages -l es it generates *.po file in
/media/path_to_project/workbench/workbench
instead of
/media/path_to_project/workbench/workbench/locale
Also, the compiled language is not showing in the template.
You need to check following things.
Ensure that you added USE_L10N = True and USE_I18N = True into your settings.py file
Check for PROJECT_ROOT and LOCALE_PATHS and make sure that it is correct
Add django.core.context_processors.i18n to TEMPLATE_CONTEXT_PROCESSORS in setting.py.
Translation and formatting are controlled by USE_I18N and USE_L10N settings respectively. However, both features involve internationalization and localization. The names of the settings are an unfortunate result of Django’s history.
I'm having a problem with Django loading the correct translation (.po/.mo) files.
I know my translations are working because when I change the LANGUAGE_CODE to 'fr' I can see my string translated.
LANGUAGE_CODE = 'nl'
LANGUAGES = (
('nl', 'Nederlands'),
('fr', 'Frans'),
)
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
TIME_ZONE = 'UTC'
USE_I18N = True
But when I set my translations to use the i18n_patterns in urls.py it is not loading my French (fr) translations when I visit: 127.0.0.1:8000/fr/about/ it still loads the Dutch (nl) values.
Funny thing is when I visit 127.0.0.1:8000/fr/admin/ I can see it loads the French admin values and when I visit 127.0.0.1:8000/nl/admin/ it loads the Dutch ones... I must be doing something wrong.
urls.py
urlpatterns = i18n_patterns(
url(r'^$', 'homepage.views.index', name="homepage"),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^contact/', 'homepage.views.contact_us', name="contact"),
url(r'^about/', 'homepage.views.about', name="about"),
url(r'^admin/', include(admin.site.urls)),
url(r'^jobs/', include(job_urls)),
url(r'^news/', include(news_urls)),
url(r'^search/', include('haystack.urls')),
url(r'^content/(?P<slug>[^\.]+)', 'homepage.views.content', name="view_content"),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You need to import ugettext_lazy as _ in your models.py and forms.py, instead of the regular ugettext. ugettext will evaluate the translations during import, at which point it has no other choice than to use the default language; ugettext_lazy will evaluate when rendering, at which point it has the correct language information available.
I have static.serve setup on my local development server, but it seems to cache static files (in my case, css, javascript and images) until I restart the the server. I am not using apache, and I have the cache set to:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
Removing the caches declaration all together doesn't seem to help either.
This didn't happen before I upgraded to 1.2.5 from an older 1.1 version.
It's a pain to have to restart the dev server every time (either by touching a python file or via the command line) every time I make a style update.
Edit - as suggested, I've added settings.py and url.py
Settings.py
# Django settings for zeiss_elearning project.
from django.utils.translation import ugettext_lazy as _
gettext = lambda s: s
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
('Jason Roy', '###'),
)
#Email settings
EMAIL_HOST = '###'
EMAIL_HOST_USER = 'info#btbcreative.com'
EMAIL_HOST_PASSWORD = '####'
DEFAULT_FROM_EMAIL = 'info#btbcreative.com'
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.mysql',
'NAME' : '###',
'USER' : '###',
'PASSWORD' : '###',
'HOST' : '/Applications/MAMP/tmp/mysql/mysql.sock',
}
}
TIME_ZONE = 'America/Tijuana'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
MEDIA_DEBUG_DOC_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'
MEDIA_ROOT = '/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/media'
MEDIA_URL = '/static_files/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '####'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.media.PlaceholderMediaMiddleware',
#'django.middleware.cache.FetchFromCacheMiddleware',
#'debug_toolbar.middleware.DebugToolbarMiddleware',
)
ROOT_URLCONF = 'zeiss_elearning.urls'
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'cms.context_processors.media',
)
TEMPLATE_DIRS = (,
'/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/templates',
'/Users/jason/Bird Takes Bear/Projects/Carl Zeiss/site 2.0/cms/templates',
)
SESSION_COOKIE_AGE = 86400
LOGIN_URL = '/membership/login/'
LOGIN_REDIRECT_URL = "/"
AUTHENTICATION_BACKENDS = (
'zeiss_elearning.shared.email_auth.EmailBackend',
'django.contrib.auth.backends.ModelBackend',
)
AUTH_PROFILE_MODULE = 'membership.UserProfile'
FORCE_SCRIPT_NAME = ''
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'cms',
'cms.plugins.text',
'cms.plugins.picture',
'cms.plugins.link',
'cms.plugins.file',
'cms.plugins.snippet',
'cms.plugins.googlemap',
'cms.plugins.zeiss_video',
'cms.plugins.html',
'cms.plugins.quiz',
'cms.plugins.popup',
'mptt',
'publisher',
'zeiss_elearning.forms',
'zeiss_elearning.membership',
'zeiss_elearning.quiz',
'menus',
'south',
)
INTERNAL_IPS = ('127.0.0.1',)
#CMS Settings
CMS_REDIRECTS = True
CMS_MENU_TITLE_OVERWRITE = True
CMS_DBGETTEXT = False
CMS_DEFAULT_TEMPLATE = 'base.html'
CMS_ALLOW_HTML_TITLES = False
CMS_TEMPLATES = (
('base.html', _('Default')),
('cirrus.html', _('Cirrus')),
('atlas.html', _('Atlas')),
)
# Site title for your template
CMS_SITE_TITLE = 'Zeiss Cirrus'
CMS_LANGUAGE_REDIRECT = False
CMS_LANGUAGES = (
('en', gettext('English')),
)
LANGUAGES = (
('en', gettext('English')),
)
CMS_APPLICATIONS_URLS = (
('zeiss_elearning.quiz.urls', 'Quiz')
)
urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
(r'^membership/', include('zeiss_elearning.membership.urls')),
(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('',
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static_files/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_DEBUG_DOC_ROOT}),)
The bottom line here, based on the data provided, seems to be that your browser is caching the media files. The recommended method to resolve this is super refreshing the pages in your browser. See ALL the comments on your post.
However, If you really do not want the media files to be cached you can simply set them constantly unique names. Like so.
<link rel="stylesheet" type="text/css" href="/site_media/css/style.css?{% now "U" %}" />
Now every time the page is reloaded the filename will be a little bit different based on the unix timestamp, forcing the browser to reload it all the time.