Internationalization
********************

A **locale** is the language an experiment is shown in, written as an
ISO 639-1 code, such as "en" for English, "de" for German or "nl" for
Dutch.


Setting locales
===============

Two configuration keys control the locales, in "config.txt" or the
experiment's "config" dictionary:

"locale" (default "en")
   The locale participants see. Every participant in a deployment sees
   the same locale.

"supported_locales" (default "[]")
   Further locales to translate the experiment into. "psynet
   translate" produces translations for these and for "locale", and
   PsyNet checks that the translation files exist before the
   experiment starts.

   locale = de
   supported_locales = ["nl"]

To run the same experiment in several languages, deploy it once per
locale, changing "locale" each time. "psynet locales" lists the
locales PsyNet supports; "psynet locales --codes-only" prints just the
codes on one line.


Marking strings
===============

Mark each participant-facing string with "_", which "get_translator()"
returns. The "translation" demo is shown in German and has a Dutch
translation:

   _ = get_translator()
   _p = get_translator(context=True)


   class Exp(psynet.experiment.Experiment):
       label = "Translation demo"

       # You could also set these in the config.txt file
       config = {
           "locale": "de",
           "supported_locales": ["en", "de", "nl"],
           # Asserted by the end-to-end tests; generic recruitment now hides
           # rewards unless the experiment opts in.
           "show_reward": True,
       }
       timeline = Timeline(
           NoConsent(),
           InfoPage(
               _p("welcome-page", "Welcome to the translation demo!"), time_estimate=2
           ),
           InfoPage(
               tags.span(
                   tags.h2(
                       f"You have chosen to translate this experiment to {config.get('locale')}."
                   ),
                   tags.p(
                       "Below you will see this text translated!",
                       tags.br(),
                       _("Below you will see this text translated!"),
                   ),
               ),
               time_estimate=5,
           ),
           InfoPage(
               tags.span(
                   tags.p("Here is an example of inline variable usage:"),
                   tags.p(
                       _(
                           "My name is {NAME}. My favorite food is {FAVFOOD}. My least favorite food is {HATEFOOD}."
                       ).format(NAME="Alice", FAVFOOD="pizza", HATEFOOD="broccoli")
                   ),
               ),
               time_estimate=5,
           ),
           ModularPage(
               "modular_translation",
               prompt=_(
                   "You can also translate text in push buttons or any kind of page!"
               ),
               control=PushButtonControl(
                   [
                       _p("button", "Chocolate"),
                       _p("button", "Vanilla"),
                       _p("button", "Strawberry"),
                   ],
                   arrange_vertically=False,
               ),
               time_estimate=4,
           ),
       )

The unmarked strings on the second page stay in English.

Warning:

  "psynet translate" finds strings by searching the source code for
  calls to "_" and "_p". Keep these names ("my_wrapper =
  get_translator()" is not recognized), and pass the text as a literal
  string, not as a variable.

"psynet translate" also extracts strings marked with "_", "_p",
"gettext" or "pgettext" from the experiment's ".html" templates.


Variables
---------

Write variables in capital letters (underscores are allowed) inside
curly brackets, and fill them in with ".format":

   next_button_name = _("Next")
   next_button_text = _('Press "{NEXT_BUTTON_NAME}" to continue.').format(
       NEXT_BUTTON_NAME=next_button_name
   )

Warning:

  Do not use f-strings. An f-string fills in the variable before the
  translation is looked up, so no translation is found.


Contexts
--------

"_" translates a string the same way wherever it appears. When one
English string needs different translations, such as "bank" for a
riverbank and for a financial institution, use "_p" and give each use
a context:

   from psynet.utils import get_translator

   _p = get_translator(context=True)

   bank_of_river = _p("river", "bank")
   financial_institution = _p("financial", "bank")

The demo uses "_p" to group the button labels under the context
""button"". In most cases "_" is enough.


Best practices
--------------

* Use "_" for most strings.

* Keep the strings short and simple.

* Leave HTML tags out of the strings; translators may translate them,
  and they make word order hard to change.

* Keep inline variables to a minimum. Instead of "_("Make the stimulus
  as {TARGET} as possible using the
  slider").format(TARGET=_("happy"))", write "_("Adjust the slider to
  match the target:") + _("happy")".


Translating
===========

Run "psynet translate" in the experiment directory:

   psynet translate

This translates the experiment into "locale" and "supported_locales".
To choose the locales on the command line instead, list them:

   psynet translate de nl

Each locale's translations are stored in
"locales/<iso_code>/LC_MESSAGES/experiment.po".


Translators
-----------

PsyNet translates with OpenAI ChatGPT ("chat_gpt").

"psynet translate" only sends strings that have no translation yet. It
groups them by source file, at most 30 strings per request. ChatGPT
also receives, for disambiguation:

* the context of each string (see Contexts);

* the existing translations from the same file, so that new
  translations use the same terms;

* the file's source code, or for large files the lines around each
  string;

* the script to write the language in, from CLDR (for example Ol Chiki
  for Santali), except where PsyNet's catalogs use another script
  (Arabic for Kurdish, Latin for Serbian).

ChatGPT needs an OpenAI API key in "~/.dallingerconfig":

   [Translator]
   openai_api_key = <your_openai_api_key>

It also needs the "openai" package:

   pip install openai

Google Translate is no longer supported. Remove "default_translator =
google_translate" from your configuration if it is there;
"google_translate_json_path" and "google_translate_project_id" are
ignored.


Reviewing and revising
======================


Manual checking
---------------

Open "locales/<iso_code>/LC_MESSAGES/experiment.po" in Poedit to check
the machine translations. Poedit shows them as "fuzzy". When you have
checked a translation, remove the fuzzy flag in Poedit.


Revising translations
---------------------

When you run "psynet translate" again:

* existing translations, fuzzy or checked, are kept as long as their
  English text and context are unchanged;

* fuzzy translations that are identical to the English text are
  translated once more, because they are often failed machine
  translations. If the model returns the English text again, as is
  right for many country names and for "OK", the entry is marked
  "psynet-kept-source" and kept on later runs. To keep an English
  translation without the retry, remove the fuzzy flag;

* translations of strings that no longer occur in the source code are
  removed.

To get a new machine translation for a string, delete its translation
in Poedit and run "psynet translate" again. "psynet translate --force"
retranslates everything and discards the translations you have
checked.

PsyNet keeps no backup of the translations, so commit the experiment's
"locales" directory to Git regularly.


Missing translations
--------------------

If a marked string has no translation in the experiment's catalog,
"psynet debug" and "psynet test local" raise an error. A live
experiment reports the error and shows the English text instead.


Translating a package
=====================

To translate a Python package for use with PsyNet, run "psynet
translate" in the root of the package. This creates a "locales"
directory in the package's source directory with translations for the
locales you list, or for all locales PsyNet supports if you list none.

See also:

  The Prepare for translation skill describes how to prepare an
  experiment for translation.
