0

I'm actually working on the languages selection of my website. All is working via a variable $language and set in a cookie:

if(isset($_COOKIE["language"]))
{
    $language = $_COOKIE["language"];
}
else
{
    $language = 'en'; 
}

Everything works, except the URLs of the website stay the same whatever the language is set:

http://localhost/modules/products/products.php

My variable $language returns me: en fr es it and de

How can I insert this variable in all the URLs to get this result without changing all the links? :

http://localhost/modules/products/products.php?lang=en

or

http://localhost/en/modules/products/products.php (that would be my favorite solution

Artik
  • 85
  • 1
  • 3
  • 9

5 Answers5

2

You don't need to set it on all URLs. You can employ a fallback-strategy to accomplish this. Check the user's intentions in this order:

  1. URL
  2. Session/Cookie
  3. Browser's/header setup (optional)
  4. Use your application's default when none of the above are set.

Implementation

Have a single link at the top of the page to switch the language. This is the only URL that will have the language parameter in it. This URL can be referred to as the language changer URL.

When you process the language changer URL, set their language of choice to the session. Then, on every subsequent request, read it from the session.

If it is not in the session and also not in the URL, then you fall back to your application's default language.

I wrote a pretty in-depth answer on this topic - https://stackoverflow.com/a/49758067/296555.

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
0

You could make something like this: $language = $_GET["lang"] ?? $_COOKIE["language"] ?? "en"

This will try to get the ?lang=en from the URL first, then try to grab the cookie value, and if not present, use de default "en" value.

The $_GET array will give you access to every value passed through the query string of the url, which is the part that comes after the question mark.

The ?? operator requires PHP7 and is explained here https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

To allow this kind of url (http://localhost/en/modules/products/products.php) you will need to rewrite paths through your web server configuration or use some kind of framework that does it for you.

Alex Ruiz
  • 415
  • 2
  • 8
0

Your code seems doesn't support multilanguage urls. Maybe the best way is create script that write selected language code to COOKIES and redirect back.

Like this:

// switch_lang.php
$language = $_GET["lang"] ?? "en";
setcookie("language", $language);
header('Location: ' . $_SERVER['HTTP_REFERER']);
Gallyamov
  • 162
  • 2
  • 9
0

A friend of mine gave me the solution. Activate the htaccess file in apache configuration. Edit your htaccess file and add:

RewriteEngine On
RewriteBase /

RewriteRule ^(en|fr|es|it|de)/(.*)$  $2?language=$1 [L,QSA]

Then in your php:

//Check if the select sends the Post lang
if (isset($_POST["lang"]))
{
    $var =substr_replace($_SERVER['REQUEST_URI'], $_POST["lang"], 1,2) ;
    header('Location:http://'.$_SERVER['HTTP_HOST'].$var);
}

//Redirect to EN laguage if no language is set in the URL, or Cookie
if (empty($_COOKIE["language"]) && empty($_GET["language"]))
{
    header('Location:http://'.$_SERVER['SERVER_NAME'].':8000/'.'en/');
}

//Check if language cookie is here and if it's different from the url
if (isset($_COOKIE["language"]) && empty($_GET["language"]))
{
        header('Location:http://'.$_SERVER['SERVER_NAME'].':8000/'.$_COOKIE["language"].'/');
}
elseif (isset($_GET["language"]))
{
    $language = $_GET["language"];
    setcookie ('language', $language, time() + 60*60*24*30, '/', 'localhost');
}
else
{
    $language = 'en';
}

The language selector:

        <form action="" method="post">
            <select name="lang" onchange='this.form.submit()'>
                <option value="en" {if $language == en}selected{/if}>En</option>
                <option value="fr" {if $language == fr}selected{/if}>Fr</option>
                <option value="es" {if $language == es}selected{/if}>Es</option>
                <option value="it" {if $language == it}selected{/if}>It</option>
                <option value="de" {if $language == de}selected{/if}>De</option>
            </select>
        </form>
Artik
  • 85
  • 1
  • 3
  • 9
-2

For the last answer you'd need to build all the links youself, when looking at the links you currently have, you're accessing the files the default way. You could include a file in every file you're accessing with something like this:

if(isset($_GET['lang']))
{ 
   header("Location: " .getcwd()."?lang=".$language.");
}
Timberman
  • 647
  • 8
  • 24