• I’m using the Global Translator plugin to help translate a Spanish website into English for some users. However in the header I’m pulling in an image that has some Spanish wording, and I would like to switch that image to an English one for the English translated pages…

    The regular website URL reads https://www.mysite.com/about/ and the English translated version reads https://www.mysite.com/en/about/

    I’m wondering if there is some sort of if/else statement that I can write that would say “if the domain name has a /en/ in it, use this image”

    Anyone have any ideas? Thanks for any help!

Viewing 15 replies - 1 through 15 (of 15 total)
  • This might be what you want:

    <?php
    if ( false === strpos(get_bloginfo('url'), '/en/') ) {
      // Use the Spanish image
    } else {
      // Use the English image
    }
    ?>
    Thread Starter Jess

    (@jessn)

    Thanks! That seems like it should work. Here’s how I have it typed out:

    <?php
    if ( false === strpos(get_bloginfo('url'), '/en/') ) { ?>
       <a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logoBlue.png" alt="logo" class="logo" /></a>
    <? } else { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logoBlueen.png" alt="logo" class="logo" /></a>
    <? } ?>

    And you can see it on this page: https://www.milliemunoz.com/contacto/
    English version: https://www.milliemunoz.com/en/contacto/

    There are some little flags in the sidebar. However clicking on the english version doesn’t change the header…any idea why?

    After thinking about it a while, I don’t think that the code I posted can work because the blog url will always be the same for all pages. I think you will need the permalink to the particular page.

    A quick not-so-pretty solution would be to use the page IDs in the if-test until a better way can be found.

    This may work:

    if ( false === strpos($_SERVER['REQUEST_URI'], '/en/') ) { ?>

    Thread Starter Jess

    (@jessn)

    Thanks so much for your help! It didn’t work, but I appreciate your assistance!

    Just as a check, try this to see if you can force the English image:

    if ( false ) { ?>

    If that doesn’t work, then it is not the if-test that is the problem.
    `

    Thread Starter Jess

    (@jessn)

    Ok if I change it to if ( false ) { ?> then it shows the second image.

    if ( false ) { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="spanish image" alt="logo" class="logo" /></a>
    <? } else { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="english image" alt="logo" class="logo" /></a>
    <? } ?>

    Is that a good thing?

    Well, it tells us that the correct image will be shown if we can get the if-test right.

    I would like to know the value of $_SERVER[‘REQUEST_URI’] on the two pages. Can you put this line of code in the php file that displays the pages so we can see what is there?

    <?php echo '<p>REQUEST:';print_r($_SERVER['REQUEST_URI']); echo '</p>'; ?>

    Ideally, the code should go just ahead of the loop. If this is not clear, please let me know and I will try to be more explicit.

    Thread Starter Jess

    (@jessn)

    Got it up, thanks. Ok so if I’m on the home page it says “REQUEST:/ ” (spanish page). And right now the English version of the home page is cached, so it’s not showing me the text. But I can see it on the English about page.

    This url: https://www.milliemunoz.com/en/about/
    Prints out: REQUEST: / about /

    OK, there is no ‘/en/’ in the REQUEST, so we can’t use that to tell if we are on the Englixh page. Is there any other way that the REQUESTs are different? Any ideas on other ways to detect which page we are on?

    Thread Starter Jess

    (@jessn)

    Well hm…this is my first time using this plugin so I’m not sure. I’ll Google around though and see if I can find anything. Thank you again for your help!!

    Try echo $_SERVER['REQUEST_URI']; instead of print_r. If this verifies the REQUEST_URI has /en/ then try..

    if(preg_match("|\/en\/|",$_SERVER['REQUEST_URI'])){
    //has EN
    }else{
    //not EN
    }

    I think for a simple variable print_r and echo will give the same results.

    print_r will just give structure information if the variable is an object or an array.

    On my default install of wordpress with permalinks my REQUEST_URI has /en/about/ in it (with echo or print_r). It is weird yours is only printing about with the spacing in the slashes. It could be something to do with your host maybe?

    Thread Starter Jess

    (@jessn)

    Hm good to know dralezero! Thanks- I’ll check with my host and see what they say!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Tricky if/else statement. Is it possible?’ is closed to new replies.