Getting translated string through a variable

I am translating a website into different languages, and I have the following problem:

Through the backend, user can select via a dropdown, a value (text string) from a list of 50 options.
This dropdown is stored in a custom field (by the way, managed with ACF PRO).

When a user visits the translated version of the site, I would like to display the translation of that text string.

At first I thought that using

$options_obj = get_field_object('user_options');
$options_value = get_field('user_options');
$options_label = $options_obj['choices'][$options_value];

echo __($options_label, 'my_text_domain');

And I put the 50 text strings translation (corresponding to all the selectable options) in a po / mo file, but as we know this won’t work. Gettext don’t translate variables.

How I could get the translated string?
Do you have any ideas?
Thank you.

1 Answer
1

I’m not completely sure whether this is what you want, but if the option page is in one language you can simply put the gettext call in the custom field itself to avoid using variables. For the admin you force the translation to spanish:

function wpse227983_force_language ( $locale ) {
    if ( is_admin() ) {
        return 'es_ES';
    }
    return $locale;
}
add_filter( 'locale', 'wpse227983_force_language' );

On the frontend the messages will then be translated in the normal way, supposing that you have a system in place that determines what the language of the page is. If the author sets a custom field for the language, you can use the above filter with a different if to force the translation.

Leave a Comment