2009-11-11 39 views
2

我有一個語言文件,其中有一長串的字符串用於查看我的文件。 我的問題是如何將變量或配置項傳遞給語言文件?Codeigniter:語言文件中的變量/配置項

<?php $lang['error_activation_key_expired'] = 'The activation key you have attempted using has expired. Please request a new Activation Key <a href="'.$this->config->item('base_url').'member/request_activation" title="Request a New Activation Key">here</a>.'; 

我能安定爲

<?php $lang['error_activation_key_expired'] = 'The activation key you have attempted using has expired. Please request a new Activation Key <a href="'.$base_url.'member/request_activation" title="Request a New Activation Key">here</a>.'; 

和BASE_URL某種方式傳遞給它。我只是不知道如何。

謝謝!

回答

4

你最好的辦法是把一個佔位符放在那裏,然後在你的控制器中交換它。

朗文件:

<?php $lang['error_activation_key_expired'] = 'The activation key you have attempted using has expired. Please request a new Activation Key <a href="%s/member/request_activation" title="Request a New Activation Key">here</a>.'; 

在控制器:

$activation_message = sprintf($this->lang->line('error_activation_key_expired'), $base_url); 
+0

我的BASE_URL多次引用遍及語言文件。重複會有問題嗎? – 2009-11-11 20:16:08

+0

不完全確定你的意思。重複在哪裏? – Thody 2009-11-11 20:24:10

+0

對不起,nm。 您的解決方案非常完美。謝謝! – 2009-11-11 20:29:19

4

在CI論壇,nlogachev提出更好的解決方案。
它可以切換變量順序。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Language extends CI_Language 
{ 

    function MY_Language() 
    { 
     parent::CI_Language(); 
    } 

    /** 
    * Fetch a single line of text from the language array. Takes variable number 
    * of arguments and supports wildcards in the form of '%1', '%2', etc. 
    * Overloaded function. 
    * 
    * @access public 
    * @return mixed false if not found or the language string 
    */ 
    public function line() 
    { 
     //get the arguments passed to the function 
     $args = func_get_args(); 

     //count the number of arguments 
     $c = count($args); 

     //if one or more arguments, perform the necessary processing 
     if ($c) 
     { 
      //first argument should be the actual language line key 
      //so remove it from the array (pop from front) 
      $line = array_shift($args); 

      //check to make sure the key is valid and load the line 
      $line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line]; 

      //if the line exists and more function arguments remain 
      //perform wildcard replacements 
      if ($line && $args) 
      { 
       $i = 1; 
       foreach ($args as $arg) 
       { 
        $line = preg_replace('/\%'.$i.'/', $arg, $line); 
        $i++; 
       } 
      } 
     } 
     else 
     { 
      //if no arguments given, no language line available 
      $line = false; 
     } 

     return $line; 
    } 


} 

?> 

用例〜

//in english 
$lang['some_key'] = 'Some string with %1 %2.'; 

//in another language 
$lang['some_key'] = 'Some string %2 with %1.'; 

//the actual usage 
echo $this->lang->line('some_key','first','second'); 
+2

完美答案。在我看來,你的答案必須被接受。 – 2013-07-13 21:44:12