Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Becoming a MutltiLingual SuperHero in Django

Becoming a MutltiLingual SuperHero in Django

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.

B3b24db20c76e4a24941c0382718dd43?s=128

Sanyam Khurana

August 24, 2018
Tweet

Transcript

  1. Becoming a Multilingual Super Hero in Django Sanyam Khurana CuriousLearner

    ErSanyamKhurana SanyamKhurana.com
  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. Retrieving Language Preference from Client's Request

  9. 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
  10. None
  11. None
  12. GOTCHA #1 The LocaleMiddleware should always come before CommonMiddleware and

    after SessionMiddleware
  13. What does LocaleMiddleware help with?

  14. • 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
  15. What To Translate?

  16. Static String Translations

  17. Templates

  18. Templates

  19. Templates

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

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

  22. • 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
  23. • 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
  24. GOTCHA #3 Use ugettext when you need immediate evaluation, else,

    always prefer ugettext_lazy
  25. Generating Translation Files For Static Strings

  26. None
  27. None
  28. None
  29. None
  30. GOTCHA #4 Use makemessages -i venv to exclude generating django.po

    files for every package in env
  31. GOTCHA #5 In Django settings 'zh-cn' In makemessages command 'zh_CN'

  32. Compiling Static String Translations

  33. None
  34. GOTCHA #6 compilemessages command would generate django.mo files for every

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

    work
  36. Dynamic String Translations

  37. Model Fields

  38. None
  39. None
  40. BONUS

  41. GOTCHA #8 Check in django shell if the translations are

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

    line that marks them as fuzzy
  44. CONCLUSION

  45. Questions Sanyam@SanyamKhurana.com CodeMentor.io/CuriousLearner CuriousLearner @ErSanyamKhurana