Jump to content
MakeWebGames

Detect browser language & change page accordingly


Haunted Dawg

Recommended Posts

Well, i just wanted to check if this is do able. And it pretty much is.. If you really wanted to expand your website. Here's a small method..

We use the $_SERVER['HTTP_ACCEPT_LANGUAGE'] php variable here.

What we do is we mainly generate our file's in english. We then put all the word's we would like to translate in a huge array along with the english array. And here is a small snippet:

 

<?php
$foul = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$sub  = substr($foul, 0, 2);
$languages = array(
   'es' => 'Spanish',
   'en' => 'English',
);
if(!isset($_GET['lang']))    {
   ob_start($languages[$sub]);
}
function Spanish($all)    {
   $spanish = array('hola','como','haces');
   $english = array('hello','how','do');
   return str_ireplace($english, $spanish, $all);
}
function English($all)    {
   //Since our content is written in english, we dont need to replace anything
}

 

That for me would change, hello, how & do to the according spanish word's as my pc is spanish.

I hope atleast some one find's this usefull.

Link to comment
Share on other sites

  • 3 months later...

You missed a key point..

For example:

I Agree - English

Acepto - Spanish

Ich bin einverstanden - German

Je suis d'accord - French

All the same phrase, just translated.. So technically your function has a limit on what you can do...

Why not keep it simple? Have a file of translations and link them into your actualy file..

I'm using CodeIgniter for a project I am doing, thought it'll be different from what I am use to, but anyway... Here is a snippet I use with CodeIgniter..

 

class Login extends Controller
{ 

private function getLang()
{
	$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
	if (!in_array($lang, array('en', 'es', 'de', 'fr')))
	{
		$lang = 'en';
	}
	//$lang = 'fr'; <-- Testing purposes only..
	return $lang;
}

public function index()
{
	// Blah blah...
	$this->lang->load('login', $this->getLang());
	// Get information needed and pass it to the view.
}

}
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...