Zend2   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getTranslations() 0 24 5
A dt() 0 18 4
A dn() 0 24 5
A getAll() 0 10 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2018
6
 * @package MW
7
 * @subpackage Translation
8
 */
9
10
11
namespace Aimeos\MW\Translation;
12
13
14
/**
15
 * Translation using Zend\I18n\Translator\Translator
16
 *
17
 * @package MW
18
 * @subpackage Translation
19
 */
20
class Zend2
21
	extends \Aimeos\MW\Translation\Base
22
	implements \Aimeos\MW\Translation\Iface
23
{
24
	private $adapter;
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\I18n\Translator\Translator
40
	 *
41
	 * @link http://framework.zend.com/manual/2.3/en/modules/zend.i18n.translating.html
42
	 */
43
	public function __construct( array $translationSources, $adapter, $locale, array $options = [] )
44
	{
45
		parent::__construct( $locale );
46
47
		$this->adapter = $adapter;
48
		$this->options = $options;
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 $singular 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, $singular )
63
	{
64
		$singular = (string) $singular;
65
66
		try
67
		{
68
			$locale = $this->getLocale();
69
70
			foreach( $this->getTranslations( $domain ) as $object )
71
			{
72
				if( ( $string = $object->translate( $singular, $domain, $locale ) ) != $singular ) {
73
					return $string;
74
				}
75
			}
76
		}
77
		catch( \Exception $e ) { ; } // Discard errors, return original string instead
78
79
		return (string) $singular;
80
	}
81
82
83
	/**
84
	 * Returns the translated singular or plural form of the string depending on the given number.
85
	 *
86
	 * @param string $domain Translation domain
87
	 * @param string $singular String in singular form
88
	 * @param string $plural String in plural form
89
	 * @param integer $number Quantity to choose the correct plural form for languages with plural forms
90
	 * @return string Returns the translated singular or plural form of the string depending on the given number
91
	 * @throws \Aimeos\MW\Translation\Exception Throws exception on initialization of the translation
92
	 *
93
	 * @link http://framework.zend.com/manual/en/zend.translate.plurals.html
94
	 */
95
	public function dn( $domain, $singular, $plural, $number )
96
	{
97
		$singular = (string) $singular;
98
		$plural = (string) $plural;
99
		$number = (int) $number;
100
101
		try
102
		{
103
			$locale = $this->getLocale();
104
105
			foreach( $this->getTranslations( $domain ) as $object )
106
			{
107
				if( ( $string = $object->translatePlural( $singular, $plural, $number, $domain, $locale ) ) != $singular ) {
108
					return $string;
109
				}
110
			}
111
		}
112
		catch( \Exception $e ) { ; } // Discard errors, return original string instead
113
114
		if( $this->getPluralIndex( $number, $this->getLocale() ) > 0 ) {
115
			return (string) $plural;
116
		}
117
118
		return (string) $singular;
119
	}
120
121
122
	/**
123
	 * Returns all locale string of the given domain.
124
	 *
125
	 * @param string $domain Translation domain
126
	 * @return array Associative list with original string as key and associative list with index => translation as value
127
	 */
128
	public function getAll( $domain )
129
	{
130
		$messages = [];
131
		$locale = $this->getLocale();
132
133
		foreach( $this->getTranslations( $domain ) as $object ) {
134
			$messages = $messages + (array) $object->getMessages( $domain, $locale );
135
		}
136
137
		return $messages;
138
	}
139
140
141
	/**
142
	 * Returns the initialized Zend translation object which contains the translations.
143
	 *
144
	 * @param string $domain Translation domain
145
	 * @return array List of translation objects implementing Zend_Translate
146
	 * @throws \Aimeos\MW\Translation\Exception If initialization fails
147
	 */
148
	protected function getTranslations( $domain )
149
	{
150
		if( !isset( $this->translations[$domain] ) )
151
		{
152
			if ( !isset( $this->translationSources[$domain] ) )
153
			{
154
				$msg = sprintf( 'No translation directory for domain "%1$s" available', $domain );
155
				throw new \Aimeos\MW\Translation\Exception( $msg );
156
			}
157
158
			$locale = $this->getLocale();
159
			// Reverse locations so the former gets not overwritten by the later
160
			$locations = array_reverse( $this->getTranslationFileLocations( $this->translationSources[$domain], $locale ) );
161
162
			foreach( $locations as $location )
163
			{
164
				$translator = \Zend\I18n\Translator\MwTranslator::factory( $this->options );
165
				$translator->addTranslationFile( $this->adapter, $location, $domain, $locale );
166
167
				$this->translations[$domain][$location] = $translator;
168
			}
169
		}
170
171
		return ( isset( $this->translations[$domain] ) ? $this->translations[$domain] : [] );
172
	}
173
174
}
175