-1

I'm using a conditional statement to check what language the page is on. It's fine when I just need to output HTML, but the particular line I need to translate already has an echo statement in it and I am unsure how to echo the whole statement.

The line:

<div class="timer-col"> <span id="days"></span> <span class="timer-type"><?php _e('days', 'framework'); ?></span> </div>

My code:

<?php
$mylocale = get_bloginfo('language');
if($mylocale == 'en' || $mylocale == 'en-US') {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type"><?php _e('days ', 'framework'); ?></span> </div>';
} else {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type"><?php _e('dien', 'framework'); ?></span> </div>';
}
?>
4
  • You're already in PHP with <?php and ?> remove those from the echos. Commented Feb 7, 2015 at 4:38
  • You have php tags in php tags! (Use concatenation: php.net/manual/en/language.operators.string.php) Commented Feb 7, 2015 at 4:38
  • You may want to put each of those echo statements on two separate lines... If this is WordPress' _e(), it works differently than a returned string that can be concatenated. (e.g. by putting the echo '<div ..> part on one line, and putting the _e(...) part on another.) Commented Feb 7, 2015 at 4:41
  • 1
    possible duplicate of PHP - echo inside an echo Commented Feb 7, 2015 at 4:46

3 Answers 3

2

If this is WordPress and its _e() function, you may want to break up your echo lines into something like the following example:

<?php
$mylocale = get_bloginfo('language');
if($mylocale == 'en' || $mylocale == 'en-US') {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type">';
    _e('days ', 'framework');
    echo '</span> </div>';
} else {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type">';
    _e('dien', 'framework');
    echo '</span> </div>';
}
?>

This is because _e() does not return a string (that normally could be used with string concatenation). Instead, e() simply prints out translated text (without returning anything).

I've noticed that if I try to use e() inside of a string, I get unexpected placement results of where e() ends up echoing the translated text.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm thanks, that makes sense. For some reason now all the timers are reading as the day timer.
It works! Thank you, I forgot to change the span id's to reflect the correct unit.
@vytfla Glad to hear it! :)
0

Have you tried this :

<?php
$mylocale = get_bloginfo('language');
if($mylocale == 'en' || $mylocale == 'en-US') {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type">'. _e('days ', 'framework').'</span> </div>';
} else {
    echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type">'. _e('dien', 'framework').'</span> </div>';
}
?>

1 Comment

I did, but it made all the text inline and work funny.
0

You'll want to write your echo statements something like this:

echo '<div class="timer-col"> <span id="days"></span> <span class="timer-type">' . _e("days", "framework") . '</span> </div>';

With single quotes around the HTML echoed parts, double quotes inside the single quotes, and PHP concatenated with '.'

See also: PHP echo inside echo

1 Comment

This puts the text outside of the span.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.