如何刪除Codeigniter中lang()的自動標籤環繞。刪除Codeigniter標籤環繞lang()
本手冊不說什麼:http://ellislab.com/codeigniter%20/user-guide/helpers/language_helper.html
我必須由我自己來寫一個函數或者是有一個簡單幹淨的方式,我在想念?
如何刪除Codeigniter中lang()的自動標籤環繞。刪除Codeigniter標籤環繞lang()
本手冊不說什麼:http://ellislab.com/codeigniter%20/user-guide/helpers/language_helper.html
我必須由我自己來寫一個函數或者是有一個簡單幹淨的方式,我在想念?
不寫第二個參數。保持空着。
看看郎功能(發現地:/system/helpers/language_helper.php
):
function lang($line, $for = '', $attributes = array())
{
$CI =& get_instance();
$line = $CI->lang->line($line);
if ($for !== '')
{
$line = '<label for="'.$for.'"'._stringify_attributes($attributes).'>'.$line.'</label>';
}
return $line;
}
正如你可以看到它需要3個參數。第一個參數是必需的,但後兩個是可選的。如果你聲明第二個參數,它將返回包裝在標籤中的語言字符串。
因此,說明只有第一個參數應使其輸出只是語言字符串。
從閱讀您的評論聽起來好像你會更好使用language class直接。然而,僅僅語言課程還不夠,你需要爲了你的目的而擴展它。爲此,您可以在名爲MY_lang.php
的application/core
文件夾中創建一個新文件。
class MY_Lang extends CI_Lang {
// You want to extend the line function
function line($line = '', $value = '')
{
$line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
// We can assume that if a value is passed it is intended to be inserted into the language string
if($value) {
$line = sprintf($line, $value);
}
// Because killer robots like unicorns!
if ($line === FALSE)
{
log_message('error', 'Could not find the language line "'.$line.'"');
}
return $line ;
}
}
假設你的語言文件有一個字符串,像這樣:
$name = "foo";
$this->lang->line('welcome_text', $name);
以上:
$lang['welcome_text'] = "Welcome %s";
然後,您可以通過加載語言類,並使用下面的代碼中使用此是100%未經測試的,所以它可能需要一些tweeking,但它應該給你一個開始的地方。
感謝您的回答。事情是我需要添加第二秒,因爲Im使用以下類型的「歡迎%s」,其中%s是名稱的動態值。和代碼像'lang('welcome_text',$ name);'我最好的選擇是去一個自定義的功能? – JohnSmith
用一個簡單的例子更新了我的答案,當你需要傳遞一個值時,如何擴展字符串的語言類。 – Jeemusu