dongpan1365 2013-09-14 17:29
浏览 24
已采纳

多语言php应用程序

Currently I have my php application running on english. We have to now cater for few other languages. We notice some do a whole set of translation of the all the form and put in a new folder label according to the translated language. Is that an optimize method?

  • 写回答

5条回答 默认 最新

  • dounianluo0086 2013-09-14 17:54
    关注

    I'm still for the idea of database.

    Let's say you have input field, next to it there is "Username" in english, it's value by default is "Enter your username". You can have something like:

    <td><?=$obj->translate('username');?></td>
    <td><input type="text" name="username" value="<?=$obj->translate('enter_username');?>" /></td>
    

    So you have i.e. in your db table translations:

    id | placeholder     |         en          |    pl                      |
    -------------------------------------------------------------------------
    1  | username        |      Username       | Nazwa użytkownika          |
    2  | enter_username  | Enter your username | Wprowadź nazwę użytkownika |
    

    So your client is polish, you set him $_SESSION['language'] = 'pl'

    You build function/method translate(), which returns the current language translation.

    public function translate($word) {
        $sql = "SELECT $_SESSION['language'] FROM translations WHERE placeholder = '$word';";
        $this->db->query($sql);
        return $this->db->result();
    }
    

    So basicly, when you run $obj->translate('enter_username'); for your Polish client, you make query like this:

    SELECT pl FROM translations WHERE placeholder = 'enter_username';
    

    which returns 'Wprowadź nazwę użytkownika'

    Everytime you need something to be printed, you have to insert it's placeholder in your db, and later or immediately it's translations.

    If you need and error alert via javascript, for wrong username for example, you add a placeholder in the db i.e. 'wrong_username_from_login_form" lately translated in English as "You have entered wrong username. Please go back and try again".

    You can have:

    <script>
    alert("<?=$obj->translate('wrong_username_from_login_form');?>");
    </script>
    

    If your client is English, he will have alerted on its screen: "You have entered wrong username. Please go back and try again"

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

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角