【问题标题】:Codeigniter: Variables/Config Items within Language FilesCodeigniter:语言文件中的变量/配置项
【发布时间】:2009-11-11 19:53:15
【问题描述】:

我有一个语言文件,其中包含用于我的视图文件的一长串字符串。 我的问题是如何将变量或配置项传递给语言文件?

<?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 传递给它。我只是不知道怎么做。

谢谢!

【问题讨论】:

    标签: php codeigniter variables config


    【解决方案1】:

    在 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】:

    您最好的选择可能是在其中放置一个占位符,然后在您的控制器中将其换掉。

    语言文件:

    <?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);
    

    【讨论】:

    • 我在整个语言文件中多次引用 base_url。重复会有问题吗?
    • 不太清楚你的意思。哪里重复?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多