-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>';
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
vytfla
  • 549
  • 2
  • 9
  • 29
  • You're already in PHP with `` remove those from the echos. – Funk Forty Niner Feb 07 '15 at 04:38
  • You have php tags in php tags! (Use concatenation: http://php.net/manual/en/language.operators.string.php) – Rizier123 Feb 07 '15 at 04:38
  • You may want to put each of those echo statements on two separate lines... If this is WordPress' [`_e()`](http://codex.wordpress.org/Function_Reference/_e), it works differently than a returned string that can be concatenated. (e.g. by putting the `echo '
    ` part on one line, and putting the `_e(...)` part on another.)
    – summea Feb 07 '15 at 04:41
  • 1
    possible duplicate of [PHP - echo inside an echo](http://stackoverflow.com/questions/14177571/php-echo-inside-an-echo) – Rizier123 Feb 07 '15 at 04:46

3 Answers3

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.

summea
  • 7,390
  • 4
  • 32
  • 48
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>';
}
?>
Jun Rikson
  • 1,964
  • 1
  • 22
  • 43
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

Community
  • 1
  • 1
Nebula42
  • 71
  • 4