PHPTAL i18n 在非对象错误上调用成员函数

发布于 2024-08-23 15:53:31 字数 129 浏览 10 评论 0原文

我在我的项目中使用 PHPTAL,除了我想使用它的 i18n 服务之外,我几乎能够在所有情况下成功实现它。我经常收到错误“调用非对象上的成员函数”

我尝试搜索网络论坛等,但没有找到任何解决方案,如果有人可以帮助我,我将非常感激。

I'm using PHPTAL in my project I'm able to successfully implement it almost all the cases except when I want to use its i18n services. I constantly get errors "Call to a member function on a non-object"

I've tried searching the net forums etc. but not found any solution, will really appreciate if somebody can help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-08-30 15:53:31

没有人回答我的问题真是令人失望,所以在这里我终于找到了解决方案并回答了我自己的问题。

默认情况下,PHPTAL 没有设置翻译器来将您的文本从一种语言翻译成另一种语言。所以你必须自己做。下面给出了一些步骤来执行此操作。 。 。

第1步.创建一个新的php文件(例如MyTranslator.php)并生成一个新类,例如PHPTAL_MyTranslator并将其存储在PHPTAL文件夹中。该类将实现 PHPTAL_TranslationService 接口。该界面中有五个功能,但我们关心的功能只是翻译。因此,只需添加其余函数的声明并为翻译函数编写代码即可。我在我的案例中编写和使用的类是:

class PHPTAL_MyTranslator implements PHPTAL_TranslationService {

    /**
     * current execution context
     */
    protected $_context = null;

    /**
     * @param string (name of the language)
     * @return string (language you've just set)
     *
     * This method sets translation language.
     * Name of the language is a dir name where you keep your translation files
     */
    public function setLanguage() {
    }

    public function __construct( $context ) {
        $this->_context = $context;
    }

    /**
     * @param string (translation file name)
     * @return void
     *
     * You can separate translations in several files, and use only when needed.
     * Use this method to specify witch translation file you want to
     * use for current controller.
     */
    public function useDomain( $domain ) {
    }

    /**
     * Set an interpolation var.
     * Replace all ${key}s with values in translated strings.
     */
    public function setVar( $key, $value ) {
    }

    /**
     * Translate a text and interpolate variables.
     */
    public function translate( $key, $htmlescape=true ) {
        $value = $key;
        if( empty( $value ) ) {
            return $key;
        }
        while( preg_match( '/\${(.*?)\}/sm', $value, $m ) ) {
            list( $src, $var ) = $m;
            if( !array_key_exists( $var, $this->_context ) ) {
                $err = sprintf( 'Interpolation error, var "%s" not set', $var );
                throw new Exception( $err );
            }
            $value = str_replace( $src, $this->_context->$var, $value );
        }
        return gettext( $value );
    }

    /**
     * Not implemented yet, default encoding is used
     */
    public function setEncoding( $encoding ) {
    }
}

第2步。现在打开PHPTAL.php文件并修改PHPTAL类的构造函数。向该函数添加一行,如下所示。 。 。 。 。

Before

<代码>

</

public function __construct($path=false)
{
    $this->_path = $path;
    $this->_globalContext = new StdClass();
    $this->_context = new PHPTAL_Context();
    $this->_context->setGlobal($this->_globalContext);

    if (function_exists('sys_get_temp_dir')) {
    ............

代码> <代码>

After

<代码>

public function __construct($path=false)
{
    $this->_path = $path;
    $this->_globalContext = new StdClass();
    $this->_context = new PHPTAL_Context();
    $this->_context->setGlobal($this->_globalContext);
    //Set translator here
    $this->setTranslator( new PHPTAL_MyTranslator( $this->_context ) );

    if (function_exists('sys_get_temp_dir')) {
    .............

这两个简单的步骤将使您的 i18n:attributes 以及 i18n:translate 属性正常工作。

干杯...

Its heartily disappointing that no one answered my question so here I'm finally with the solution and answering my own question.

By default there is no translator set by PHPTAL in order to translate your text from one language to another. So you've to do it on your own. There are some steps give below to do this . . .

Step 1. Create a new php file( e.g. MyTranslator.php ) and generate a new class for example PHPTAL_MyTranslator and store it inside the PHPTAL folder. This class will implement the interface PHPTAL_TranslationService. There are five functions in this interface but the function of our concern is only translate. So just add a declaration for rest of the functions and write code for the translate function. The class I've written and used in my case is :

class PHPTAL_MyTranslator implements PHPTAL_TranslationService {

    /**
     * current execution context
     */
    protected $_context = null;

    /**
     * @param string (name of the language)
     * @return string (language you've just set)
     *
     * This method sets translation language.
     * Name of the language is a dir name where you keep your translation files
     */
    public function setLanguage() {
    }

    public function __construct( $context ) {
        $this->_context = $context;
    }

    /**
     * @param string (translation file name)
     * @return void
     *
     * You can separate translations in several files, and use only when needed.
     * Use this method to specify witch translation file you want to
     * use for current controller.
     */
    public function useDomain( $domain ) {
    }

    /**
     * Set an interpolation var.
     * Replace all ${key}s with values in translated strings.
     */
    public function setVar( $key, $value ) {
    }

    /**
     * Translate a text and interpolate variables.
     */
    public function translate( $key, $htmlescape=true ) {
        $value = $key;
        if( empty( $value ) ) {
            return $key;
        }
        while( preg_match( '/\${(.*?)\}/sm', $value, $m ) ) {
            list( $src, $var ) = $m;
            if( !array_key_exists( $var, $this->_context ) ) {
                $err = sprintf( 'Interpolation error, var "%s" not set', $var );
                throw new Exception( $err );
            }
            $value = str_replace( $src, $this->_context->$var, $value );
        }
        return gettext( $value );
    }

    /**
     * Not implemented yet, default encoding is used
     */
    public function setEncoding( $encoding ) {
    }
}

Step 2. Now open the PHPTAL.php file and modify the constructor of PHPTAL class. Add a line to this function as shown below . . . . .

Before

public function __construct($path=false)
{
    $this->_path = $path;
    $this->_globalContext = new StdClass();
    $this->_context = new PHPTAL_Context();
    $this->_context->setGlobal($this->_globalContext);

    if (function_exists('sys_get_temp_dir')) {
    ............

After

public function __construct($path=false)
{
    $this->_path = $path;
    $this->_globalContext = new StdClass();
    $this->_context = new PHPTAL_Context();
    $this->_context->setGlobal($this->_globalContext);
    //Set translator here
    $this->setTranslator( new PHPTAL_MyTranslator( $this->_context ) );

    if (function_exists('sys_get_temp_dir')) {
    .............

These two simple steps will make your i18n:attributes as well as i18n:translate attributes to work properly.

Cheers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文