Slide 1

Slide 1 text

Becoming a Multilingual Super Hero in Django (v2.0) Sanyam Khurana CuriousLearner ErSanyamKhurana SanyamKhurana.com DjangoCon US 2018

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

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Retrieving Language Preference from Client's Request

Slide 15

Slide 15 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 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

What does LocaleMiddleware help with?

Slide 20

Slide 20 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 21

Slide 21 text

What To Translate?

Slide 22

Slide 22 text

Static String Translations

Slide 23

Slide 23 text

Templates

Slide 24

Slide 24 text

Templates

Slide 25

Slide 25 text

Templates

Slide 26

Slide 26 text

Templates

Slide 27

Slide 27 text

Templates

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

All *.py Files

Slide 30

Slide 30 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 31

Slide 31 text

No content

Slide 32

Slide 32 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 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Generating Translation Files For Static Strings

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

GOTCHA #5 Django settings uses language name 'zh-cn' makemessages command, use locale name 'zh_CN'

Slide 47

Slide 47 text

Compiling Static String Translations

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Dynamic String Translations

Slide 52

Slide 52 text

Model Fields

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

BONUS

Slide 56

Slide 56 text

Passing Context To Async Tasks

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Switching Language In Templates

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Supporting multiple languages in templates

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

CONCLUSION

Slide 77

Slide 77 text

Level up your Python Skills Subscribe for updates SanyamKhurana.com/python.html

Slide 78

Slide 78 text

Questions medium.com/@CuriousLearner [email protected] CodeMentor.io/CuriousLearner CuriousLearner @ErSanyamKhurana SanyamKhurana.com/python.html