Completed
Push — master ( 732333...251e18 )
by Aimeos
01:56
created

TranslateViewHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 45
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 9 1
D render() 0 28 10
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Flow
7
 */
8
9
10
namespace Aimeos\Shop\ViewHelper;
11
12
13
use TYPO3\Fluid\Core\ViewHelper\Exception;
14
15
16
class TranslateViewHelper extends TYPO3\Fluid\Core\AbstractViewHelper
17
{
18
	protected $escapeChildren = false;
19
20
21
	public function initializeArguments()
22
	{
23
		$this->registerArgument( 'singular', 'string', 'Singular form of the text to translate' );
24
		$this->registerArgument( 'plural', 'string', 'Plural form of the text, used if $number is greater than one', false );
25
		$this->registerArgument( 'number', 'integer', 'Amount of things relevant for the plural form', false );
26
		$this->registerArgument( 'escape', 'boolean', 'If translated string should be escaped', false );
27
		$this->registerArgument( 'domain', 'string', 'Translation domain from core or an extension', false );
28
		$this->registerArgument( 'arguments', 'array', 'Arguments to be replaced in the resulting string', false );
29
	}
30
31
32
	public function render()
33
	{
34
		$iface = '\Aimeos\MW\View\Iface';
35
		$view = $this->templateVariableContainer->get( '_aimeos_view' );
36
37
		if( !is_object( $view ) || !( $view instanceof $iface ) ) {
38
			throw new Exception( 'Aimeos view object is missing' );
39
		}
40
41
		if( !isset( $this->arguments['singular'] ) ) {
42
			throw new Exception( 'Attribute "singular" missing for Aimeos translate view helper' );
43
		}
44
45
		$singular = $this->arguments['singular'];
46
		$plural = ( isset( $this->arguments['plural'] ) ? $this->arguments['plural'] : '' );
47
		$number = ( isset( $this->arguments['number'] ) ? $this->arguments['number'] : 1 );
48
		$escape = ( isset( $this->arguments['escape'] ) ? $this->arguments['escape'] : true );
49
		$domain = ( isset( $this->arguments['domain'] ) ? $this->arguments['domain'] : 'client' );
50
		$values = ( isset( $this->arguments['arguments'] ) ? $this->arguments['arguments'] : array() );
51
52
		$string = vsprintf( $view->translate( $domain, $singular, $plural, $number ), (array) $values );
53
54
		if( $escape === false ) {
55
			return $string;
56
		}
57
58
		return $view->encoder()->html( $string );
59
	}
60
}