Slide 1

Slide 1 text

Becoming a Multilingual Super Hero in Django Sanyam Khurana CuriousLearner ErSanyamKhurana SanyamKhurana.com

Slide 2

Slide 2 text

def __who_am_i__()

Slide 3

Slide 3 text

def __who_am_i__(): • One of you! A part of the community • CPython contributor and bug triage access on bugs.python.org • Contributor to Gecko Engine, add-ons, Marionette, TaskCluster • GSoC 2018 Mentor for Debian • RGSoC 2016 Mentor

Slide 4

Slide 4 text

The Road to become a Multilingual SuperHero in Django

Slide 5

Slide 5 text

Preparing for Internationalization & Localization

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Retrieving Language Preference from Client's Request

Slide 9

Slide 9 text

Selecting Language in the order of preference • Language prefix in requested URL (Eg: /en/api/resource) • LANGUAGE_SESSION_KEY key in current user's session • django_language cookie determined by LANGUAGE_COOKIE_NAME • Accept-Language header • Fallback to LANGUAGE_CODE in settings

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

GOTCHA #1 The LocaleMiddleware should always come before CommonMiddleware and after SessionMiddleware

Slide 13

Slide 13 text

What does LocaleMiddleware help with?

Slide 14

Slide 14 text

• Language prefix in requested URL • LANGUAGE_SESSION_KEY key in current user's session • django_language cookie determined by LANGUAGE_COOKIE_NAME • Accept-Language header • LANGUAGE_CODE setting

Slide 15

Slide 15 text

What To Translate?

Slide 16

Slide 16 text

Static String Translations

Slide 17

Slide 17 text

Templates

Slide 18

Slide 18 text

Templates

Slide 19

Slide 19 text

Templates

Slide 20

Slide 20 text

GOTCHA #2 i18n tag should be loaded in every file, even if it extends another file that already has it loaded.

Slide 21

Slide 21 text

All *.py Files

Slide 22

Slide 22 text

• Language prefix in requested URL • LANGUAGE_SESSION_KEY key in current user's session • django_language cookie determined by LANGUAGE_COOKIE_NAME • Accept-Language header • LANGUAGE_CODE setting

Slide 23

Slide 23 text

• Language prefix in requested URL • LANGUAGE_SESSION_KEY key in current user's session • django_language cookie determined by LANGUAGE_COOKIE_NAME • Accept-Language header • LANGUAGE_CODE setting

Slide 24

Slide 24 text

GOTCHA #3 Use ugettext when you need immediate evaluation, else, always prefer ugettext_lazy

Slide 25

Slide 25 text

Generating Translation Files For Static Strings

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

GOTCHA #4 Use makemessages -i venv to exclude generating django.po files for every package in env

Slide 31

Slide 31 text

GOTCHA #5 In Django settings 'zh-cn' In makemessages command 'zh_CN'

Slide 32

Slide 32 text

Compiling Static String Translations

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

GOTCHA #6 compilemessages command would generate django.mo files for every package if your venv is in project directory

Slide 35

Slide 35 text

GOTCHA #7 Restart wsgi server after compilemessages, otherwise translations won't work

Slide 36

Slide 36 text

Dynamic String Translations

Slide 37

Slide 37 text

Model Fields

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

BONUS

Slide 41

Slide 41 text

GOTCHA #8 Check in django shell if the translations are working with activate and ugettext

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

GOTCHA #9 Some strings are still not being translated Remove line that marks them as fuzzy

Slide 44

Slide 44 text

CONCLUSION

Slide 45

Slide 45 text

Questions [email protected] CodeMentor.io/CuriousLearner CuriousLearner @ErSanyamKhurana