Passed
Push — master ( 75518a...a72d90 )
by Aimeos
13:15
created

Gettext::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2021
6
 * @package MW
7
 * @subpackage Translation
8
 */
9
10
11
namespace Aimeos\MW\Translation;
12
13
use Gettext\Translations;
0 ignored issues
show
Bug introduced by
The type Gettext\Translations 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...
14
15
16
/**
17
 * Translation using Gettext library
18
 *
19
 * @package MW
20
 * @subpackage Translation
21
 */
22
class Gettext
23
	extends \Aimeos\MW\Translation\Base
24
	implements \Aimeos\MW\Translation\Iface
25
{
26
	private $files;
27
	private $sources;
28
29
30
	/**
31
	 * Initializes the translation object using Gettext
32
	 *
33
	 * @param array $sources Associative list of translation domains and lists of translation directories.
34
	 * @param string $locale ISO language name, like "en" or "en_US"
35
	 */
36
	public function __construct( array $sources, string $locale )
37
	{
38
		parent::__construct( $locale );
39
40
		$this->sources = $sources;
41
	}
42
43
44
	/**
45
	 * Returns the translated string for the given domain.
46
	 *
47
	 * @param string $domain Translation domain
48
	 * @param string $singular String to be translated
49
	 * @return string The translated string
50
	 * @throws \Aimeos\MW\Translation\Exception Throws exception on initialization of the translation
51
	 */
52
	public function dt( string $domain, string $singular ) : string
53
	{
54
		foreach( $this->getTranslations( $domain ) as $object )
55
		{
56
			if( ( $result = $object->get( $singular ) ) !== null )
57
			{
58
				if( is_array( $result ) && isset( $result[0] ) ) {
59
					return (string) $result[0];
60
				} elseif( is_string( $result ) ) {
61
					return $result;
62
				}
63
			}
64
		}
65
66
		return (string) $singular;
67
	}
68
69
70
	/**
71
	 * Returns the translated singular or plural form of the string depending on the given number.
72
	 *
73
	 * @param string $domain Translation domain
74
	 * @param string $singular String in singular form
75
	 * @param string $plural String in plural form
76
	 * @param int $number Quantity to choose the correct plural form for languages with plural forms
77
	 * @return string Returns the translated singular or plural form of the string depending on the given number
78
	 * @throws \Aimeos\MW\Translation\Exception Throws exception on initialization of the translation
79
	 */
80
	public function dn( string $domain, string $singular, string $plural, int $number ) : string
81
	{
82
		$idx = $this->getPluralIndex( (int) $number, $this->getLocale() );
83
84
		foreach( $this->getTranslations( $domain ) as $object )
85
		{
86
			if( ( $list = $object->get( $singular ) ) !== null && isset( $list[$idx] ) ) {
87
				return (string) $list[$idx];
88
			}
89
		}
90
91
		return ( $idx > 0 ? (string) $plural : $singular );
92
	}
93
94
95
	/**
96
	 * Returns all locale string of the given domain.
97
	 *
98
	 * @param string $domain Translation domain
99
	 * @return array Associative list with original string as key and associative list with index => translation as value
100
	 */
101
	public function all( string $domain ) : array
102
	{
103
		$messages = [];
104
105
		foreach( $this->getTranslations( $domain ) as $object ) {
106
			$messages = $messages + $object->all();
107
		}
108
109
		return $messages;
110
	}
111
112
113
	/**
114
	 * Returns the MO file objects which contain the translations.
115
	 *
116
	 * @param string $domain Translation domain
117
	 * @return array List of translation objects implementing \Aimeos\MW\Translation\File\Mo
118
	 * @throws \Aimeos\MW\Translation\Exception If initialization fails
119
	 */
120
	protected function getTranslations( string $domain ) : array
121
	{
122
		if( !isset( $this->files[$domain] ) )
123
		{
124
			if ( !isset( $this->sources[$domain] ) )
125
			{
126
				$msg = sprintf( 'No translation directory for domain "%1$s" available', $domain );
127
				throw new \Aimeos\MW\Translation\Exception( $msg );
128
			}
129
130
			// Reverse locations so the former gets not overwritten by the later
131
			$locations = array_reverse( $this->getTranslationFileLocations( $this->sources[$domain], $this->getLocale() ) );
132
133
			foreach( $locations as $location ) {
134
				$this->files[$domain][$location] = new \Aimeos\MW\Translation\File\Mo( $location );
135
			}
136
		}
137
138
		return ( isset( $this->files[$domain] ) ? $this->files[$domain] : [] );
139
	}
140
141
}
142