translations.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/translations.rst">Edit me on GitHub</a></div>
  3. ============
  4. Translations
  5. ============
  6. Converse supports localization of its user interface and date formats. As
  7. of writing, 17 languages are supported.
  8. The translations of Converse can be found in the `locale
  9. <https://github.com/jcbrand/converse.js/tree/master/locale>`_ directory.
  10. Translations of Converse are very welcome. You can add translations either
  11. manually by editing the ``.po`` files in the above-mentioned ``locale``
  12. directory, or through the web at `weblate <https://hosted.weblate.org/projects/conversejs/#languages>`_.
  13. As of version 3.3.0, Converse no longer automatically bundles translations
  14. in its source file and instead fetches only the relevant locale for the current
  15. session from a URL as specified by the :ref:`assets_path` setting.
  16. There are three configuration settings relevant to translations and
  17. localisation. You're encouraged to read the documentation for each of them.
  18. * :ref:`i18n`
  19. * :ref:`locales`
  20. * :ref:`assets_path`
  21. Manually updating translations
  22. ==============================
  23. If you simply want to add a few missing translations, then consider doing it
  24. through the web at `weblate <https://hosted.weblate.org/projects/conversejs/#languages>`_.
  25. Some things however cannot be done via weblate and instead have to be done
  26. manually in a checkout of the Converse source repository.
  27. These tasks are documented below.
  28. Updating the translations template (.pot file)
  29. ----------------------------------------------
  30. The gettext `.pot` file located in
  31. `./locale/converse.pot <https://github.com/jcbrand/converse.js/blob/master/locale/converse.pot>`_
  32. is the template containing all translations and from which for each language an individual PO
  33. file is generated.
  34. The `.pot` file contains all translateable strings extracted from Converse.
  35. To make a user-facing string translateable, wrap it in the double underscore helper
  36. function like so:
  37. .. code-block:: javascript
  38. __('This string will be translated at runtime');
  39. After adding the string, you'll need to regenerate the POT file:
  40. ::
  41. make pot
  42. Making translations file for a new language
  43. -------------------------------------------
  44. To create a new translations file for a language in which Converse is not yet
  45. translated into, do the following
  46. .. note:: In this example we use Polish (pl), you need to substitute 'pl' to your own language's code.
  47. ::
  48. mkdir -p ./locale/pl/LC_MESSAGES
  49. msginit -i ./locale/converse.pot -o ./locale/pl/LC_MESSAGES/converse.po -l pl
  50. Please make sure to add the following attributes at the top of the file (under
  51. *Content-Transfer-Encoding*). They are required as configuration settings for Jed,
  52. the JavaScript translations library that we're using.
  53. .. code-block:: po
  54. "domain: converse\n"
  55. "lang: pl\n"
  56. "Content-Type: text/plain; charset=UTF-8\n"
  57. "plural_forms: nplurals=2; plural=(n != 1);\n"
  58. Updating an existing translations file
  59. --------------------------------------
  60. You can update the `.po` file for a specific language by doing the following:
  61. .. note:: In this example we use German (de), you need to substitute 'de' to your own language's code.
  62. ::
  63. msgmerge ./locale/de/LC_MESSAGES/converse.po ./locale/converse.pot -U
  64. To do this for ALL languages, run:
  65. ::
  66. make po
  67. The resulting `.po` file is then what gets translated.
  68. Generating a JSON file from a translations file
  69. -----------------------------------------------
  70. Unfortunately `Jed <http://slexaxton.github.io/Jed>`_, which we use for
  71. translations in Converse cannot use the `.po` files directly. We have
  72. to generate from it a file in JSON format and then put that in a `.js` file
  73. for the specific language.
  74. To generate JSON from a PO file, you'll need po2json for node.js. Run the
  75. following command to install it (npm being the node.js package manager):
  76. ::
  77. npm install po2json
  78. You can then convert the translations into JSON format:
  79. ::
  80. po2json -p -f jed -d converse locale/de/LC_MESSAGES/converse.po locale/de/LC_MESSAGES/converse.json
  81. To do this for ALL languages, run:
  82. ::
  83. make po2json
  84. .. note::
  85. If you are adding translations for a new language that is not already supported,
  86. you'll have to add the language path in main.js and make one more edit in ./src/locales.js
  87. to make sure the language is loaded by require.js.
  88. Making sure the JSON file will get loaded
  89. ------------------------------------------
  90. Finally, make sure that the language code is added to the list of default
  91. values for the ``locales`` config setting.
  92. This is done in ``src/converse-core.js``.
  93. Look for the following section:
  94. .. code-block:: javascript
  95. // Default configuration values
  96. // ----------------------------
  97. this.default_settings = {
  98. // ... Omitted for brevity
  99. locales_url: 'locale/{{{locale}}}/LC_MESSAGES/converse.json',
  100. locales: [
  101. 'af', 'ar', 'bg', 'ca', 'de', 'es', 'en', 'fr', 'he',
  102. 'hu', 'id', 'it', 'ja', 'nb', 'nl',
  103. 'pl', 'pt_BR', 'ru', 'tr', 'uk', 'zh_CN', 'zh_TW'
  104. ],
  105. // ... Omitted for brevity
  106. };