implementing_language_buttons.rst 3.5 KB

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