Hi there,

I'm a real novice so apologies for my ignorance... any help would be greatly appreciated.

I'm trying to create a basic multilingual page using POEdit (which seems to work fine).

require_once('localization.php');

echo gettext("Hello World!");
echo "<p>" . _("Welcome to my translation test page."). "</p>";

localization.php:

$locale = "es_ES";
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
bind_textdomain_codeset("messages", 'UTF-8');
textdomain("messages");

So I have translated both strings in POEdit and the page loads in Spanish. All good up to here.

The next thing I want to do is create two links at the top of the page "English" and "Español" to enable the user to select the language they want to see the page in. The page should load by default in English. So I was going to add something along the lines of:

echo "<p><a href=\"".$_SERVER['PHP_SELF']."?locale=en_UK\">English</a> -
  <a href=\"".$_SERVER['PHP_SELF']."?locale=es_ES\">Español</a></p>\n";

Here I'm stuck. The page loads in Spanish and the links don't change anything.

Sorry again if the problem is blatantly obvious. Thanks for any help. I'm using apache on windows.

    Try this.
    Now, I know too little of your script to say it will work.
    But have a try. 🙂
    localization.php

    //try get locale from URL, else use 'en_UK' for default
    $locale = isset($_GET['locale']) ? $_GET['locale'] : 'en_UK';
    //$locale = "es_ES";
    putenv("LC_ALL=$locale");
    setlocale(LC_ALL, $locale);
    bindtextdomain("messages", "./locale");
    bind_textdomain_codeset("messages", 'UTF-8');
    textdomain("messages"); 

      Thanks for trying but still no luck. The page still displays translated in Spanish and the links don't change anything. Any other ideas?

      Maybe because I'm including the line:

      require_once('localization.php');
      

      at the top of index.php the strings are translating on load. I would like to include this line only when the user clicks on the link "Español". And effectively cancel the line when the user clicks on "English". Is there a way to do this?

      Thanks again for any help.

        you can check the $_SERVER["HTTP_ACCEPT_LANGUAGE"] variable to know the user's accepted languages.
        If the first entry isn't en_en lets echo your language selector.

          Write a Reply...