-4

My site have an folder language where have:

en.php

<?
$lang = array(

    'sp1' => 'Welcome',
    'sp2' => 'Home',

);
?>

and it.php

<?
    $lang = array(

        'sp1' => 'Benvenuto',
        'sp2' => 'A casa',

    );
    ?>

In the index.php i have like:

<h4><?=$lang['sp1']?></h4>
<a><u><strong><?=$lang['sp2']?></a></u></strong><br />

But this is an option for change the language from cpanel, how I can transform to the geo language.. when an italian visit my site can view my site in italian language, etc?

  • 1
    You can query the user_agent accepted language and change which array to use depending on what the accepted language returns as, have a read of http://stackoverflow.com/questions/620276/checking-browsers-language-by-php – Pheonix2105 Mar 30 '14 at 22:15
  • http://www.php.net/manual/en/book.geoip.php ? – Scuzzy Mar 30 '14 at 22:15
  • 2
    This question is either much too broad (translation, or "i18n", is a huge topic) or not very clear: are you asking how to detect the user's preferred language? What does cpanel have to do with this? – IMSoP Mar 30 '14 at 22:15
  • Can you write the code exactly and where to put it, because I'm not very good at php :( – user3462476 Mar 30 '14 at 22:18
  • Check @Companjo's answer below it will do what you want :), you want to put this before you use the $lang variable, near the top of your page would be best. So what it would do then is first check the browser users language if its 'en-us' loading the english language array then in your page $lang['sp1'] would be whatever $lang['sp1'] is in en.php if it detects italian language then $lang['sp1'] would be whatever is in it.php $lang['sp1'] :) – Pheonix2105 Mar 30 '14 at 22:19
  • @IMSoP - yes how to detect the user's preferred language.. in my cpanel is an option change the default language, here is 2 language.. it and en – user3462476 Mar 30 '14 at 22:20
  • @Pheonix2105 - where is the Companjo's answer? – user3462476 Mar 30 '14 at 22:22
  • @user3462476 Try pressing CTRL+F5 to force the page to refresh, he has posted the code you'll need already :) – Pheonix2105 Mar 30 '14 at 22:25
  • I think the cpanel option is irrelevant: that's the language you want that piece of software to be in. If you're looking for how they do that, I don't know, but it's likely much more complex than you need. – IMSoP Mar 30 '14 at 22:57

2 Answers2

1

You can use $_SERVER['HTTP_ACCEPT_LANGUAGE']

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "fr":
        //echo "PAGE FR";
        include("fr.php");
        break;
    case "it":
        //echo "PAGE IT";
        include("it.php");
        break;
    case "en":
        //echo "PAGE EN";
        include("en.php");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        include("en.php");//include EN in all other cases of different lang detection
        break;
}
?>
Companjo
  • 1,789
  • 18
  • 24
  • While a useful tool, this should not be used alone, and the user should always be given an explicit choice of language as well, as discussed in [this W3C document](http://www.w3.org/International/questions/qa-when-lang-neg) – IMSoP Mar 30 '14 at 22:27
  • I agree, but i think it would be a little bit to complicated for @user3462476 – Companjo Mar 30 '14 at 22:32
  • The complexity of good advice is no excuse for giving bad advice; implemented alone, this will lead to terrible usability. Besides, it doesn't need to be *much* more complex than this: just use this to pick a default language, and let the user click a link to change it, with their choice saved in a cookie. – IMSoP Mar 30 '14 at 22:34
  • In another site is working but in the site swapes .com no... why? I adedd this in the same pages. – user3462476 Mar 30 '14 at 23:16
0

Browsers advertise the user's "preferred" language in an HTTP header called Accept-Language, which you can read in PHP as $_SERVER['HTTP_ACCEPT_LANGUAGE'].

There is actually a fairly complex structure to this header, allowing multiple languages to be given priorities, and the language itself can take a few different forms. The answers to this previous question discuss how to parse it fully.

But as a first approximation, you can just take the first two letters, which will generally be one of the ISO 639-1 codes listed here by the US Library of Congress or here on Wikipedia:

$default_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

However, this should never be the only way of selecting the language. As explained fairly clearly by this W3C document, the browser might not be correctly configured, or the user might not be using their own computer, so that it would not be sending the correct value.

Instead, you should treat this as a default, and allow the user to over-ride it. This can be as simple as providing links on every page which add ?lang=it (etc) to the current URL. Then set a cookie containing their preference, and ignore the accept-language from then on:

if ( $_GET['lang'] ) {
    // User asked for a particular language; obey, and remember in a cookie
    setcookie('lang', $_GET['lang'], 0, '/');
    $preferred_language = $_GET['lang'];
} elseif ( $_COOKIE['lang'] ) {
    // Cookie found from previous selection
    $preferred_language = $_COOKIE['lang'];
} else {
    // Guess based on browser settings
    $preferred_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
Community
  • 1
  • 1
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • but how add the php file: it.php and en.php – user3462476 Mar 30 '14 at 23:27
  • @user3462476 Well, once you run this code, you have a variable `$preferred_language`. If it is `'it'`, then include `it.php`; if not, fall back to including the English strings; when you add, say, French, you can also check if it is `'fr'`, and so on. – IMSoP Mar 30 '14 at 23:30
  • how include... you can write code completely.. because I don't understand very well – user3462476 Mar 30 '14 at 23:36
  • @user3462476 Sorry, but if you want me to write your code, pay me. If you can't write PHP code equivalent to the sentence "If `$preferred_language` is `'it'` then include `it.php` otherwise include `en.php`" then you are going to really struggle to write anything yourself. – IMSoP Mar 30 '14 at 23:41
  • the default need en.php and when an italian visit the site need it.php – user3462476 Mar 30 '14 at 23:41