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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog