user9870819
user9870819

Reputation:

MultiLanguage PHP Link

So I am with a little issue. Everything in this code is working but when someone put the link like this ?lang=enASa instead of ?lang=en for some reason it just shows the "define" (Don't know if I can call that a define) Example: Instead of showing: Title it shows this: _TITLE . The code is this:

<?php
    session_start();

    // Set Language variable
    if(isset($_GET['lang']) && !empty($_GET['lang'])){
        $_SESSION['lang'] = $_GET['lang'];

        if(isset($_SESSION['lang']) && $_SESSION['lang'] != $_GET['lang']){
            echo "<script type='text/javascript'> location.reload(); </script>";
        }
    }

    // Include Language file
    if(isset($_SESSION['lang'])){
        include "lang_".$_SESSION['lang'].".php";
    }else{
        include "lang_en.php";
    }
?>

In the pages I am using this (<?= _TITLE ?>) to replace the words:

<h3 style="color: white" class="subheading left"><?= _TITLE ?></h3>

And to translate i am using this file (lang_en.php):

<?php

define("_TITLE", "Title");

All code works fine, and I was hoping that someone could help me here forcing to use default English when the lang is not valid. Example: If anyone tries www.example.com/index.php?lang=edadsa instead of www.example.com/index.php?lang=en our just www.example.com/index.php force to use the default.

Upvotes: 0

Views: 91

Answers (1)

Tom&#225;š Vališka
Tom&#225;š Vališka

Reputation: 56

You have to check if a language file exists for the given language, before calling the include.

// Include Language file
if(isset($_SESSION['lang']) && file_exists("lang_".$_SESSION['lang'].".php")){
 include "lang_".$_SESSION['lang'].".php";
}else{
 include "lang_en.php";
}

Upvotes: 3

Related Questions