variable inside blocktranslate not translated when makemessages - django

I make a simple template like this
<button class="btn">{% blocktranslate %}{{ greeting }} Quy!{% endblocktranslate %}</button>
with greeting equals hello.
I have added the following MIDDLEWARE, and LANGUAGE_CODE = 'vi'. Everything else is left as default.
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
and
LANGUAGE_CODE = "vi"
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LANGUAGES = [
('vi', _('Vietnamese')),
('en', _('English')),
]
After running django-admin makemessages -l vi, the .po file is created
msgid "%(greeting)s Quy!"
msgstr ""
I added
# vi/LC_MESSAGES/django.po
msgid "hello"
msgstr "chào"
or
# en/LC_MESSAGES/django.po
msgid "hello"
msgstr "hi"
and then run django-admin compilemessages but "hello" shown up instead of "chào" or "hi". Why I cannot translate the variable

I tries it with tag translate, and it works if I send variable greeting as "hello", whereas for blocktranslate I need _("hello")

Related

Django command makemessages does not generate string marked for translations for files in templates directory

I have project in Django==2.2.12
and this is part of my settings:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(BASE_DIR, "templates")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.static",
],
},
},
]
LANGUAGE_CODE = "pl"
TIME_ZONE = "Europe/Warsaw"
USE_I18N = True
USE_L10N = True
USE_TZ = False
LANGUAGES = (
('pl', gettext('Polish')),
('en', gettext('English')),
)
When I execute:
django-admin makemessages --locale=en
It generates .po files with string to translate in locale directory from .py files but it completly skips .txt files that are located in my templates directory. For example it does not generate string for translation for my text.txt file with following content:
{% load i18n %}
{% blocktranslate %}
string to translate
{% endblocktranslate %}
Make sure to use {%trans 'your string %}
Use this command to update .po files: python manage.py makemessages -l ar --no-wrap --no-location
Remove #fuzzy if it's above what you want to translate
Run python manage.py compilemessages
I hope this helps!

Django unable to translate language

I want to translate my website to CH, but no response after I followed the online instruction. Did I miss something?
I have changed the language code in settings.py.
settings.py:
import os
import django_heroku
from django.utils.translation import gettext_lazy as _
LANGUAGES = (
('en', ('English')),
('zh-Hant', _('Traditional Chinese')),
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMP_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
....]}
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hant'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale')
]
Added the tag for translation in html
base.html:
{% load i18n %}
<!DOCTYPE html>
<html>
....
home.html:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% trans "Let's translate this" %}
....
Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:
msgid "Let's translate this"
msgstr "來翻譯這個"
Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:
For traditional Chinese:
zh-hant in your config, and your directory should be named zh_Hant.
See the locale directory: https://github.com/django/django/tree/master/django/contrib/auth/locale
locale name
A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: it, de_AT, es, pt_BR, sr_Latn. The language part is always in lowercase. The country part is in titlecase if it has more than 2 characters, otherwise it’s in uppercase. The separator is an underscore.
and languages keys with lowercase:
LANGUAGES = (
('en', _('English')),
('zh-hant', _('Traditional Chinese')),
)
Change your language code so that you have LANGUAGES like this;
LANGUAGES = (
('en', gettext('English')),
('ja', gettext('Japanese')),
('it', gettext('Italian')),
('zh-hant', gettext('Chinese')),
)
I've tested this on a multi-lingual site that I have.
python manage.py makemessages -l zh-hant
python manage.py compilemessages
Django admin then looks like this by default;

How to set the default language for all users or browsers

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

How to translate django-allauth templates?

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)

Django internationalization not working

My django is not executing translations that i define in my *.po files.
I have my setup like this:
MainFolder:
locale
en
LC_MESSAGES
django.po
django.mo
de
LC_MESSAGES
django.po
django.mo
app1
settings.py
app2
....
in my settings.py i have everything enabled i think:
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
TEMPLATE_CONTEXT_PROCESSORS = (
....
'django.core.context_processors.static',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
)
let's say my django.po file contains an example like this:
msgid "Home"
msgstr "Home22222"
and i use this in my template file:
{% trans "Home" %}
The home is not being replace by Home22222
It stays the same, even when i remove the LocaleMiddleware.
i did ran
makemessages --locale=en
and afterwards:
compilemessages
I am using Python 2.7 and django 1.6.4