|
| 1 | +.. _localizaation: |
| 2 | + |
| 3 | +Localization / Multi-lingual Support |
| 4 | +------------------------------------ |
| 5 | + |
| 6 | +pywb supports configuring different language locales and loading different language translations, and dynamically switching languages. |
| 7 | + |
| 8 | +pywb can extract all text from templates and generate CSV files for translation and convert them back into a binary format used for localization/internationalization. |
| 9 | + |
| 10 | +(pywb uses the `Babel library <http://babel.pocoo.org/en/latest/>`_ which extends the `standard Python i18n system <https://docs.python.org/3/library/gettext.html>`_) |
| 11 | + |
| 12 | +Locales to use are configured in the ``config.yaml``. |
| 13 | + |
| 14 | +The command-line ``wb-manager`` utility provides a way to manages locales for translation, including generatin extracted text, update translated text. |
| 15 | + |
| 16 | +Adding a Locale and Extracting Text |
| 17 | +=================================== |
| 18 | + |
| 19 | +To add a new locale for translation and automatically extract all text that needs to be translated, run:: |
| 20 | + |
| 21 | + wb-manager i18n extract <loc> |
| 22 | + |
| 23 | +The ``<loc>`` can be one or more supported two-letter locales or CLDR language codes. To list available codes, you can run ``pybabel --list-locales``. |
| 24 | + |
| 25 | +Localization data is placed in the ``i18n`` directory, and translatable strings can be found in ``i18n/translations/<locale>/LC_MESSAGES/messages.csv`` |
| 26 | + |
| 27 | +Each CSV file looks as follows, listing source string and an empty string for the translated version:: |
| 28 | + |
| 29 | + "location","source","target" |
| 30 | + "pywb/templates/banner.html:6","Live on","" |
| 31 | + "pywb/templates/banner.html:8","Calendar icon","" |
| 32 | + "pywb/templates/banner.html:9 pywb/templates/query.html:45","View All Captures","" |
| 33 | + "pywb/templates/banner.html:10 pywb/templates/header.html:4","Language:","" |
| 34 | + "pywb/templates/banner.html:11","Loading...","" |
| 35 | + ... |
| 36 | + |
| 37 | + |
| 38 | +This CSV can then be passed to translators to translate the text. |
| 39 | + |
| 40 | +(The extraction parameters arae configured to load data from ``pywb/templates/*.html`` in ``babel.ini``) |
| 41 | + |
| 42 | + |
| 43 | +For example, the following will generate translation strings for ``es`` and ``pt`` locales:: |
| 44 | + |
| 45 | + wb-manager i18n extract es pt |
| 46 | + |
| 47 | + |
| 48 | +The translatable text can then be found in ``i18n/translations/es/LC_MESSAGES/messages.csv`` and ``i18n/translations/pt/LC_MESSAGES/messages.csv``. |
| 49 | + |
| 50 | + |
| 51 | +The CSV files should be updated with a translation for each string in the target column. |
| 52 | + |
| 53 | +The extract commannd add any new strings without overwriting existing translations, so it is safe to run multiple times. |
| 54 | + |
| 55 | + |
| 56 | +Updating Locale Catalog |
| 57 | +======================= |
| 58 | + |
| 59 | +Once the text has been translated, and the CSV files updated, simply run:: |
| 60 | + |
| 61 | + wb-manager i18n update <loc> |
| 62 | + |
| 63 | +This will parse the CSVs and compile the translated string tables for use with pywb. |
| 64 | + |
| 65 | + |
| 66 | +Specifying locales in pywb |
| 67 | +========================== |
| 68 | + |
| 69 | +To enable the locales in pywb, add one or more locales can be added to the ``locales`` key in ``config.yaml``, ex:: |
| 70 | + |
| 71 | + locales: |
| 72 | + - en |
| 73 | + - es |
| 74 | + |
| 75 | +Single Language Default Locale |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +pywb can be configured with a default, single-language locale, by setting the ``default_locale`` property in ``config.yaml``:: |
| 79 | + |
| 80 | + |
| 81 | + default_locale: es |
| 82 | + locales: |
| 83 | + - es |
| 84 | + |
| 85 | + |
| 86 | +With this configuration, pywb will automatically use the ``es`` locale for all text strings in pywb pages. |
| 87 | + |
| 88 | +pywb will also set the ``<html lang="es">`` so that the browser will recognize the correct locale. |
| 89 | + |
| 90 | + |
| 91 | +Mutli-language Translations |
| 92 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +If more than one locale is specified, pywb will automatically show a language switching UI at the top of collection and search pages, with an option |
| 95 | +for each locale listed. To include English as an option, it should also be added as a locale (and no strings translated). For example:: |
| 96 | + |
| 97 | + locales: |
| 98 | + - en |
| 99 | + - es |
| 100 | + - pt |
| 101 | + |
| 102 | +will configure pywb to show a language switch option on all pages. |
| 103 | + |
| 104 | + |
| 105 | +Localized Collection Paths |
| 106 | +========================== |
| 107 | + |
| 108 | +When localization is enabled, pywb supports the locale prefix for accessing each collection with a localized language: |
| 109 | +If pywb has a collection ``my-web-archive``, then: |
| 110 | + |
| 111 | +* ``/my-web-archive/`` - loads UI with default language (set via ``default_locale``) |
| 112 | +* ``/en/my-web-archive/`` - loads UI with ``en`` locale |
| 113 | +* ``/es/my-web-archive/`` - loads UI with ``es`` locale |
| 114 | +* ``/pt/my-web-archive/`` - loads UI with ``pt`` locale |
| 115 | + |
| 116 | +The language switch options work by changing the locale prefix for the same page. |
| 117 | + |
| 118 | +Listing and Removing Locales |
| 119 | +============================ |
| 120 | + |
| 121 | +To list the locales that have previously been added, you can also run ``wb-manager i18n list``. |
| 122 | + |
| 123 | +To disable a locale from being used in pywb, simply remove it from the ``locales`` key in ``config.yaml`` |
| 124 | + |
| 125 | +To remove data for a locale permanently, you can run: ``wb-manager i18n remove <loc>``. This will remove the locale directory on disk. |
| 126 | + |
| 127 | +To remove all localization data, you can manually delete the ``i18n`` directory. |
| 128 | + |
| 129 | + |
| 130 | +UI Templates: Adding Localizable Text |
| 131 | +===================================== |
| 132 | + |
| 133 | +Text that can be translated, localizable text, can be marked as such directly in the UI templates: |
| 134 | + |
| 135 | +1. By wrapping the text in ``{% trans %}``/``{% endtrans %}`` tags. For example:: |
| 136 | + |
| 137 | + {% trans %}Collection {{ coll }} Search Page{% endtrans %} |
| 138 | + |
| 139 | +2. Short-hand by calling a special ``_()`` function, which can be used in attributes or more dynamically. For example:: |
| 140 | + |
| 141 | + ... title="{{ _('Enter a URL to search for') }}"> |
| 142 | + |
| 143 | + |
| 144 | +These methods can be used in all UI templates and are supported by the Jinja2 templating system. |
| 145 | + |
| 146 | +See :ref:`ui-customizations` for a list of all available UI templates. |
| 147 | + |
0 commit comments