doubeishuai6598 2017-11-13 12:20
浏览 39
已采纳

用于翻译内容的PHP代码段。 这是一个好习惯吗? 可以变得更好/更简单吗?

I am currently developing a website that will have translated content served by different gTLD. (example.com, example.fr, example.de)

The idea is to have them all redirect to the same content on the same server, and based on the gTLD, have a simple php routine that serves the correct language.

Here is the code I am using to detect the domain/language:

<?php

$url_tld = explode("." , $_SERVER['HTTP_HOST'] , 3);
if ($url_tld[2] == "fr") { $language = "FR"; $lang_id = 1;}
elseif ($url_tld[2] == "de") { $language = "DE"; $lang_id = 2;}
elseif ($url_tld[2] == "nl") { $language = "NL"; $lang_id = 3;}
else { $language = "EN"; $lang_id = 0;}

function txt($x) {
    global $lang_id;
    echo $x[$lang_id];
};

In the html code I run txt function whenever I need different language content; example:

<h1><?=txt(['Hello World','Bonjour France','Hallo Deutschland','Hallo Nederland'])?></h1>

I am trying to avoid using any database as the website will be mostly static.

Can this approach get simpler? Or more eye friendly (for those updating code manually)?

Best regards and thanks in advance.

  • 写回答

2条回答 默认 最新

  • doushao8399 2017-11-13 13:02
    关注

    I don't like the 3 in your explode() and the 2's in your indexes. What is it based on? What if the domain is example.co.uk or www.example.fr? Better use:

    $domainExtension = end(explode(".",$_SERVER['HTTP_HOST']));
    

    Then you can do:

    switch ($domainExtension) {
      case 'fr' : $language = 'FRE'; break;
      case 'de' : $language = 'GER'; break;
      case 'nl' : $language = 'DUT'; break;
      default   : $language = 'ENG'; break;
    }
    

    As you can see the switch () makes the code more readable. I don't abbreviate unnecessarily, for better readability. I also used the official three-letter abbreviation for languages. It's a bit more versatile, but two letters will do, see:

    https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes

    Personally I don't like numbers for languages, that's why I left them out, although I understand why you used them. You can do:

    $texts = ['hello','Bonjour','Guten Tag','Hallo'];
    

    but in that case you cannot leave out any languages. I would forget the numbers and do:

    $texts = ['ENG' => 'hello',
              'FRE' => 'Bonjour',
              'GER' => 'Guten Tag',
              'DUT' => 'Hallo'];
    

    Then, when a language is missing, you can replace it by another:

    function getText($language,$texts) {
      if (isset($texts[$language]) return $texts[$language];  // it is there
                              else return reset($texts);      // take the first
    };
    

    You can then also sort the languages from most commonly used to rarely used. Note that I don't use a global, which is on purpose. Again, you can choose to use one.

    Finally, to get back to the simpler list of texts you could use another function:

    function setTexts($english,$french,$german,$dutch)
    {
      return ['ENG' => $english,
              'FRE' => $french,
              'GER' => $german,
              'DUT' => $dutch];
    }
    
    $helloTexts = setTexts('hello','Bonjour','Guten Tag','Hallo');
    

    and then used like this:

    echo getText($language,$helloTexts);
    

    But I have to stress that there are many other ways of doing this. You could, for instance, have separate include files for the various languages. That way you don't process any texts that aren't used. It might save a bit of time, but it will be minimal, and it is nice to keep the different translations of the same piece of text together.

    One last tip: The broswer can tell you the preferred language of the visitor. See: $_SERVER['HTTP_ACCEPT_LANGUAGE'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog