i18n in Vue apps
================
We use Gettext with `vue-gettext`_ to translate strings in our Vue apps.
Here is some advice specific to ``vue-gettext``:
* Always add a ``key`` attribute when you have conditional translated strings.
For example, DO NOT do this:
.. code-block:: html
// TranslatedExample.vue
First translated string
This string will never be shown
The string in the ``v-else`` will never be shown, because Vue does not know that the two ``
`` tags actually have different content. Vue tries to limit the number of DOM changes, so it will only change attributes and will change the text node, which will mess up ``vue-gettext``. See `the Vue.js documentation on the key attribute `_ for details.
INSTEAD, DO THIS:
.. code-block:: html
// TranslatedExample.vue
First translated string
This string will be shown
* Never use Vue.js interpolation inside translated strings.
For example, DO NOT do this:
.. code-block:: html
// TranslatedExample.vue
Current value: {{ reactive_value }}
Current value: {{ reactive_value }}
This will break the translation. The string will always show in English, never in translated languages. Always use ``v-bind:translate-params="{ params }"`` or ``v-translate="{ params }"`` with ``%{ param }`` in the translated string. See `vue-gettext's syntax `_
INSTEAD, DO THIS:
.. code-block:: html
// TranslatedExample.vue
Current value: %{ reactive_value }
Current value: %{ reactive_value }
* Never use ``v-bind`` on attributes in HTML tags in translated strings.
For example, DO NOT do this:
.. code-block:: html
// TranslatedExample.vue
{{ link_text }} has done some changes in this document.
This will break reactivity. If ``link_url`` or ``link_text`` change value, the text will not change. See `vue-gettext's doc about this `_.
INSTEAD, DO THIS:
.. code-block:: html
// TranslatedExample.vue
%{ link_text } has done some changes in this document.
* Name your parameter when your translations have parameters.
For example, DO NOT do this:
.. code-block:: html
// TranslatedExample.vue
%{ vue_variable_for_nb } changes have been done in this document.
If your vue variable is updated, then you won't have to update the corresponding translation.
INSTEAD, DO THIS:
.. code-block:: html
// TranslatedExample.vue
%{ nb } changes have been done in this document.
Resources
^^^^^^^^^
- vue-gettext: https://github.com/Polyconseil/vue-gettext
.. _vue-gettext: https://github.com/Polyconseil/vue-gettext