dtkago3208 2011-11-26 21:02
浏览 45
已采纳

开发多语言系统

This is more of an analytical question.

I need to know how best to make a multilingual system, a.k.a. a system where the user can change the language. The language will be stored in a cookie or a database.
I've worked in the past with different files for each language, for example:

nl.php

$lang['hi'] = 'Hoi';
$lang['howareyou'] = 'Hoe gaat het?';

en.php

$lang['hi'] = 'Hi'];
$lang['howareyou'] = 'How are you?';

index.php

include($language . '.php');

As you can see, this system is both inefficient and insecure. Is there a better way to do it? I can think of a few ways to do this this instant, but I don't really know which one would be good.

Can anyone help me with this? Please don't just say "Do it like this!", also tell me why it is a good way to do it.

  • 写回答

2条回答 默认 最新

  • dongmo1708 2011-11-26 21:09
    关注

    Well, if you don't need to provide ability to change localization texts via web interface, you can just do it like this:

    include/locales.php

    <?php
        if (!isset($locales)) {
            $locales = array(
                "en" => array(
                    "hi" => "Hi",
                    "howareyou" => "How are you?"
                ),
                "nl" => array(
                    "hi" => "Hoi",
                    "howareyou" => "Hoe gaat het?"
                )
            );
        }
    ?>
    

    index.php

    include("include/locales.php");
    if (!isset($locales[$language])) $locale = $locales[$deflang]; // $deflang could be "en"
    else $locale = $locales[$deflang];
    
    echo $locale["hi"]." ".$locale["howareyou"];
    

    This is the fastest approach, because parsing single include file with hash is very fast operation. If you need to provide ability to modify localization strings via web interface, then you will need to store localization strings in DB and read em from there each time you show a page... This approach is way more slow.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分