dongxie7683 2011-02-20 14:30
浏览 10
已采纳

管理PHP中向用户显示的文本的最佳方法

Ok for sure this has been asked and answered already but i somehow can't find a proper tutorial. I want to keep the text displayed to users somewhere else and to prevent my code from becoming too large and unreadable. My site won't be internationalized. I just want to have some kind of file with key-value structure and get the text from there. I want to keep the text in files, not in the database as some tutorials suggest. I found a solution which will work but i am not sure whether this is a good approach. I am thinking of using parse_ini_file and to keep my texts in .ini file. Is there something wrong with this approach? Could you suggest something better?

  • 写回答

5条回答 默认 最新

  • drcmg28484 2011-02-20 14:47
    关注

    I put all language data in arrays. Its easy and also we can add multi-language support

    lang/en.php

    <?php 
     return array(  
       'index' => 'Homepage',
       'feedback' => 'Feedback'
       'logout' => 'Logout from profile',
     )
    ?>
    

    lang/ru.php

    <?php 
     return array(
       'logout' => 'Выйти из профиля',
     )
    ?>
    

    Then we can load languages:

    $lang = include('lang/en.php');
    if(isset($_GET['lang']))
    {
       $lang = array_merge($lang, include('lang/ru.php'));
    }
    

    After all it $lang will look like:

    Array
    (
       [index] => Homepage
       [feedback] => Feedback
       [logout] => Выйти из профиля
    )
    

    And we can very simple use it:

    function __($name) {
        global $lang;
        return $lang[$name];
    }
    

    Somewhere in the site template:

    ...
    <title><?=__('index')?></title>
    </head>
    <body>
    <?=__('feedback')?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据