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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动