1

Example I have this outputting decoded JSON $data and want to make contents translateable in wordpress.

<?php echo $data['hair']['type'] )?>

So in english this would output "Brown hair", but I'd like them to be available for translation.

I know there is some syntax to put strings in __('string') but I cant seem to make it work with Json outputs.

  • You have to translate it first, then encode it to JSON. – fuxia Feb 23 '16 at 20:07
  • What I wanted is the output be translatable to different languages, as wordpress makes it available with its core function __( ) or _e( ), if I translate first than it's not translatable output anymore but static output. for example with PO Edit I can translate strings in different language. – annkswe Feb 23 '16 at 20:15
  • You cannot pass variables to these functions, you have to use strings. That's why you run the translations functions on the values first and encode them later. Why is that a problem? – fuxia Feb 23 '16 at 21:01
1

Technically, and this is highly discouraged do to it's unfriendly and unpredictable nature, you can do this:

<?php _e( $data['hair']['type'] ); ?>

However, you'll need to have all the possible values for that ("brown hair", "blond hair", etc.), listed in a .POT file (a translation template) so that translators know what to translate. You'll need to either have control over whatever creates the JSON data, or intimate knowledge of what it can output, you won't be able to get far.

The __() and similar functions take a string, look it up in the translation file they're provided (you usually pass a "textdomain" name so it knows which one to use), and returns the matching translated string it finds, otherwise returning the original string.

When people say that those functions "make the contents translatable" they mean it identifies the text as being translatable for a human or bot that goes through the code to find and assemble the .POT file, which is then given to translators to make completed versions in their languages.

Addendum (hit submit too soon):

This is why you don't pass a variable to those functions; the person or bot searching your code won't know what the actual text is that needs to be translated.

You're options are to either write those entries in the POT file yourself, like this:

msgid "Brown hair"
msgstr ""

msgid "Blond hair"
msgstr ""

Or otherwise have the values defined in your code somewhere like so:

$hair_types = array(
    'Brown hair' => __( 'Brown hair' ),
    'Blond hair' => __( 'Blond hair' ),
);

And then reference it somewhat like this:

<?php echo $hair_types[ $data['hair']['type'] ]; ?>

And hope the JSON you get doesn't add curve balls like "Brownish-reddish-goldish hair" and other values you weren't aware were even possible.

Basically, if you don't have control over what's generating the text, you won't be able to (nor expected to) localize it.

  • Thank you for running with it. Yes the Json data is strictly defined thus predictable. I am going to give it a shot. – annkswe Mar 3 '16 at 23:48

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.