Search code examples
djangodjango-templatesinternationalization

variable inside blocktranslate not translated when makemessages


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


Solution

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