summaryrefslogtreecommitdiff
path: root/smartyplugins/PreTr.php
blob: 8d10f54e4e7d4cedc748a24a532ead94082df2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * smarty_pre_tr
 * 
 * @param array $source 
 * @access public
 * @return full source with partially treated  sections
 */

namespace Bitweaver\Plugins;

use Smarty\Compile\Modifier\ModifierCompilerInterface;
use Smarty\Compiler\Template;

class PreTr implements ModifierCompilerInterface {
	 public function __construct() {
		 // Initialization code
	 }

	 public function compile($code, Template $template) {
		$source = $code[0];
		// Now replace the matched language strings with the entry in the file
		// $return = preg_replace_callback( '#\{tr[^\{]*\}([^\{]+)\{/tr\}#', '_translate_lang', $source );
		//
		// correction: in order to match when a variable is inside  tags.
		// Example: The newsletter was sent to {$sent} email addresses,
		// and where there are parameters with  take away the smarty comments
		// {* *} in case they have tr tags
		$ret = preg_replace_callback( '#(?s)(\{tr[^\}]*\})(.+?)\{/tr\}#', [ $this, 'translate_lang' ], preg_replace ( '#(?s)\{\*.*?\*\}#', '', $source ) );
		return $ret;
	}

	/**
	 * _translate_lang 
	 * 
	 * @param array $pKey Hash passed in by smarty_prefilter_tr() above
	 * @param array $pKey[0] starting  tag
	 * @param array $pKey[1] string that needs to be translated
	 * @param array $pKey[2] flosing  tag
	 * @access protected
	 * @return string translated string
	 *
	 * Note: If you want to have a  block interpreted AFTER variable substitution, add a parameter to 
	 * e.g.: {tr post=1}text {$that} needs to be {$evaluated}
	 */
	private function translate_lang( $pKey ) {
		global $gBitLanguage, $lang;

		// this is the original codeblock:
		//	if (strstr($pKey[2], "{\$")) {
		//		// We have a variable - keep the tags to be perhaps translated in block.tr.php
		//		return $pKey[1].$pKey[2]."";
		//	} elseif ($pKey[1] == "") {
		//		// no more possible translation in block.tr.php
		//		return $trans;
		//	} else {
		//		// perhaps variable substitution to do in block.tr.php
		//		return $pKey[1].$trans."";
		//	}
		// Explanation why this doesn't work:
		//  - case 1
		//    returning stuff like waiting for {$number} ratings
		//    will get translated by block.tr.php but only after the {$number} has
		//    been interpreted and we will end up with master strings like:
		//    waiting for 4 ratings
		//    waiting for 5 ratings
		//    waiting for 6 ratings
		//
		//  - case 2
		//    this should be fine since it has been translated and the  blocks
		//    have been removed i.e. block.tr.php won't be called anymore
		//
		//  - case 3
		//    this will leave everything as is. this will work as well, but only if
		//    there is no {$var} in the string - see case 1
		// --- xing

		// if the entire string in  is a variable, we pass it on to block.tr.php
		// e.g. {$menu.menu_title} in top_bar.tpl
		// if you change this regexp, please modify the one in languages/BitLanguage.php as well (approx line 256)
		if( preg_match( '!^(\{\$[^\}]*\})+$!', $pKey[2] ) ) {
			return $pKey[1].$pKey[2]."";
		} elseif( $pKey[1] == "" ) {
			// no parameters set for block.tr.php
			$trans = $gBitLanguage->translate( $pKey[2] );
			return $trans;
		}
			// perhaps there are parameters set for block.tr.php
			return $pKey[1].$pKey[2]."";

	}
}