dongyuzhu2244 2012-01-19 12:32
浏览 95
已采纳

PHP / MySQL web-app使用枚举数据库字段进行国际化

I have joined a project recently and now I'm working on its Internationalization improvement. Technologies used are PHP/MySQL/Zend Framework/Dojo. I18n is implemented using gettext almost as described here link to SO question in the second answer.

But I encountered one problem. Some part of the information specific to certain DB tables is stored within those tables in the enum type columns. For example there is a field usr_online_status in the table "user" which could be one of either 'online' or 'offline'. There are many such tables with enum fields which contain info like ('yes' ,'no') ,('download', 'upload') and so on. Of course this info is displayed in English regardless of the current Language chosen by user.

I would like to solve this inconvenience. But don't know what is the best way to do this in terms of performance and ease of implementation. I see two possible options:
1) Make language specific dictionary tables for each table which uses such enums.
2) Download all the info from enums. Translate it. Make a script which could on demand alter every table and replace those enums with the required translations.

But there may be simpler or better solutions for this problem.

What would you do ?

Thanks for your answers.

UPD1 Important remark. Info from the enums is not only displayed at the GUI but is used in search. For example - there is a grid on a webpage which contains info about users. You can type 'line' in a search field and the result will be only those users with the word '%line%' in their info, for example 'online' status.

  • 写回答

1条回答

  • douxuanpa8298 2012-01-19 12:40
    关注

    You definitly want dictionary tables: Only with these can 2 different users of the app work in different languages at the same time.

    I recommend to put some of these dictionary tables into PHP though, as this has proven to be quite an unintrusive and performant way of doing it - e.g.

    $translation=array('yes'=>'Ja','no'=>'Nein', ..)
    
    //...
    
    $row=mysql_fetch_row($qry);
    //$row[1] has yes/no
    $row[1]=$translation[$row[1]];
    
    //...
    

    $translation could be require_once()'ed depending on the current user's language preferences, the URL or whatever

    Basically you trade some RAM for speed and easyness.

    UPDATE:

    With Gior312 adding the info about search, here is my solution for it: Have the reverse translation in a DB table (you even might use it to create $translation per a script):

    CREATE TABLE translations (
      id INT PRIMARY KEY AUTO_INCREMENT,
      languageid INT NOT NULL,
      enumword VARCHAR(m) NOT NULL,
      langword VARCHAR(n) NOT NULL,
      -- n and m to your needs
      INDEX(languageid)
      -- other indices to your needs
    )
    

    Now when the search up until now was

    $line=... //Maybe coming from $_POST['line'] via mysql_real_escape_string()
    $sql="SELECT * FROM sometable WHERE somefield LIKE '%$line%'";
    

    What you now do is

    $line=... //Maybe coming from $_POST['line'] via mysql_real_escape_string()
    $sql="SELECT enumword FROM translations WHERE languageid=$currentlanguageid AND langword LIKE '%$line%'";
    //fetch resulting enumwords into array $enumwords
    $enumlist=explode("','",$enumwords);
    //This assumes, that the field enumwords contains nothing, that needs to be escaped
    $sql="SELECT * FROM sometable WHERE somefield IN ('$enumlist')";
    

    The rationale behind treating forward and back translation differently is:

    • There will be many more lines in the code where you display, than where you search, so the unintrusiveness of the forward translation is more important
    • The forward trnslation has to be done PER ROW (with a join), the reverse only PER QUERY, so the performance of the forward translation is more important than the performance of the reverse translation
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的