Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

brichards commented Oct 15, 2014

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