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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分