2

I'm developing a basic website using PHP's built-in gettex support. I don't want to use any components of external libraries for different reasons; first because I want to practice the techniques related to gettex, second because the site is small enough not to demand anything other than vanilla php, third for performance. The issue I'm facing now is related to navigating the site in the languages I have already localized.

I've started out by setting up the needed directory structure:

locales/
    - pt_BR/
        - LC_MESSAGES
            messages.mo
            messages.po
    - es_ES/
    - ru/

Then included a file with the code below at the top of each page:

<?php
// use sessions
session_start();

// get language preference
if (isset($_GET["lang"])) {
    $language = $_GET["lang"];
}
elseif (isset($_SESSION["lang"])) {
    $language  = $_SESSION["lang"];
}
else {
    $language = "en_US";
}

// save language preference for future page requests
$language = $_SESSION["Language"];

$folder = "locale";
$domain = "messages";
$encoding = "UTF-8";

putenv("LANG=" . $language);
setlocale(LC_ALL, $language);

bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);

textdomain($domain);

After setting up the code I added a simple rule to my .htaccess not to use a query string, but simple locale codes instead:

RewriteEngine on
RewriteRule (.+/)?pt-br/? ?lang=pt_BR

For the home page at the server root directory it worked as expected. If I type a url like mysite.test it renders everything in English and if I switch to mysite.test/pt-br I get everything in Brazilian Portuguese. My trouble is: how can I have the other pages in the site to keep this pattern? If I enter mysite.test/pt-br/about, for instance, I get the same home page in Brazilian Portuguese, not the About (equally previously localized) page.

In my understanding that happens because the server looks for the file about.php inside the directory pt-br that, in fact, doesn't exist, then it resorts back to the rewrite rule, but... I don't really think this is the case and am wondering how the whole thing is done, that is, building a structure where URLs are such as:

mysite.test/pt-br/about
mysite.test/es/about
mysite.test/ru/about

And the same for any other page.

I haven't found anything precise enough around other than instructions on how to set up the directory structure for gettex to work and using sections to keep the language choice, but I'm not sure on the implementation, so any help to find material on that would be really appreciated.

5
  • 1
    $language = $_SESSION["lang"]; / $_SESSION["Language"] = $language; Feb 7, 2019 at 7:26
  • +1 Even though this is subject to be closed due to typo only, you've earned to get much rep in order to investing the effort forming a proper question on SO! Well done! Feb 7, 2019 at 7:34
  • @Quasimodo'sclone, the odd thing about the typo you mentioned above is that as soon as I fixed it in my code the expected output I described when entering an url such as mysite.test/pt-br stopped working. I get the English version of the page only. Feb 7, 2019 at 7:39
  • Please update the question accordingly. What is the value of $language? Feb 7, 2019 at 7:47
  • $folder = "locale"; but you show a structure locales/... Feb 7, 2019 at 7:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.