Bilingual Site

Posted on 2011-05-11
Last Modified: 2012-06-21
I want to create a Spanish version of my site.  I have an idea how to do this but, being new to PHP, I thought I would see if perhaps I'm missing something, or there is a better way.

My thought is to drop the English version I have down into an English Folder and create a Spanish folder at the same level.

The site top index file will default and jump into the English folder. The user can switch languages, which will write a language cookie to his browser and use header() to switch to the other folder.

The top index file will look for a language cookie coming from the user and use it to jump into one folder or the other, so a user returning to the site will immediately see the language they selected the last time they visited the site.

That's it!

I know there are language packages that can express various phrases in multiple languages, but I want a complete translation of the site into Spanish - everything: testimonials, articles, image text - everything.

Thanks for any suggestions.

Question by:steva
    LVL 107

    Expert Comment

    by:Ray Paseur
    My thought is to drop the English version I have down into an English Folder and create a Spanish folder at the same level.

    You're on exactly the right track there.  See these two examples:

    You might consider getting a few extra eyes on the translations, or having a "report this" button on the translated pages.  That way you could be sure that you're getting the best quality translations!

    Good luck with it, ~Ray
    LVL 11

    Accepted Solution

    You can also use the following to check the language of the HTTP request if there is one.

    # redirect based on browser language
    $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
    switch($lang) {
    	# spanish
    	case "es":
    		header("Location: /es/index");
    	# english
    	case "en":
    	# default
    		header("Location: /en/index");

    Open in new window


    Author Comment


    Thanks for the suggestion. Actually, the wife of my client comes from Argentina, so I think I can get a decent translation.

    I looked at the php manual pages you referenced.  It would be interesting to see the change.php program that the form calls, but I guess  that's not possible.  

    By the way, my White/Eisenhamer "PHP 5 in Practice" came in and you're right, I love it.


    I wasn't aware of the HTTP_ACCEPT_LANGUAGE header.  It looks interesting, but how does it get set in the user's browser.  I see that it has q values for each language.  How would someone's browser get set, for example, to es,en;q=0.3?

    LVL 107

    Assisted Solution

    by:Ray Paseur
    Here is the language change form.  GET method takes you to the root URL, plus the language and function URL.  Easy!
    <div class="langchooser">
      <form action="/manual/change.php" method="get">
       <p>view this page in </p><fieldset><select name="page">
        <option value="pt_BR/function.isset.php">Brazilian Portuguese</option>
        <option value="zh/function.isset.php">Chinese (Simplified)</option>
        <option value="fr/function.isset.php">French</option>
        <option value="de/function.isset.php">German</option>
        <option value="ja/function.isset.php">Japanese</option>
        <option value="pl/function.isset.php">Polish</option>
        <option value="ro/function.isset.php">Romanian</option>
        <option value="fa/function.isset.php">Persian</option>
        <option value="es/function.isset.php">Spanish</option>
        <option value="tr/function.isset.php">Turkish</option>
        <option value="help-translate.php">Other</option>
       <input type="image" src="" id="changeLangImage" alt="Change language" />

    Open in new window

    LVL 11

    Expert Comment

    by:Ovid Burke
    It detects the users' browser/system languages and lists them in the same order as the users' settings. I would imagine that a user/browser/system will select at least one default language at setup or installation. I use this to initially determine which language to display. The user can still change the language by options on the site if they choose, and that part you might still need to manage with cookies.

    In FF: Tools > Options > Content (Tab) > Languages
    In IE: Tools > Internet Options > General (Tab) > Languages

    I never bothered with q=0.3 etc, because the first two characters is sufficient for this purpose.


    Author Closing Comment


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now