Zend   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 9 2
A getTranslations() 0 22 5
A __construct() 0 8 1
A dn() 0 18 5
A dt() 0 14 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2014-2018
7
 * @package MW
8
 * @subpackage Translation
9
 */
10
11
12
namespace Aimeos\MW\Translation;
13
14
15
/**
16
 * Translation using \Zend_Translate
17
 *
18
 * @package MW
19
 * @subpackage Translation
20
 */
21
class Zend
22
	extends \Aimeos\MW\Translation\Base
23
	implements \Aimeos\MW\Translation\Iface
24
{
25
	private $options;
26
	private $translationSources;
27
	private $translations = [];
28
29
30
	/**
31
	 * Initializes the translation object using \Zend_Translate.
32
	 * This implementation only accepts files as source for the \Zend_Translate_Adapter.
33
	 *
34
	 * @param array $translationSources Associative list of translation domains and lists of translation directories.
35
	 * 	Translations from the first file aren't overwritten by the later ones
36
	 * as key and the directory where the translation files are located as value.
37
	 * @param string $adapter Name of the Zend translation adapter
38
	 * @param string $locale ISO language name, like "en" or "en_US"
39
	 * @param string $options Associative array containing additional options for \Zend_Translate
40
	 *
41
	 * @link http://framework.zend.com/manual/1.11/en/zend.translate.adapter.html
42
	 */
43
	public function __construct( array $translationSources, $adapter, $locale, array $options = [] )
44
	{
45
		parent::__construct( $locale );
46
47
		$this->options = $options;
48
		$this->options['adapter'] = (string) $adapter;
49
		$this->options['locale'] = (string) $locale;
50
		$this->translationSources = $translationSources;
51
	}
52
53
54
	/**
55
	 * Returns the translated string for the given domain.
56
	 *
57
	 * @param string $domain Translation domain
58
	 * @param string $string String to be translated
59
	 * @return string The translated string
60
	 * @throws \Aimeos\MW\Translation\Exception Throws exception on initialization of the translation
61
	 */
62
	public function dt( $domain, $string )
63
	{
64
		try
65
		{
66
			foreach( $this->getTranslations( $domain ) as $object )
67
			{
68
				if( $object->isTranslated( $string ) === true ) {
69
					return $object->translate( $string, $this->getLocale() );
70
				}
71
			}
72
		}
73
		catch( \Exception $e ) { ; } // Discard exceptions, return the original string instead
74
75
		return (string) $string;
76
	}
77
78
79
	/**
80
	 * Returns the translated singular or plural form of the string depending on the given number.
81
	 *
82
	 * @param string $domain Translation domain
83
	 * @param string $singular String in singular form
84
	 * @param string $plural String in plural form
85
	 * @param integer $number Quantity to choose the correct plural form for languages with plural forms
86
	 * @return string Returns the translated singular or plural form of the string depending on the given number
87
	 * @throws \Aimeos\MW\Translation\Exception Throws exception on initialization of the translation
88
	 *
89
	 * @link http://framework.zend.com/manual/en/zend.translate.plurals.html
90
	 */
91
	public function dn( $domain, $singular, $plural, $number )
92
	{
93
		try
94
		{
95
			foreach( $this->getTranslations( $domain ) as $object )
96
			{
97
				if( $object->isTranslated( $singular ) === true ) {
98
					return $object->plural( $singular, $plural, $number, $this->getLocale() );
99
				}
100
			}
101
		}
102
		catch( \Exception $e ) { ; } // Discard exceptions, return the original string instead
103
104
		if( $this->getPluralIndex( $number, $this->getLocale() ) > 0 ) {
105
			return (string) $plural;
106
		}
107
108
		return (string) $singular;
109
	}
110
111
112
	/**
113
	 * Returns all locale string of the given domain.
114
	 *
115
	 * @param string $domain Translation domain
116
	 * @return array Associative list with original string as key and associative list with index => translation as value
117
	 */
118
	public function getAll( $domain )
119
	{
120
		$messages = [];
121
122
		foreach( $this->getTranslations( $domain ) as $object ) {
123
			$messages = $messages + $object->getMessages();
124
		}
125
126
		return $messages;
127
	}
128
129
130
	/**
131
	 * Returns the initialized Zend translation object which contains the translations.
132
	 *
133
	 * @param string $domain Translation domain
134
	 * @return array List of translation objects implementing \Zend_Translate
135
	 * @throws \Aimeos\MW\Translation\Exception If initialization fails
136
	 */
137
	protected function getTranslations( $domain )
138
	{
139
		if( !isset( $this->translations[$domain] ) )
140
		{
141
			if ( !isset( $this->translationSources[$domain] ) )
142
			{
143
				$msg = sprintf( 'No translation directory for domain "%1$s" available', $domain );
144
				throw new \Aimeos\MW\Translation\Exception( $msg );
145
			}
146
147
			// Reverse locations so the former gets not overwritten by the later
148
			$locations = array_reverse( $this->getTranslationFileLocations( $this->translationSources[$domain], $this->getLocale() ) );
149
			$options = $this->options;
150
151
			foreach( $locations as $location )
152
			{
153
				$options['content'] = $location;
154
				$this->translations[$domain][] = new \Zend_Translate( $options );
0 ignored issues
show
Bug introduced by
The type Zend_Translate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
155
			}
156
		}
157
158
		return ( isset( $this->translations[$domain] ) ? $this->translations[$domain] : [] );
159
	}
160
}
161