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",]
Related
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've already translated my project in Malagasy (mg_Mg) but when installing django-allauth and override templates, translation won't be applied.
After python manage.py makemessages, python manage.py compilemessages and testing some translations eg: sign up -> Hisoratra anarana in render it show sign up.
But in my local app, all templates are translated correctly.
In config/setting.py
import environ
BASE_DIR = environ.Path(__file__) - 2
...
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = [
('en-us', 'English'),
('fr-fr', 'French'),
('mg-mg', 'Malagasy'),
]
LOCALE_PATHS = [BASE_DIR('locale')]
In fandaharana/views.py
def month(request, month_num, year_num):
translation.activate('mg-mg')
...
return render(request, 'month.html', data)
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 use django-modeltranslation it works good but i have problem defaul language not working, website loads of other languages
my settings.py
LANGUAGE_CODE = 'ka'
gettext = lambda s: s
LANGUAGES = [
('ka', gettext('Georgian')),
('en', gettext('English')),
('ru', gettext('Russian')),
]
MODELTRANSLATION_DEFAULT_LANGUAGE = 'ka'
MODELTRANSLATION_LANGUAGES = ('ka','en','ru')
MODELTRANSLATION_FALLBACK_LANGUAGES = ('ru','en')
USE_I18N = True
USE_L10N = True
urls.py config
urlpatterns = patterns('',
url(r'^app/', include('app.urls')),
# url(r'^i18n/', include('django.conf.urls.i18n')), # i tried this too
url(r'^set_language/$', 'django.views.i18n.set_language',{},name='set_language'),
)
how to fix this problem? what is the solution?