duanguangsong2380 2014-01-22 07:30
浏览 74
已采纳

多语言网站 - PHP类 - 正确的方法是什么? [关闭]

I have just starter to use PHP OOP and I would like to write a class to make a multi-language website. I started from this but I wanted to use OOP so I came up with this:

Language.php

<?php

class Language {

    private $UserLng;
    private $langSelected;
    public $lang = array();


    public function __construct($userLanguage){

        $this->UserLng = $userLanguage;
    }

    public function userLanguage(){

        switch($this->UserLng){
            /*
            ------------------
            Language: English
            ------------------
            */
            case "en":
                $lang['PAGE_TITLE'] = 'My website page title';
                $lang['HEADER_TITLE'] = 'My website header title';
                $lang['SITE_NAME'] = 'My Website';
                $lang['SLOGAN'] = 'My slogan here';
                $lang['HEADING'] = 'Heading';

                // Menu

                $lang['MENU_LOGIN'] = 'Login';
                $lang['MENU_SIGNUP'] = 'Sign up';
                $lang['MENU_FIND_RIDE'] = 'Find Ride';
                $lang['MENU_ADD_RIDE'] = 'Add Ride';
                $lang['MENU_LOGOUT'] = 'Logout';

                return $lang;
                break;

                /*
                ------------------
                Language: Italian
                ------------------
                */

            case "it":
                $lang['PAGE_TITLE'] = 'Il titolo della mia pagina';
                $lang['HEADER_TITLE'] = 'Il mio titolo';
                $lang['SITE_NAME'] = 'Il nome del mio sito';
                $lang['SLOGAN'] = 'Uno slogan';
                $lang['HEADING'] = 'Heading';

                // Menu

                $lang['MENU_LOGIN'] = 'Entra';
                $lang['MENU_SIGNUP'] = 'Registrati';
                $lang['MENU_FIND_RIDE'] = 'Trova gruppi';
                $lang['MENU_ADD_RIDE'] = 'Aggiungi gruppo';
                $lang['MENU_LOGOUT'] = 'Esci';

                return $lang;
                break;

        }
    }
}

index.php

<?php 
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);//Detecting Default Browser language
$language = New Language($lang);
$langArray = array();
$langArray =  $language->userLanguage();
?>

<div class="cssmenu">
    <ul>
        <li> class="active"><a href="/login"><?php echo $langArray['MENU_LOGIN']?></a></li>                                                                                         
        <li><a href="/rides"><?php echo $langArray['MENU_FIND_RIDE']?></a></li>                                               
        <li><a  id ="btnShow"><?php echo $langArray['MENU_ADD_RIDE']?></a></li>                                              
        <li><a href="/logout.php"><?php echo $langArray['MENU_LOGOUT']?> </a></li>                                                   
        <li><a href="/register"><?php echo $langArray['MENU_SIGNUP']?></a></li>
    </ul>
</div>

While this perfectly works, I am not sure this is the correct way to do it. I didn't find many tutorials on how to do this using OOP so I have the following doubts:

  1. Is this a correct way to do it?
  2. is this code maintainable?
  3. would it make more sense to create a table in the database with all the different languages?
  4. I am still struggling to understand "abstract classes". Would this be the case to create an abstract class language.php and then extend(I hope this is the correct terminology) that class with other languages class (english.php, italian.php etc).
  • 写回答

2条回答 默认 最新

  • dongxian5735 2014-01-26 03:07
    关注

    You are very much on track, except for one bit, your data and code are intertwined, which is not a good form of design. Different people use various approaches for handling i18N. I prefer using .ini files, you can have the data in different INI files and even add sections if you want. Using your example, you can do:

    -- en.ini

    PAGE_TITLE = 'My website page title'
    HEADER_TITLE = 'My website header title'
    SITE_NAME' = 'My Website'
    SLOGAN = 'My slogan here'
    HEADING = 'Heading'
    

    -- it.ini

    PAGE_TITLE = 'Il titolo della mia pagina'
    HEADER_TITLE = 'Il mio titolo'
    SITE_NAME = 'Il nome del mio sito'
    SLOGAN = 'Uno slogan'
    HEADING = 'Heading'
    

    Then your code can be rewritten as :

    <?php
    
    class Language {
    
    private $UserLng;
    private $langSelected;
    public $lang = array();
    
    
    public function __construct($userLanguage){
    
        $this->UserLng = $userLanguage;
        //construct lang file
        $langFile = '/path/to/ini/files/'. $this->UserLng . '.ini';
        if(!file_exists($langFile)){
            throw new Execption("Language could not be loaded"); //or default to a language
        }
    
        $this->lang = parse_ini_file($langFile);
    }
    
    public function userLanguage(){
        return $this->lang;
    }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)