Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Language switcher using directory structure

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #27892
    e11world
    Member

    Hi all,
    I’ve been looking on the net for something like this and found a few things but because I’m a beginner still, I can’t put it together.
    I need to change the site’s language based on the folder /en/ or /fr/ and the file names are always the same. The site is not big and this would work perfectly.

    I have this code in asp that does it but not sure how to convert it into php:
    A sample site using it is http://www.contacservices.com/fr/contactus.html but on home page, it’s using index.html and index_fr.html because they’re not in the /en/ and /fr/ folders

    Code:
    <% Dim PATH_INFO PATH_INFO = Request.ServerVariables("PATH_INFO") If (Instr(PATH_INFO,"/fr/")>0) Then
    ‘Response.Write(“FRENCH”)
    PATH_INFO =Replace( PATH_INFO , “/fr/”, “/en/”)
    ElseIf (Instr(PATH_INFO,”/en/”)>0) Then
    ‘Response.Write(“ENGLSIH”)
    PATH_INFO =Replace( PATH_INFO , “/en/”, “/fr/”)
    End If
    %>
    <%'Response.Write(Request.ServerVariables("PATH_INFO"))%>

    I also found this http://www.phpbuilder.com/tips/item.php?id=343 but I can’t figure out why it’s not working. Maybe I’m just not using it right.
    Can you guys please help me?

    #70594
    Chris Coyier
    Keymaster

    I really have no idea. The closest I’ve ever come to playing with multiple language support is this nice jQuery plugin:

    http://sundaymorning.jaysalvat.com/

    #70690
    e11world
    Member

    Unfortunately, this is not what I had in mind. I’ve seen that before but I’ve posted my request on another forum and will post back a link once I get the right answer. Thanks Chris!

    #70747
    e11world
    Member

    The url looks messy when you click on the flags and navigation back and forth. I think this confuses the user.
    I found another sample but also not sure how to use this one either http://www.phpbuilder.com/tips/item.php … int_mode=1 :?
    Please, I hope someone answer my question the way I wrote it. I think I’m getting closer but I would love to see code samples because I’m such a newbie with PHP.
    I need it to do the same thing shown here http://www.contacservices.com/en/aboutus.html (when you click on the Français in the top right corner). It just changes the folder from en to fr and reloads/redirects the page. This works from any page except the home page which is because it’s not within the en or fr folders.
    Thanks to anyone who can help!

    #70772

    Ok I think I understand what you mean. You have this setup:
    [img]http://grab.by/2mmk[/img]
    and you want the php code to create a link that will switch between the en and fr directories.

    #70795
    e11world
    Member

    EXACTLY! Thanks for clearing this up Dave. Only thing is, the include folder would be in the top root level as well as the en and fr folders. Do you have a solution for this?

    #70818

    You can use a regular expression to get the language out of the url. Such as:

    Code:
    if (preg_match(‘/^/(en|fr)/(.*)/’, $_SERVER[‘REQUEST_URI’], $matches)) {
    $cur_lang = $matches[1];
    if ($cur_lang == ‘en’)
    $alt_lang = ‘fr’;
    else
    $alt_lang = ‘en’;

    $alt_lang_url = “/{$alt_lang}/{$matches[2]}”;

    echo “$alt_lang“;
    }

    #70851
    e11world
    Member

    Thanks for the reply again dave.
    But I found a solution after playing around with my original code!!! :lol: :lol: Basically converting the code I already had from the start from ASP to PHP did the trick:

    I put the following code in the include folder and I named it hdr-ar.php

    Code:
    0){
    echo “YOU ARE IN THE ARABIC SITE”;
    $path_info = str_replace(“/ar/”, “/en/”, $path_info);
    }else if (strpos($path_info, “/en/”) > 0){
    echo “YOU ARE IN THE ENGLISH SITE”;
    $path_info = str_replace(“/en/”, “/ar/”, $path_info);
    }
    echo ‘
    ‘;
    echo $path_info;
    ?>

    You can comment the echo statements. It’s just there for testing.

    I put the following code in the include folder and I named it hdr-en.php

    Code:
    0){
    echo “YOU ARE IN THE ARABIC SITE”;
    $path_info = str_replace(“/ar/”, “/en/”, $path_info);
    }else if (strpos($path_info, “/en/”) > 0){
    echo “YOU ARE IN THE ENGLISH SITE”;
    $path_info = str_replace(“/en/”, “/ar/”, $path_info);
    }
    echo ‘
    ‘;
    echo $path_info;
    ?>

    *The arabic writing might show weird on this post but you get the point.

    I had a problem running this using PATH_INFO which didn’t really do anything. Then I used REQUEST_URI and that fixed it right away.
    Just include the file(s) using

    Code:

    and you’re good to go.

    It’s too bad not many people answered my question properly (maybe didn’t fully understand it) and I’m more amazed/sad :shock: at how little responses there were but anyways, I hope this helps someone in the future. :D
    By the way, this works in php5 and should be fine in php4 too.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.