douwendu2460 2013-12-19 00:19
浏览 33
已采纳

如何将翻译解析添加到rain.tpl?

I'm not good with regular expressions and I met huge problems with changing rain.tpl class. Whole class is available here (I don't want to copy-paste whole file here).

The thing is that I'd like to parse {_(word)} into <?= __('word') ?> because it's a lot easier to use than {function="__('word')"}.

I tried to change it myself but I can't put proper regular expressions into methods and output are more and more errors.

EDIT:
The part of parsing function responsible for parsing {function="__('word')"} etc looks like this:

//function
elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){

    //tag
    $tag = $code[ 0 ];

    //function
    $function = $code[ 1 ];

    // check if there's any function disabled by black_list
    $this->function_check( $tag );

    if( empty( $code[ 2 ] ) )
        $parsed_function = $function . "()";
    else
        // parse the function
        $parsed_function = $function . $this->var_replace( $code[ 2 ], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );

    //if code
    $compiled_code .=   "<?php echo $parsed_function; ?>";
}

And the functions that calls the parsing (there are some regular expressions too) look like it:

protected function compileTemplate( $template_code, $tpl_basedir ){

    //tag list
    $tag_regexp = array( 'loop'         => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})',
                         'loop_close'   => '(\{\/loop\})',
                         'if'           => '(\{if(?: condition){0,1}="[^"]*"\})',
                         'elseif'       => '(\{elseif(?: condition){0,1}="[^"]*"\})',
                         'else'         => '(\{else\})',
                         'if_close'     => '(\{\/if\})',
                         'function'     => '(\{function="[^"]*"\})',
                         'noparse'      => '(\{noparse\})',
                         'noparse_close'=> '(\{\/noparse\})',
                         'ignore'       => '(\{ignore\}|\{\*)',
                         'ignore_close' => '(\{\/ignore\}|\*\})',
                         'include'      => '(\{include="[^"]*"(?: cache="[^"]*")?\})',
                         'template_info'=> '(\{\$template_info\})',
                         'function'     => '(\{function="(\w*?)(?:.*?)"\})'
                        );

    $tag_regexp = "/" . join( "|", $tag_regexp ) . "/";

    //split the code with the tags regexp
    $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );

    //path replace (src of img, background and href of link)
    $template_code = $this->path_replace( $template_code, $tpl_basedir );

    //compile the code
    $compiled_code = $this->compileCode( $template_code );

    //return the compiled code
    return $compiled_code;

}

Hope it's enough for you to help me without downloading and reading the whole class file. Hope someone knows this template and can help me :)

  • 写回答

1条回答 默认 最新

  • dongshi7433 2013-12-19 15:29
    关注

    By the time of waiting I managed to find the solution by myself. Here it is:

    1. I added the line 'translation' => '(\{_\([^)]*(\w*)\)\})', to the array $tag_regexp
    2. I added the new elseif:

    //translation
                elseif( preg_match( '/\{_\((\w*)\)\}/', $html, $code ) ){
    
                    //function
                    $word = $code[ 1 ];
    
                    //if code
                    $compiled_code .=   "<?= __('$word'); ?>";
                }
    

    Maybe it will help someone :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面