duangou1953 2012-05-09 14:58
浏览 33
已采纳

将变量绑定到Kohana消息

Is there a way to pass through a variable to Kohana messages, which can then be picked up by the il8n translation e.g.:

Kohana::message('user', 'greeting')

messages/user.php:

return array
(
    'greeting' => __('user.greeting', array(':user' => $name ))
);

i18n/en.php:

return array
(
    'greeting' => 'Hello, :user'
);

I grabbed the method of linking the il8n to message from i18n and Error messages in Kohana 3

  • 写回答

1条回答 默认 最新

  • dpgua04022 2012-05-10 09:30
    关注

    I don't have the exact answer you are looking for, but I could suggest a solution if you don't mind overwriting the current Kohana::message() method. I have done something similar in my own app.

    Just create a Kohana.php file in your 'applications/classes' with the following code:

    class Kohana extends Kohana_Core {
    
        public static function message($file, $path = NULL, $default = NULL, $replacements = array())
        {
            static $messages;
    
            if ( ! isset($messages[$file]))
            {
                // Create a new message list
                $messages[$file] = array();
    
                if ($files = Kohana::find_file('messages', $file))
                {
                    foreach ($files as $f)
                    {
                        // Combine all the messages recursively
                        $messages[$file] = Arr::merge($messages[$file], Kohana::load($f));
                    }
                }
            }
    
            if ($path === NULL)
            {
                // Return all of the messages
                $message = $messages[$file];
            }
            else
            {
                // Get a message using the path
                $message = Arr::path($messages[$file], $path, $default);
            }
    
            return !empty($replacements) ? strtr($message,$replacements) : $message;
        }
    }
    

    Basically the only notable change is the addition of $replacements argument and return !empty($replacements) ? strtr($message,$replacements) : $message; which replaces the contents of your message with those that are in your replacements array.

    So you can now do this:

    Kohana::message('user', 'greeting', NULL, array(':user' => $name));

    messages/user.php:

    return array
    (
        'greeting' => __('user.greeting'),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计