Skip to content

Instantly share code, notes, and snippets.

@trishasalas
Created October 14, 2014 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trishasalas/f45091c5d2cb077251a1 to your computer and use it in GitHub Desktop.
Save trishasalas/f45091c5d2cb077251a1 to your computer and use it in GitHub Desktop.
translate a variable
<?php
$url = get_post_meta( $post->ID, '_project-url', true );
if ($url != '') {
$link = sprintf( __( '%s', 'wpp-portfolio' ), esc_url( $url ) );?>
<p><?php echo $link;?></p>
<?php }
?>
@brichards
Copy link

The short answer here is that you cannot make a variable that is obviously translatable like this. The reason is because an auto-generated .pot file will only include "%s" as the translatable text and a human translator will come along and ask, "What does %s mean?"

Now, that said, this can properly translate your passed variable, but it will only work if the translation file has a translation available for whatever content is passed in. The way to make this happen is to manually edit your generated .pot file to include all known values for %s so that a translator can translate them in the .mo they return to you. Of course, if you knew all of the possible variables for $url, you probably wouldn't be doing this in the first place :)

Here's a fork of your code sample. The first code sample is a simpler way of writing what you wrote and maintains the translation function. The second sample just removes the internationalization (since I suspect you won't know all possible values for the user-provided $url and therefore can't possibly include them in your .pot file to make translation practical)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment