扩展具有静态方法的类

Iamjonesy

我想在模块中扩展KOHANA的i18n类,这样我可以在查找默认文件结构之前先查找数据库以查找翻译。问题是我要覆盖的方法是静态的。

原始类有一个方法,get()所以我称呼我的类:Appointedd_I18n::get('Term goes here...')而那个方法叫load()那就是我要覆盖的方法,但是因为它是静态的,所以没有加载MY方法,而是加载了原始方法。

这是我的模块/类:

<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Extends the Kohana translation code to include a db lookup. 
 *
 */

class Appointedd_I18n extends Kohana_I18n {

    /**
     * Returns the translation table for a given language.
     *
     *     // Get all defined Spanish messages
     *     $messages = I18n::load('es-es');
     *
     * @param   string  $lang   language to load
     * @return  array
     */
    public static function load($lang)
    {
        die('think I\'ll look up the db'); // to test this method is being called
        if (isset(I18n::$_cache[$lang]))
        {
            return I18n::$_cache[$lang];
        }

        // New translation table
        $table = array();

        // Split the language: language, region, locale, etc
        $parts = explode('-', $lang);

        do
        {
            // Create a path for this set of parts
            $path = implode(DIRECTORY_SEPARATOR, $parts);

            if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
            {
                $t = array();
                foreach ($files as $file)
                {
                    // Merge the language strings into the sub table
                    $t = array_merge($t, Kohana::load($file));
                }

                // Append the sub table, preventing less specific language
                // files from overloading more specific files
                $table += $t;
            }

            // Remove the last part
            array_pop($parts);
        }
        while ($parts);

        // Cache the translation table locally
        return I18n::$_cache[$lang] = $table;
    }

} // END class Appointedd_i18n

这是KOHANA_I18n类:

