Jump to content

Recommended Posts

Posted

Boas tardes, pretendo ter no meu site a opção de multi língua e tenho um script a funcionar mas não me parece que seja a maneira melhor.

Tenho o seguinte código:

<?php
if (isset($_SESSION['id']))
  {
    // ...
    //Query SQL para ir buscar o idioma no user
    // ...
    if ($lang == 'pt')
      {
        include('includes/langs/portuguese.php');
      }
    elseif ($lang == 'gb')
      {
        include('includes/langs/english.php');
      }
    elseif (($lang != 'gb') OR ($lang != 'pt'))
      {
        include('includes/langs/portuguese.php');
      }
  }
else
  {
    $_SESSION['lang'] = $_GET['lang'];
    if (!isset($_SESSION['lang']))
      {
        include('includes/langs/portuguese.php');
      }
    elseif ($_SESSION['lang'] == 'pt')
      {
        include('includes/langs/portuguese.php');
      }
    elseif ($_SESSION['lang'] == 'gb')
      {
        include('includes/langs/english.php');
      }
    elseif (($_SESSION['lang'] != 'gb') OR ($_SESSION['lang'] != 'pt'))
      {
        include('includes/langs/portuguese.php');
      }
  }
?>

Não haverá maneira melhor de o fazer sem estar sempre a fazer esta verificação?

Posted

deixo aqui o meu script!

ficheiro lingua.php

session_start();
if(isset($_GET['lang']))
{
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;

setcookie('lang', $lang, time() + (3600 * 24 * 30));

}else if(isset($_SESSION['lang'])){
$lang = $_SESSION['lang'];
}else if(isset($_COOKIE['lang'])){
$lang = $_COOKIE['lang'];
}else{
$lang = 'pt-PT';
}

switch($lang){
case 'en-GB' : $lang_file = 'en-GB.php';
break;
case 'pt-PT' : $lang_file = 'pt-PT.php';
break;
default : $lang_file = 'pt-PT.php';
}

include("lang/".$lang_file);

ficheiro pt-PT.php (ficheiro de lingo dentro da pasta lang

$lang = array();

$lang['TITULO_PAGINA'] = "TITULO SITE";
$lang['PESQUISA'] = "Pesquisa";

ficheiro en-GB.php (ficheiro de lingo dentro da pasta lang

$lang = array();

$lang['TITULO_PAGINA'] = "SITE TITLE";
$lang['PESQUISA'] = "Search";

no index.php tenho

include("lingua.php");
// para usar meto assim com este exemplo:
echo $lang['TITULO_PAGINA']; 

// as "bandeiras" so tem de ter o link para index.php?lang=pt-PT
// o pt-PT é o nome do ficheiro, se tiveres o ficheiro de lingua pt.php so tens de por lang=pt
Posted

Gravas um cookie com duração de 1 mes por exemplo(o meu é!) e verificas se ha cookie, se houver nao faz mais nada, se não houver parte a accao para a sessao, se nao for isto, entao peco desculpa porque nao percebi onde querias chegar

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.