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.
Related
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",]
I'm trying to make the local development work with everything under a /abc.
For example, instead of http://localhost:8080 I want everything to be at something like http://localhost:8080/abcd
I was able to achieve this by updating the url.py with the following:
urlpatterns = patterns('',
url(r'^abcd/admin/', include(admin.site.urls), name='admin'),
url(r'^abcd/search/?$', views.search, name='search'),
url(r'^abcd/$', views.index, name='root')
)
# Force Django to serve static assets, if this is not the production
if settings.PRODUCTION is False:
urlpatterns += patterns('', (
r'^abcd/static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT
}
))
Now I can view the pages with http://localhost:8080/abcd but the rendering does not show up correctly because of the static files. I need the static files to be served at http://localhost:8080/abcd/static/ and for it to work with all the pages.
Is there a simple way to make it work? I tried doing the above and it doesn't seem to work. I'm super new to django so I don't fully understand how I can achieve what I'm trying to do. Any help from experts would be much appreciated.
It should work.
urlpatterns = patterns('',
url(r'^abcd/', views.index, name='root')
url(r'^abcd/admin/', include(admin.site.urls), name='admin'),
url(r'^abcd/search/?$', views.search, name='search')
)
# Force Django to serve static assets, if this is not the production
if settings.PRODUCTION is False:
urlpatterns += patterns('', (
r'^abcd/static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT
}
))
change settings.py
STATIC_URL = '/abcd/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
I have a Django Website that I am trying to internationalize. Until now it looked like this:
Homepage:
www.myhomepage.com
Another page:
www.myhomepage.com/content/cities
Now I am trying to make it like this:
Homepage:
www.myhomepage.com/en
www.myhomepage.com/de
Another page:
www.myhomepage.com/en/content/cities
www.myhomepage.com/de/content/cities
Following this and this, I managed to make the homepage work, so with www.myhomepage.com/en I see the homepage in English and with www.myhomepage.com/de I see it in German.
The problem comes when I want to go to any other page, like www.myhomepage.com/en/content/cities. Then, the page rendered is still the homepage. Before changing any settings to internationalize it www.myhomepage.com/content/cities was showing up properly.
My guess is that the problem is with the view rendering or the url, but I do not manage to make it work.
Note that the view for www.myhomepage.com belongs to one app and the view for content/cities belongs to a different app.
This is the code I have:
settings.py
MIDDLEWARE_CLASSES = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.i18n',
],
},
},
]
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
USE_I18N = True
Main app:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += i18n_patterns('',
url(r'^content/', include('content.urls', namespace='content')),
)
views.py
from django.shortcuts import render
def home_view(request):
...
context = {
...
}
#print('request home', request)
return render(request, 'home_template.html', context)
By activating the print statement and loading www.myhomepage.com/en/content/cities, the following is printed in the console: request home: <WSGIRequest: GET '/en/content/cities/'>, even though this view belongs to the home_page.
Content app:
urls.py
from .views import countries_and_cities
urlpatterns = [
...
url(r'^cities/$', countries_and_cities),
...
]
views.py
from django.shortcuts import render
def countries_and_cities(request):
...
context = {
...
}
return render(request, 'cities_template.html', context)
I have also tried what it is suggested in the docs, without success.
urls.py from main app:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
from content import views as content_views
content_patterns = ([
url(r'^cities/$', content_views.countries_and_cities, name='cities'),
], 'content')
urlpatterns += i18n_patterns('',
url(r'^content/', include(content_patterns, namespace='content')),
)
What am I doing wrong?
I finally found the problem.
There was another url defined as:
urlpatterns += i18n_patterns(
...
url(r'', include('main.urls')),
...
)
This was causing the problem, even if before going for internationalization it was working properly.
I just changed it to:
urlpatterns += i18n_patterns(
...
url(r'^$', include('main.urls')),
...
)
And it is working properly.
I want to add a language prefix in URL patterns just like the django documentation homepage. Following this example my urls.py looks like this:
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from myapp import views
from myapp.views import MyFirstView, MySecondView
myapp_patterns = [
url(r'^$', views.CategoryView, name='index'),
url(r'^(?P<name>[a-zA-Z0-9_]+)/$', MyFirstView.as_view(), name='detail'),
url(r'^(?P<name>[a-zA-Z0-9_]+)/mysecond_view/$', MySecondView, name='mysecond_view')
]
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^categories/', include(myapp_patterns)),
url(r'^', views.LandingView),
]
This works but now when I add += i18n_patterns
urlpatterns += i18n_patterns [
url(r'^admin/', admin.site.urls),
url(r'^categories/', include(myapp_patterns)),
url(r'^', views.LandingView),
]
I get the error: NameError: name 'urlpatterns' is not defined
I did add the LocalMiddleware:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
as well as this:
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
I don't understand how urlpatterns all of the sudden is not defined anymore.
What am I doing wrong here?
Primarily because of +=. There's no previous definition of urlpatterns.
You should start of with assignment = to define it.
All the pages throw a 404 error on a site for users who are not logged in. But if I log in to the admin and go back to view the site, all the pages are fine and viewable.
I've been using Django CMS for years and haven't come across this before. The only difference with this site is the default language is french, in my settings I have:
LANGUAGES = [
('fr', 'Francais'),
]
as my LANGUAGES setting and here is my LANGUAGE_CODE
LANGUAGE_CODE = 'fr'
Here are my urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
and my middleware...
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.multilingual.MultilingualURLMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
)
What could be the cause of this?
I was in a similar situation to you: I'm using multiple languages with Django-CMS.
My issue was related to the CMS_LANGUAGES variable I'd defined: I'd simply lifted a portion of the example from the docs.
A comment on a GitHub issue helped point me in the correct direction.
I'd previously had the variable set up as:
CMS_LANGUAGES = {
...
'default': {
'fallbacks': ['en', 'de', 'fr'],
'redirect_on_fallback': False,
'public': False,
'hide_untranslated': False,
}
}
Notice the definition of the public boolean.
Also, make sure that you follow the instructions in the documentation with respect to setting up your language variables within the CMS_LANGUAGES dictionary.
Replacing the above with 'public': True got things working for me again.
Just add a plus sign :)
if settings.DEBUG:
urlpatterns += patterns('',