Jump to content

Recommended Posts

My website is multilanguage, you can change your langluage yourself.

 

In the head of the site ( English Spanish Portuguese Italian)

 

I like to change this into the flag of the country.

Does anyone know how to change the name into the en.png

 

thanks

Dennis

 

 

HTM page


<?php language_navigation(); ?>

 

en.php

 

$lang["available_language"] = array(
	"en" => "English",
	"es" => "Spanish",
	"pt" => "Portuguese",
	"it" => "Italian",	
);

 

config.php


// The default language
$default_lang = "en";

// The relative path to the lang folder
$lang_folder = "lang";




//Get the language used by the browser
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$language = "";

// Check if the language file exists
function use_lang($language)
{
	global $lang_folder;
	if( is_file($lang_folder."/".$language.".php") )
	{
		return true;
	}
}	

if( isset($_GET['lang']) && use_lang($_GET['lang']) )
{
	$language = $_GET['lang'];
}
else if ( isset($browser_lang) && use_lang($browser_lang) )
{
	$language = $browser_lang;
}
else 
{ 
	$language = $default_lang;
}

// Include the right language file	
    include($lang_folder."/".$language.".php");

// Helper function to echo the values of the $lang array
function lang($key)
{
	global $lang;
	echo $lang[$key];
}

function language_navigation()
{
	global $lang;
	$languages = $lang["available_language"];
	foreach( $languages as $single_language )
	{
		echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?lang=' . key($languages) . '">'. $single_language . '</a></li>';
		next($languages);
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/
Share on other sites

the line

if( isset($_GET['lang']) && use_lang($_GET['lang']) )

checks to see if any language variables are set in the URL

You need to pass the $_GET variables when a user clicks on the appropriate language flag

 

$lang["available_language"] = array(
	"en" => "English",
	"es" => "Spanish",
	"pt" => "Portuguese",
	"it" => "Italian",	
);

//you'll have to set the src part to point to the folder where the flags are
//you'll also have to change index.php to the page name, or look into using a $_SERVER variable to echo the page
foreach ($lang AS $key=>$value) {
echo "<a href='index.php?lang=$key'><img src='$key.png' alt='$value' /></a>";
}
//foreach will return <a href='index.php?lang=en'><img src='en.png' alt='english' /></a> for the first

Maybe i am running in a circle: and i am little confused.

I try so hard but i fail (stupid me).

 

For this it is working, the flags are ok. (thanks)

 

but i can not change the language for the site.

It keeps looking at my explorer language and changing it back.

Why can't the language stay whatever i want the language to be?

 

 


global $lang;
	$languages = $lang["available_language"];
	foreach( $languages as $available_language=>$key )
	{

			 echo "<a href='index.php?lang=$available_language'><img src='$key' alt='$image' /> ";

				next($languages);
			}
}

Hello Joel,

 

I have read all info about GET use $

But it dazzels me (maybe this is too much for a newbie) i need to read more.

 

The problem was(is) clients in France are on my website and have the site in France (ok).

But they are English poeple... they can not change the site completley in English only one page at the time.

 

(for the imanges this is only a nicer look)

 

Maybe if i figured it all out i will give a nice reply, for the whole multilanguage PHP script.

 

Thank for now

dennis

 

use a session to store the language for the duration of the user's visit to your site and then check if the session is set after you check the get variables (in case they want to change languages again)

i.e.

session_start();

if (isset($_GET['lang'])) {
//add to session
$_SESSION['lang'] = $_GET['lang'];
} else if (isset($_SESSION['lang'])) {
//language is set in session - do nothing.
} else {
//default language
$_SESSION['lang']='en'
}

// Include the right language file	
include("lang/{$_SESSION['lang']}.php");

 

*EDIT* you need to ensure session_start(); is located at the top of each PHP script which you want to include the session variables. read up on sessions at the link I put above.

 

Hello Joel,

 

All is working.... but i tested the use_lang($browser_lang) i can't get it right.

 

Can you help me with the last point,

oh by the way, i removed the $_SERVER['PHP_SELF']

With PHP 5 this is not working..... see what i did.

 

top page of index.php

<?php session_start();
include("config.php"); ?>

 

<?php
// The default language
$default_lang = "en";

// The relative path to the lang folder
$lang_folder = "lang";




//Get the language used by the browser
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$language = "";

// Check if the language file exists
function use_lang($language)
{
	global $lang_folder;
	if( is_file($lang_folder."/".$language.".php") )
	{
		return true;
	}
}	

if( isset($_GET['lang']) OR use_lang($_GET['lang']) )
{
	$language = $_GET['lang'];
}
else if ( isset($browser_lang) OR use_lang($browser_lang) )
{
	$language = $browser_lang;
}
else 
{ 
	$language = $default_lang;
}

// Include the right language file	
    include($lang_folder."/".$language.".php");

// Helper function to echo the values of the $lang array
function lang($key)
{
	global $lang;
	echo $lang[$key];
}

function language_navigation()
{
	global $lang;
	$languages = $lang["available_language"];
	foreach( $languages as $available_language => $key )
	{
		echo "<a href=" . '?lang=' . $available_language . " ><img src='$key' /> ";
		next($languages);
	}
}
?>

Hello Joel,

 

All is working.... but i tested the use_lang($browser_lang) i can't get it right.

 

Can you help me with the last point,

oh by the way, i removed the $_SERVER['PHP_SELF']

With PHP 5 this is not working..... see what i did.

 

top page of index.php

<?php session_start();
include("config.php"); ?>

 

<?php
// The default language
$default_lang = "en";

// The relative path to the lang folder
$lang_folder = "lang";




//Get the language used by the browser
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$language = "";

// Check if the language file exists
function use_lang($language)
{
	global $lang_folder;
	if( is_file($lang_folder."/".$language.".php") )
	{
		return true;
	}
}	

if (isset($_GET['lang'])) {

//add to session

$_SESSION['lang'] = $_GET['lang'];

} else if (isset($_SESSION['lang'])) {

//language is set in session - do nothing.

} else {

//default language

$_SESSION['lang']='en';
}

// Include the right language file
include("lang/{$_SESSION['lang']}.php");



// Helper function to echo the values of the $lang array
function lang($key)
{
	global $lang;
	echo $lang[$key];
}

function language_navigation()
{
	global $lang;
	$languages = $lang["available_language"];
	foreach( $languages as $available_language => $key )
	{
		echo "<a href=" . '?lang=' . $available_language . "><img src='$key'/></a> ";
		next($languages);
	}
}
?>




I want the user to have his own language to start (by checking his browser language)

 

And second to make a choice to change the language to another language.

(not only the page is is watching, but the whole site )(we fixed this it is ok now)

 

( for instance a Englishman behind a internetcafe-pc in France.. this is a france pc ) but he want the site in English.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.