1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2015-2025 |
||
6 | * @package Base |
||
7 | * @subpackage Translation |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace Aimeos\Base\Translation; |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Abstract class for the translation interface |
||
16 | * |
||
17 | * @package Base |
||
18 | * @subpackage Translation |
||
19 | */ |
||
20 | abstract class Base |
||
21 | { |
||
22 | private string $locale; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Initializes the translation object. |
||
27 | * |
||
28 | * @param string $locale Locale string, e.g. en or en_GB |
||
29 | */ |
||
30 | public function __construct( string $locale ) |
||
31 | { |
||
32 | if( preg_match( '/^[a-z]{2,3}(_[A-Z]{2})?$/', $locale ) !== 1 ) { |
||
33 | throw new \Aimeos\Base\Translation\Exception( sprintf( 'Invalid locale "%1$s"', $locale ) ); |
||
34 | } |
||
35 | |||
36 | $this->locale = (string) $locale; |
||
37 | } |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Returns the current locale string. |
||
42 | * |
||
43 | * @return string ISO locale string |
||
44 | */ |
||
45 | public function getLocale() : string |
||
46 | { |
||
47 | return $this->locale; |
||
48 | } |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Returns the location of the translation file. |
||
53 | * If the requested file does exists (eg: de_DE) the implementation |
||
54 | * will check for "de" and will return that location as fallback. |
||
55 | * |
||
56 | * @param string[] $paths Paths of the translation files |
||
57 | * @param string $locale Locale to be used |
||
58 | * @return string[] List of locations to the translation files |
||
59 | * @throws \Aimeos\Base\Translation\Exception If translation file doesn't exist |
||
60 | */ |
||
61 | protected function getTranslationFileLocations( array $paths, string $locale ) : array |
||
62 | { |
||
63 | $locations = []; |
||
64 | |||
65 | foreach( $paths as $path ) |
||
66 | { |
||
67 | $location = $path . DIRECTORY_SEPARATOR . $locale; |
||
68 | |||
69 | if( file_exists( $location ) ) |
||
70 | { |
||
71 | $locations[] = $location; |
||
72 | continue; |
||
73 | } |
||
74 | |||
75 | if( strlen( $locale ) > 3 ) |
||
76 | { |
||
77 | $location = $path . DIRECTORY_SEPARATOR . substr( $locale, 0, -strlen( strrchr( $locale, '_' ) ) ); |
||
78 | |||
79 | if( file_exists( $location ) ) { |
||
80 | $locations[] = $location; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return $locations; |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Returns the plural index number to be used for the plural translation. |
||
91 | * |
||
92 | * @param int $number Quantity to find the plural index |
||
93 | * @param string $locale Locale to be used |
||
94 | * @return int Number of the plural index |
||
95 | */ |
||
96 | protected function getPluralIndex( int $number, string $locale ) : int |
||
97 | { |
||
98 | if( $locale == 'pt_BR' ) { |
||
99 | $locale = 'xbr'; // temporary set a locale for brasilian |
||
100 | } |
||
101 | |||
102 | if( strlen( $locale ) > 3 ) { |
||
103 | $locale = substr( $locale, 0, -strlen( strrchr( $locale, '_' ) ) ); |
||
104 | } |
||
105 | |||
106 | return $this->index( abs( $number ), $locale ); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Returns the plural index for the given locale. |
||
112 | * |
||
113 | * @param int $number Quantity to find the plural index |
||
114 | * @param string $locale Locale to be used |
||
115 | * @return int Number of the plural index |
||
116 | * |
||
117 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
||
118 | * @license New BSD License, https://opensource.org/licenses/BSD-3-Clause |
||
119 | */ |
||
120 | protected function index( int $number, string $locale ) : int |
||
121 | { |
||
122 | switch( $locale ) |
||
123 | { |
||
124 | case 'af': |
||
125 | case 'az': |
||
126 | case 'bn': |
||
127 | case 'bg': |
||
128 | case 'ca': |
||
129 | case 'da': |
||
130 | case 'de': |
||
131 | case 'el': |
||
132 | case 'en': |
||
133 | case 'eo': |
||
134 | case 'es': |
||
135 | case 'et': |
||
136 | case 'eu': |
||
137 | case 'fa': |
||
138 | case 'fi': |
||
139 | case 'fo': |
||
140 | case 'fur': |
||
141 | case 'fy': |
||
142 | case 'gl': |
||
143 | case 'gu': |
||
144 | case 'ha': |
||
145 | case 'he': |
||
146 | case 'hu': |
||
147 | case 'is': |
||
148 | case 'it': |
||
149 | case 'ku': |
||
150 | case 'lb': |
||
151 | case 'ml': |
||
152 | case 'mn': |
||
153 | case 'mr': |
||
154 | case 'nah': |
||
155 | case 'nb': |
||
156 | case 'ne': |
||
157 | case 'nl': |
||
158 | case 'nn': |
||
159 | case 'no': |
||
160 | case 'om': |
||
161 | case 'or': |
||
162 | case 'pa': |
||
163 | case 'pap': |
||
164 | case 'ps': |
||
165 | case 'pt': |
||
166 | case 'so': |
||
167 | case 'sq': |
||
168 | case 'sv': |
||
169 | case 'sw': |
||
170 | case 'ta': |
||
171 | case 'te': |
||
172 | case 'tk': |
||
173 | case 'ur': |
||
174 | case 'zu': |
||
175 | return ( $number == 1 ) ? 0 : 1; |
||
176 | |||
177 | case 'am': |
||
178 | case 'bh': |
||
179 | case 'fil': |
||
180 | case 'fr': |
||
181 | case 'gun': |
||
182 | case 'hi': |
||
183 | case 'ln': |
||
184 | case 'mg': |
||
185 | case 'nso': |
||
186 | case 'xbr': |
||
187 | case 'ti': |
||
188 | case 'wa': |
||
189 | return ( ( $number == 0 ) || ( $number == 1 ) ) ? 0 : 1; |
||
190 | |||
191 | case 'be': |
||
192 | case 'bs': |
||
193 | case 'hr': |
||
194 | case 'ru': |
||
195 | case 'sr': |
||
196 | case 'uk': |
||
197 | return ( ( $number % 10 == 1 ) && ( $number % 100 != 11 ) ) ? 0 : ( ( ( $number % 10 >= 2 ) && ( $number % 10 <= 4 ) && ( ( $number % 100 < 10 ) || ( $number % 100 >= 20 ) ) ) ? 1 : 2 ); |
||
198 | |||
199 | case 'cs': |
||
200 | case 'sk': |
||
201 | return ( $number == 1 ) ? 0 : ( ( ( $number >= 2 ) && ( $number <= 4 ) ) ? 1 : 2 ); |
||
202 | |||
203 | case 'ar': |
||
204 | return ( $number == 0 ) ? 0 : ( ( $number == 1 ) ? 1 : ( ( $number == 2 ) ? 2 : ( ( ( $number >= 3 ) && ( $number <= 10 ) ) ? 3 : ( ( ( $number >= 11 ) && ( $number <= 99 ) ) ? 4 : 5 ) ) ) ); |
||
205 | |||
206 | case 'cy': |
||
207 | return ( $number == 1 ) ? 0 : ( ( $number == 2 ) ? 1 : ( ( ( $number == 8 ) || ( $number == 11 ) ) ? 2 : 3 ) ); |
||
208 | |||
209 | case 'ga': |
||
210 | return ( $number == 1 ) ? 0 : ( ( $number == 2 ) ? 1 : 2 ); |
||
211 | |||
212 | case 'lt': |
||
213 | return ( ( $number % 10 == 1 ) && ( $number % 100 != 11 ) ) ? 0 : ( ( ( $number % 10 >= 2 ) && ( ( $number % 100 < 10 ) || ( $number % 100 >= 20 ) ) ) ? 1 : 2 ); |
||
214 | |||
215 | case 'lv': |
||
216 | return ( $number == 0 ) ? 0 : ( ( ( $number % 10 == 1 ) && ( $number % 100 != 11 ) ) ? 1 : 2 ); |
||
217 | |||
218 | case 'mk': |
||
219 | return ( $number % 10 == 1 ) ? 0 : 1; |
||
220 | |||
221 | case 'mt': |
||
222 | return ( $number == 1 ) ? 0 : ( ( ( $number == 0 ) || ( ( $number % 100 > 1 ) && ( $number % 100 < 11 ) ) ) ? 1 : ( ( ( $number % 100 > 10 ) && ( $number % 100 < 20 ) ) ? 2 : 3 ) ); |
||
223 | |||
224 | case 'pl': |
||
225 | return ( $number == 1 ) ? 0 : ( ( ( $number % 10 >= 2 ) && ( $number % 10 <= 4 ) && ( ( $number % 100 < 12 ) || ( $number % 100 > 14 ) ) ) ? 1 : 2 ); |
||
226 | |||
227 | case 'ro': |
||
228 | return ( $number == 1 ) ? 0 : ( ( ( $number == 0 ) || ( ( $number % 100 > 0 ) && ( $number % 100 < 20 ) ) ) ? 1 : 2 ); |
||
229 | |||
230 | case 'sl': |
||
231 | return ( $number % 100 == 1 ) ? 0 : ( ( $number % 100 == 2 ) ? 1 : ( ( ( $number % 100 == 3 ) || ( $number % 100 == 4 ) ) ? 2 : 3 ) ); |
||
232 | |||
233 | default: |
||
234 | return 0; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | } |
||
239 |