skip to Main Content

when i test it locally the translations do worked, but when I deploy it on a server it doesn’t work.
Some of the words do get translated but it’s not from my locale, it’s django’s default translation.

In settings.py:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

LANGUAGE_CODE = 'en-us'
# LANGUAGE_CODE = 'zh-hans'  # i also tried just putting language code as zh-hans but still ntg gets translated.

TIME_ZONE = 'Asia/Kuala_Lumpur'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

LANGUAGES = [
    ('en', 'English'),
    ('zh-hans', '简体中文 zh-hans'),
]

in urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django_email_verification import urls as mail_urls
from rest_framework.authtoken import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('email/', include(mail_urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += i18n_patterns(
    path('i18n/', include('django.conf.urls.i18n')),
    path('', include('main.urls')),
    prefix_default_language=False,
)

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


For the LOCALE_PATHS it is correct as i put print(LOCALE_PATHS) in the settings and it gave me the right path to my locale "home/username/django_project/locale"

I did do python manage.py makemessages -l zh_hans as well as python manage.py compilemessages
and restarted my server but still the same.

Btw I’m using AWS ubuntu and apache2. (Not apache problem as i tried python manage.py runserver 0.0.0.0:8000 before i even installed apache and it’s still the same)

My dev comp is windows and my server is ubuntu (not sure how it may help but yea)

and there is no error at all there’s just no translation.

what could be the problem ? can anyone help me out?

2

Answers


  1. It’s most likely that Django cannot read your translation file correctly.

    What is the folder name in your locales folder?
    It seems like Ubuntu prefers capitalized locale name, such as zh_Hans or zh_HANS. Try changing the folder name so that Ubuntu can read it correctly.

    Please note that when you’re changing folder name, you should change it to something else first, because sometimes Ubuntu will think changing from lowercase to uppercase isn’t really changing anything. So do an intermediate step like this:

    cd locales
    mv zh_hans/ zh/
    mv zh/ zh_Hans/
    

    Other ways to debug what’s the problem

    You can also use django.utils.translation.get_language() to print out if it’s detecting the correct current language code. So if the above line prints out zh-hans but you still not see your translation being loaded, it’s probably something to do with either locale paths, or the file not being loaded by Django.

    You can also log in to admin site and see if the default translation is loaded.

    Login or Signup to reply.
  2. For me, my translation worked, but 3rd not.

    So I have to rename zh_hans folder to zh_Hans in 3rd-library in site-packages.

    Because Linux is case-sensitive, and Django ignored zh_hans

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search