To translate a string in WordPress, as in most CMSs that use PHP, you have to wrap the string in a GetText translation function (i.e. __ (), _e (), _n (), _x () ... etc).
The string has also to be included in a PO file (GetText Portable Object files, the industry standard for multilingual websites in PHP - https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html). This file includes pairs of strings, one of them is the string in the original language and the other is its translation in the target language.
In order to build these PO files for every language (i.e. my-text-domain-es_ES.po for Spanish, my-text-domain-fr_FR.po for French ... etc.) in which we need to translate the plugin or theme, the translator will use a POT file (PO template) that contains all the strings to translate in the original language and will add a translated string for every one of them. PO files are compiled to binary MO files, that can be processed much faster.
At run-time, the strings to translate are fetched through the MO files and replaced by its translation.
The POT files are usually generated using special tools that parse the source code files and extract the translatable strings.
If we write code like this:
$translated_text = __( $text, 'my_text_domain');
When the code is parsed by the special tools, the value of the string to be translated ($text) is not yet defined and therefore is not present in the code. As a result, the automatic search tools will not include this string in the POT file and therefore will not be included in PO nor MO files.
At run time, when the value of the string $text presumably will be already defined, there will be no correspondence for this value in the translation files and the translation will not be possible.
However, if the set of possible values for that variable is limited and we know them, we have two options to overcome this problem:
Option 1: Manually edit the POT file to add the entries with the possible values of $text. This option is simple and easy to implement. Any code editor and minimal knowledge of the POT format will be enough. But it has one drawback. Every time we use the automatic search tools to update the translations after modifying our code, the modifications that we have made would be lost and we would have to include them manually again.
Option 2: include in our code all the possible values of $text wraped by translation functions. Let's see an example. Suppose that $text can take the values: apple, orange, banana, peach and pear. We would need to write the following code:
// this variable is used only to include as parameters in
// translation functions all the possible values of $text
$locale = __('apple', 'my_text_domain') .
__('orange', 'my_text_domain') .
__('banana', 'my_text_domain') .
__('peach', 'my_text_domain') .
__('pear', 'my_text_domain');
$translated_text = __( $text, 'my_text_domain');
This option is also easy to implement and has the advantage that it will not be lost when we use the automatic search tools to update the translations.
If we have, in our theme or plugin, several variables that we want to be translated and they have a limited set of known possible values, we could include all of them in a separated file that has to be in the root folder or a subfolder (i.e. 'includes' or 'assets' folder) of the theme or plugin as is shown below:
<?php
//values of the variables to be translated
$locale_var1 = __('text-of-var1_val1', 'my_text_domain') .
__('text-of-var1_val2', 'my_text_domain') .
__('text-of-var1_val3', 'my_text_domain') .
.......
__('text-of-var1_valn', 'my_text_domain');
$locale_var2 = __('text-of-var2_val1', 'my_text_domain') .
__('text-of-var2_val2', 'my_text_domain') .
.......
__('text-of-var2_valn', 'my_text_domain');
.............
$locale_varn = __('text-of-varn_val1', 'my_text_domain') .
__('text-of-varn_val2', 'my_text_domain') .
.......
__('text-of-varn_valn', 'my_text_domain');
This is a clean and maintainable approach that keeps the definition of these strings in a separate file that can be modified without affecting other code files.