Becoming a MutltiLingual SuperHero in Django v2.0

Becoming a MutltiLingual SuperHero in Django v2.0

In this talk, we’ll see how we make a language agnostic backend, to serve our app in different languages, based on what language the client wants to communicate in.

We’ll see how to support translation for static data and dynamic data, using various third-party services.

We'll also learn to retain language context in Async tasks ;)

B3b24db20c76e4a24941c0382718dd43?s=128

Sanyam Khurana

October 15, 2018
Tweet

Transcript

  1. Becoming a Multilingual Super Hero in Django (v2.0) Sanyam Khurana

    CuriousLearner ErSanyamKhurana SanyamKhurana.com DjangoCon US 2018
  2. def __who_am_i__()

  3. 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
  4. The Road to become a Multilingual SuperHero in Django

  5. Preparing for Internationalization & Localization

  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. Retrieving Language Preference from Client's Request

  15. 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
  16. None
  17. None
  18. GOTCHA #1 The LocaleMiddleware should always come before CommonMiddleware and

    after SessionMiddleware
  19. What does LocaleMiddleware help with?

  20. • 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
  21. What To Translate?

  22. Static String Translations

  23. Templates

  24. Templates

  25. Templates

  26. Templates

  27. Templates

  28. GOTCHA #2 i18n tag should be loaded in every file,

    even if it extends another file that already has it loaded.
  29. All *.py Files

  30. • 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
  31. None
  32. • 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
  33. None
  34. None
  35. GOTCHA #3 Use ugettext when you need immediate evaluation, else,

    always prefer ugettext_lazy
  36. Generating Translation Files For Static Strings

  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. GOTCHA #4 Use makemessages -i venv to exclude generating django.po

    files for every package in env
  46. GOTCHA #5 Django settings uses language name 'zh-cn' makemessages command,

    use locale name 'zh_CN'
  47. Compiling Static String Translations

  48. None
  49. GOTCHA #6 compilemessages command would generate django.mo files for every

    package if your venv is in project directory
  50. GOTCHA #7 Restart wsgi server after compilemessages, otherwise translations won't

    work
  51. Dynamic String Translations

  52. Model Fields

  53. None
  54. None
  55. BONUS

  56. Passing Context To Async Tasks

  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. Switching Language In Templates

  64. None
  65. None
  66. None
  67. Supporting multiple languages in templates

  68. None
  69. None
  70. None
  71. None
  72. None
  73. GOTCHA #8 Check in django shell if the translations are

    working with activate and ugettext
  74. None
  75. GOTCHA #9 Some strings are still not being translated Remove

    line that marks them as fuzzy
  76. CONCLUSION

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

  78. Questions medium.com/@CuriousLearner Sanyam@SanyamKhurana.com CodeMentor.io/CuriousLearner CuriousLearner @ErSanyamKhurana SanyamKhurana.com/python.html