Sam
Sam

Reputation: 15508

How to output the outcome into a string variable - what goes wrong here?

I'm having trouble with setting a variable and then giving it the outcome of a translated string. What am I doing wrong?

# usage: this translates some text into different language.
echo __('some text'); 

# make a variable and fill it with the outcome of the translated text
$title="echo __('translated content text')";

The first line outputs nicely. The second line output literaly echo __('translated concent text').

Update

Thanks all. great answers. Gosh how stupid i must ve been, therefore am a bit wiser now:)

$title = __('Colourful train rides');   # works

now experimenting with these endings
ob_end_flush();
ob_end_clean();

Upvotes: 2

Views: 372

Answers (3)

alex
alex

Reputation: 490453

The first line outputs nicely

It sounds like that function echoes (also, its name would be very confusing if it didn't).

You will need to use an output buffer to capture that output.

ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean(); // Thanks BoltClock

Upvotes: 5

Mark Byers
Mark Byers

Reputation: 838796

Don't include quotes around the function call and don't call echo:

$title = __('translated concent text');

Upvotes: 5

Jamie Rumbelow
Jamie Rumbelow

Reputation: 5095

You're wrapping the function call in quotes, so it's being outputted literally (instead of invoking the function). Take away the pair of quotes, and you'll have a result closer to what you want:

$title = __echo('translated concent text');

However, for this to work the __echo() function will have to return the string, not just echo it. If you want to catch the output, you'll have to use PHP's Output Buffering.

ob_start();
__echo('translated concent text');
$title = ob_get_contents();
ob_end_clean();

Upvotes: 2

Related Questions