dttvb115151 2013-10-07 14:57
浏览 52
已采纳

在oo php中加载语言文件

So i am new to oo php and i-m building a sample app for learning purpses , one thing i must do is load a language file according to some settings The code, as you will discover below is divided between two classes , a settings class witch should load the language file and another class in this case "contact" witch should read the array in the language files and display the propper message. this is the Settings class the lang variable sets the default language it can take 2 values at the moment : ro- for romanian and en - for english ,

    class Settings
{
  static public $lang = 'ro';
  static public $load;

  static public function get_language()
   {
    if(self::$lang == 'ro')
    {
      self::$load = require 'ro.php';
    }
    elseif(self::$lang == 'en')
    {
     self::$load = require 'en.php';
    }
    return self::$load;
   }

}

The second class :

class Contact extends Settings {
   //proprietati

   public $nume;
   public $subiect;
   public $mesaj;
   public $dincs;


   //comportament - metode

   public function __construct()
   {
     //$this->dincs = 'Din construct';
     parent::get_language();
   }


   public function write_file()
   {

     if(empty($this->nume))
     {
       return $mess['name_error'];
     }
     else
     {
       $fp = fopen('data.txt', 'w');
       fwrite($fp, $this->nume.".".$this->subiect .".". $this->mesaj."|".$this->dincs ."|".parent::$load);
       fclose($fp);

       return $mess['file_written'];
     }
   }

}

A sample from the language file:

   $mess = array ("name_error"  => "You must insert your name",
                  "file_written" => "the file has been written",

   );

I have looked up on google , and tried some other stuff and can't seem to get it to work , and that may be because i am approaching this problem incorectly. Plese help.

  • 写回答

2条回答 默认 最新

  • douchanxiu5636 2013-10-07 17:49
    关注

    `

    class Settings
    {
        protected $language = 'ro';
        protected $load;
    
        public function setLanguage($language = 'ro')
        {
            $this->language = $language;
    
            // file_get_contents()
            $this->load = require($this->language . '.php');
        }
    
        public function getLanguage()
        {
            return $this->language;
        }
    
    }
    
    class Contact extends Settings 
    {
       protected $name;
       protected $subject;
       protected $message;
       protected $dincs;
       protected $settingsObject;
    
       // set all the properties below...
    
       public function setName($name)
       {
            $this->name = $name;
       }   
    
       public function setSubject($subject)
       {
            $this->subject = $subject;
       }   
    
       public function setMessage($message)
       {
            $this->message = $message;
       }   
    
       public function setDincs($dincs)
       {
            $this->dincs = $dincs;
       }
    
       // get all the properties...
       public function getName()
       {
            return $this->name;
       }
    
       // This function only accepts an instance of Settings as the parameter
       public function setSettingsObject(Settings $settings)
       {    
            $this->settingsObject = $settings;
       }
    
       public function writeContentsToFile()
       {
            // if there is nothing in name, throw a new exception
            if (empty($this->name))
            {
                throw new Exception('name has not been set! Set name before calling '. __METHOD__);
            }
            else
            {
               // get the file
               $fp = fopen('data.txt', 'w');
    
               // formatted string
               $contents = sprintf('%s . %s . %s | %s | %s', $this->name, $this->subject, $this->message, $this->dincs, $this->settingsObject->getLanguage());
    
               // write to the file
               fwrite($fp, $contents);
    
               // close the handler
               fclose($fp);
    
               return ('File written! Contents written: ' . $contents);
            }
        }
    }
    
    
    // instantiate settings
    $settings = new Settings();
    $settings->setLanguage('en');
    
    // instantiate contact and set the settings object
    $contact = new Contact();
    $contact->setName('Joe Smith'); // if this is not set, ::writeContentsToFile will throw an Exception
    $contact->setSettingsObject($settings);
    
    // try and catch the Exception that ::writeContentsToFile may throw
    try 
    {
        echo $contact->writeContentsToFile();
    }
    catch (Exception $exception)
    {
        var_dump($exception);
    }
    

    ?> `

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

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动