source: branches/version-2_12-multilang/data/smarty_extends/compiler.assign_t.php @ 22153

Revision 22153, 1.9 KB checked in by pineray, 11 years ago (diff)

#163 テキスト出力多言語対応

値を翻訳しながら変数にセットするコンパイラ関数を追加.

Line 
1<?php
2/**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
7
8/**
9 * Smarty {assign_t} compiler function plugin
10 *
11 * Type:     compiler function<br>
12 * Name:     assign_t<br>
13 * Purpose:  assign a translated value to a template variable
14 * @author   pineray 松田光貴 <matsudaterutaka at gmail dot com>
15 * @param string containing var-attribute and value-attribute
16 * @param Smarty_Compiler
17 */
18function smarty_compiler_assign_t($tag_attrs, &$compiler)
19{
20    // 多言語対応用の関数が定義されていなければエラーを出力
21    if (!function_exists('t')) {
22        $compiler->_syntax_error("[compiler] function 't' is not defined", E_USER_WARNING);
23        return;
24    }
25
26    $params = $compiler->_parse_attrs($tag_attrs);
27
28    // セット対象が無ければエラーを出力
29    if (!isset($params['var'])) {
30        $compiler->_syntax_error("assign: missing 'var' parameter", E_USER_WARNING);
31        return;
32    }
33    $var = $params['var'];
34    unset($params['var']);
35
36    // エイリアスが無ければエラーを出力
37    if (empty($params['string'])) {
38        $compiler->_syntax_error("[compiler] parameter 'string' cannot be empty", E_USER_WARNING);
39        return;
40    }
41
42    foreach ($params as $key => $param) {
43        eval("\$params[{$key}] = {$param};");
44    }
45
46    $string = $params['string'];
47    unset($params['string']);
48
49    // オプション用の配列
50    $options = array();
51    // 言語コードの指定があればオプションにセット
52    if (!empty($params['lang_code'])) {
53        $options['lang_code'] = $params['lang_code'];
54        unset($params['lang_code']);
55    }
56    // 機種の指定があればオプションにセット
57    if (!empty($params['device_type_id'])) {
58        $options['device_type_id'] = $params['device_type_id'];
59        unset($params['device_type_id']);
60    }
61
62    $translated = '"' . t($string, $params, $options) . '"';
63
64    return "\$this->assign({$var}, {$translated});";
65}
Note: See TracBrowser for help on using the repository browser.