dongshou7903 2014-03-30 22:12
浏览 40
已采纳

用多种语言翻译网站[关闭]

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?

  • 写回答

2条回答 默认 最新

  • douwu0335 2014-03-30 22:59
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上