Django

Django Internationalization

i18n, gettext, translation strings, locale middleware, language detection, translation files

20 питань зі співбесід·
Senior
1

Which function should be used to mark a string as translatable in Django?

Відповідь

The gettext function, commonly imported with the _ (underscore) alias, is the standard method for marking translatable strings in Django. It looks up the translation of the string in the message catalog corresponding to the active language and returns the translated version if available.

2

What is the difference between gettext() and gettext_lazy() in Django?

Відповідь

gettext_lazy() returns a proxy object that is only evaluated when converted to a string, which is essential for translations defined at module level (like in models or forms). gettext() immediately evaluates the translation at call time, which is problematic if the language isn't determined yet.

3

Which Django middleware is responsible for activating the language for each request?

Відповідь

LocaleMiddleware examines the request to determine the user's preferred language by checking multiple sources: URL prefix, cookies, browser's Accept-Language header, and the LANGUAGE_CODE setting. It then activates the corresponding language for the entire request duration.

4

Which Django command extracts translatable strings from the source code?

5

Which command compiles .po translation files into binary .mo files?

+17 питань зі співбесід

Опануй Django для наступної співбесіди

Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.

Почни безкоштовно