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条)

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件