implementing_language_buttons.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. -----------------------------
  2. Implementing language buttons
  3. -----------------------------
  4. Each article with translations has translations links, but that's the only way to switch between language subsites.
  5. For this reason it is convenient to add language buttons to the top menu bar to make it simple to switch between the language subsites on all pages.
  6. Example designs
  7. ---------------
  8. Language buttons showing other available languages
  9. ..................................................
  10. The ``extra_siteurls`` dictionary is a mapping of all other (not the *DEFAULT_LANG* of the current (sub-)site) languages to the *SITEURL* of the respective (sub-)sites
  11. .. code-block:: jinja
  12. <!-- SNIP -->
  13. <nav><ul>
  14. {% if extra_siteurls %}
  15. {% for lang, url in extra_siteurls.items() %}
  16. <li><a href="{{ url }}">{{ lang }}</a></li>
  17. {% endfor %}
  18. <!-- separator -->
  19. <li style="background-color: white; padding: 5px;">&nbsp</li>
  20. {% endif %}
  21. {% for title, link in MENUITEMS %}
  22. <!-- SNIP -->
  23. Language buttons showing all available languages, current is active
  24. ..................................................................
  25. The ``extra_siteurls`` dictionary is a mapping of all languages to the *SITEURL* of the respective (sub-)sites. This template sets the language of the current (sub-)site as active.
  26. .. code-block:: jinja
  27. <!-- SNIP -->
  28. <nav><ul>
  29. {% if lang_siteurls %}
  30. {% for lang, url in lang_siteurls.items() %}
  31. <li{% if lang == DEFAULT_LANG %} class="active"{% endif %}><a href="{{ url }}">{{ lang }}</a></li>
  32. {% endfor %}
  33. <!-- separator -->
  34. <li style="background-color: white; padding: 5px;">&nbsp</li>
  35. {% endif %}
  36. {% for title, link in MENUITEMS %}
  37. <!-- SNIP -->
  38. Tips and tricks
  39. ---------------
  40. Showing more than language codes
  41. ................................
  42. If you want the language buttons to show e.g. the names of the languages or flags [#flags]_, not just the language codes, you can use a jinja filter to translate the language codes
  43. .. code-block:: python
  44. languages_lookup = {
  45. 'en': 'English',
  46. 'cz': 'Čeština',
  47. }
  48. def lookup_lang_name(lang_code):
  49. return languages_lookup[lang_code]
  50. JINJA_FILTERS = {
  51. ...
  52. 'lookup_lang_name': lookup_lang_name,
  53. }
  54. And then the link content becomes
  55. .. code-block:: jinja
  56. <!-- SNIP -->
  57. {% for lang, siteurl in lang_siteurls.items() %}
  58. <li{% if lang == DEFAULT_LANG %} class="active"{% endif %}><a href="{{ siteurl }}">{{ lang | lookup_lang_name }}</a></li>
  59. {% endfor %}
  60. <!-- SNIP -->
  61. Changing the order of language buttons
  62. ......................................
  63. Because ``lang_siteurls`` and ``extra_siteurls`` are instances of ``OrderedDict`` with ``main_lang`` being always the first key, you can change the order through a jinja filter.
  64. .. code-block:: python
  65. def my_ordered_items(ordered_dict):
  66. items = list(ordered_dict.items())
  67. # swap first and last using tuple unpacking
  68. items[0], items[-1] = items[-1], items[0]
  69. return items
  70. JINJA_FILTERS = {
  71. ...
  72. 'my_ordered_items': my_ordered_items,
  73. }
  74. And then the ``for`` loop line in the template becomes
  75. .. code-block:: jinja
  76. <!-- SNIP -->
  77. {% for lang, siteurl in lang_siteurls | my_ordered_items %}
  78. <!-- SNIP -->
  79. .. [#flags] Although it may look nice, `w3 discourages it <http://www.w3.org/TR/i18n-html-tech-lang/#ri20040808.173208643>`_.