<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Internationalization (i18n) class. Provides language loading and translation
 * methods without dependencies on [gettext](http://php.net/gettext).
 *
 * Typically this class would never be used directly, but used via the __()
 * function, which loads the message and replaces parameters:
 *
 *     // Display a translated message
 *     echo __('Hello, world');
 *
 *     // With parameter replacement
 *     echo __('Hello, :user', array(':user' => $username));
 *
 * @package    Kohana
 * @category   Base
 * @author     Kohana Team
 * @copyright  (c) 2008-2012 Kohana Team
 * @license    http://kohanaframework.org/license
 */
class Kohana_I18n {

    /**
     * @var  string   target language: en-us, es-es, zh-cn, etc
     */
    public static $lang = 'en-us';

    /**
     * @var  string  source language: en-us, es-es, zh-cn, etc
     */
    public static $source = 'en-us';

    /**
     * @var  array  cache of loaded languages
     */
    protected static $_cache = array();

    /**
     * Get and set the target language.
     *
     *     // Get the current language
     *     $lang = I18n::lang();
     *
     *     // Change the current language to Spanish
     *     I18n::lang('es-es');
     *
     * @param   string  $lang   new language setting
     * @return  string
     * @since   3.0.2
     */
    public static function lang($lang = NULL)
    {
        if ($lang)
        {
            // Normalize the language
            I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', $lang));
        }

        return I18n::$lang;
    }

    /**
     * Returns translation of a string. If no translation exists, the original
     * string will be returned. No parameters are replaced.
     *
     *     $hello = I18n::get('Hello friends, my name is :name');
     *
     * @param   string  $string text to translate
     * @param   string  $lang   target language
     * @return  string
     */
    public static function get($string, $lang = NULL)
    {
        if ( ! $lang)
        {
            // Use the global target language
            $lang = I18n::$lang;
        }

        // Load the translation table for this language
        $table = I18n::load($lang);

        // Return the translated string if it exists
        return isset($table[$string]) ? $table[$string] : $string;
    }

    /**
     * Returns the translation table for a given language.
     *
     *     // Get all defined Spanish messages
     *     $messages = I18n::load('es-es');
     *
     * @param   string  $lang   language to load
     * @return  array
     */
    public static function load($lang)
    {
        if (isset(I18n::$_cache[$lang]))
        {
            return I18n::$_cache[$lang];
        }

        // New translation table
        $table = array();

        // Split the language: language, region, locale, etc
        $parts = explode('-', $lang);

        do
        {
            // Create a path for this set of parts
            $path = implode(DIRECTORY_SEPARATOR, $parts);

            if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
            {
                $t = array();
                foreach ($files as $file)
                {
                    // Merge the language strings into the sub table
                    $t = array_merge($t, Kohana::load($file));
                }

                // Append the sub table, preventing less specific language
                // files from overloading more specific files
                $table += $t;
            }

            // Remove the last part
            array_pop($parts);
        }
        while ($parts);

        // Cache the translation table locally
        return I18n::$_cache[$lang] = $table;
    }

} // End I18n

if ( ! function_exists('__'))
{
    /**
     * Kohana translation/internationalization function. The PHP function
     * [strtr](http://php.net/strtr) is used for replacing parameters.
     *
     *    __('Welcome back, :user', array(':user' => $username));
     *
     * [!!] The target language is defined by [I18n::$lang].
     * 
     * @uses    I18n::get
     * @param   string  $string text to translate
     * @param   array   $values values to replace in the translated text
     * @param   string  $lang   source language
     * @return  string
     */
    function __($string, array $values = NULL, $lang = 'en-us')
    {
        if ($lang !== I18n::$lang)
        {
            // The message and target languages are different
            // Get the translation for this message
            $string = I18n::get($string);
        }

        return empty($values) ? $string : strtr($string, $values);
    }
}

有没有一种方法可以扩展Kohana_I18n以包括数据库更新而无需编辑系统类?

达斯达

“有没有办法在不编辑系统类的情况下扩展Kohana_I18n以包括数据库更新?” 是。

听起来您要么不熟悉Kohana的级联文件系统,要么不了解在这种情况下它如何有用,要么您不想出于任何原因更改I18n的行为。

如果不是最后一种情况,则只需重命名Appointedd_I18nI18n并相应地更改文件名即可。SYSPATH / classes / I18n.php一个仅包含的文件class I18n extends Kohana_I18n {}而且,如果您查看SYSPATH / classes / Kohana / I18n.php,您将看到selfKohana_I18n从未使用它来调用任何东西。它们I18nKohana_I18n类中一直使用因此您可以“替换” I18n类并更改其行为。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java类扩展了抽象类,但必须具有静态方法

来自分类Dev

Java类扩展了抽象类,但必须具有静态方法

来自分类Dev

仅具有静态方法的类的模块

来自分类Dev

具有静态和非静态重载的类方法

来自分类Dev

具有静态链接的类

来自分类Dev

您如何指示类将具有特定的静态方法?

来自分类Dev

util类中具有静态方法的Mockito

来自分类Dev

具有最终静态方法的final类冗余

来自分类Dev

具有属性和同步方法的静态类

来自分类Dev

util类中具有静态方法的Mockito

来自分类Dev

仅具有静态方法的类-是否为Enum

来自分类Dev

具有静态方法的服务对象的结构、类或枚举?

来自分类Dev

具有静态属性的静态类

来自分类Dev

具有基类和子类的扩展方法

来自分类Dev

使用AutoFac解析具有扩展方法的类

来自分类Dev

扩展方法与实例方法与静态类

来自分类Dev

TypeScript声明使用静态方法扩展类。

来自分类Dev

具有初始化方法的类或静态类的全局实例

来自分类Dev

具有静态箭头功能的类

来自分类Dev

具有静态成员的DLL类

来自分类Dev

如何影响具有静态属性的类

来自分类Dev

具有Spring配置类的静态资源

来自分类Dev

从具有串联元素的类扩展

来自分类Dev

VBA类。具有多个参数和静态函数的调用方法

来自分类Dev

如何丰富具有静态方法的Java库类(又称Scala中的对象)?

来自分类Dev

将具有泛型参数的静态方法转换为泛型类

来自分类Dev

具有静态方法的类在Android中生存多长时间?

来自分类Dev

子类中的静态方法具有与超类中的签名相同的签名

来自分类Dev

具有可变类名和名称空间的PHP静态方法调用

Related 相关文章

热门标签

归档