2

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.

  • Do I understand it correctly that you want the dropdown in the backoffice alsways to be in the base language, while on the frontend the translated string should appear? – cjbj May 27 '16 at 8:26
  • 1
    PO/MO files designed for one language per site. In a single site WPML translate those option by filtering option value. – Sumit May 27 '16 at 8:29
  • @cjbj you are right! – Capiedge May 27 '16 at 10:39
  • @Sumit can you explain better how to get it? I don't want the user to choose three times the same option (one per language). – Capiedge May 27 '16 at 10:39
  • Don't use variables for gettext calls - I strongly suggest reading this article – TheDeadMedic May 27 '16 at 10:44
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.

  • What do you mean with "put the gettext call in the custom field"? This function only changes the $locale variable, depending if you are in the admin or not? I still have the same problem, trying to translate the custom field stored value. I can't catch the way in which you intend to solve the problem. – Capiedge May 29 '16 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.