I would recommend allowing the user to choose their own language. However, you could use something like this:
<?php
if (!isset($language)) {
$language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$language = strtolower(substr(chop($language[0]),0,2));
}
?>
$language
would then contain the language you should display to your user. What you do from there is up to you.
You'd do better to use whatever language the client passes headers saying it prefers, at first. That, at least, lets the user decide what language they want without potentially having to wander through the foreign-language preferences trying to figure out the Russian words for "show me stuff in English, damn it!" Their browser will almost certainly be set to accept a language the user knows well. (If it doesn't, then it's misconfigured.) Use that code, or their preferences once they've picked one, to select a table of localized messages / files a la gettext.
No, that doesn't mean you want to localize your site. It means you're assuming you know what your users want, better than they do. Take a look at this question and answers on UX.stackexchange.com
The problems with your approach are that you assume that anyone from a country top-level-domain will be wanting to view your site in that countries language. As a commenter already stated; what about foreign speakers abroad? What about certain people who are trying to learn another language and wanting to view content in that language?? What about countries where more than once language is standard? A country is NOT a language.
The best option for you to do (if you truly want a localized site) is to allow the user to CHOOSE his or her viewing language.
Allow a dropdown or other method for a user to select their language, and use PHP's Gettext module to help with your content translation.
1-create a class with php like this
class Language
{
function __construct($lan)
{
require_once($lan . '.php');
}
}
2-create files like en.php or jp.php an then in each lang files use class variables (en.php):
$this->firstpage="HOME";
$this->login="LOGIN";
3-use class in main page
$language = new language($_POST["language"]);
4-use php variables in content like this
<a href=""> <?php $language->firstpage; ?> </a>
for little sites this is a good way to use php for change language;
you can do this by
<a href="index.php?language=en">
<a href="index.php?language=no">
and get the languages and store them in cookie and include file according to cookie like
if ( !empty($_GET['language']) ) {
$_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'nl';
} else {
$_COOKIE['language'] = 'nl';
}
setcookie('language', $_COOKIE['language']);
and than
if ( $_COOKIE['language'] == "en") {
include("headerEn.php");
} else {
include("header.php");
} ?>
To give a solution without changing your approach, You can do like this.
<?php
if(isset($_GET['language']))
$language = $_GET['language'];
else
$language = "";
if ($language == "en") {
include("headerEn.php");
} else {
include("header.php");
} ?>
<a href="index.php?language = en"><img src="images/language/languageNO.png"> </a>
<a href="index.php?language = no"><img src="images/language/languageEN.png"></a>
If you want to keep the selection, you can store this value in Database or Session.
It is always good to have a default value, so that you never end up being in a no-language site.
$language = $_REQUEST["language"];
$default_header="myheaderXXX.php";
switch ($language) {
case "en":
include("headerEn.php");
break;
case "no":
include("header.php");
break;
default:
include($default_header);
}
And then create links like this:
<a href="index.php?language=en">
<a href="index.php?language=no">
You can implement the same code like this . I have editted your code .
<?php
$language; ?>
<?php if ($language == "en") : ?>
<?php include("headerEn.php"); ?>
<a href="index.php"><?php $language = "en"; ?><img src="images/language/languageNO.png"></a>
<?php else: ?>
<?php include("header.php"); ?>
<a href="index.php"><?php $language = "no"; ?><img src="images/language/languageEN.png"></a>
<?php endif; ?>
this will solve your problem .
Post a Comment