|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\SettingsModule\Api; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
|
15
|
|
|
use Symfony\Component\Intl\Intl; |
|
16
|
|
|
|
|
17
|
|
|
class LocaleApi |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Locales with translations present |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $supportedLocales = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get array of supported locales |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getSupportedLocales() |
|
31
|
|
|
{ |
|
32
|
|
|
if (empty($this->supportedLocales)) { |
|
33
|
|
|
$this->supportedLocales[] = 'en'; |
|
34
|
|
|
$finder = new Finder(); |
|
35
|
|
|
if (is_dir('app/Resources/translations')) { |
|
36
|
|
|
$files = $finder->files() |
|
37
|
|
|
->in(['app/Resources/translations']) |
|
38
|
|
|
->depth(0) |
|
39
|
|
|
->name('*.po') |
|
40
|
|
|
->notName('*.template.*'); |
|
41
|
|
|
foreach ($files as $file) { |
|
42
|
|
|
$fileName = $file->getBasename('.po'); |
|
43
|
|
|
list($domain, $locale) = explode('.', $fileName); |
|
|
|
|
|
|
44
|
|
|
if (!in_array($locale, $this->supportedLocales)) { |
|
45
|
|
|
$this->supportedLocales[] = $locale; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
$this->addLegacyLocales(); // @deprecated remove at Core-2.0 |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->supportedLocales; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get array of supported locales with their translated name |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getSupportedLocaleNames() |
|
61
|
|
|
{ |
|
62
|
|
|
$locales = $this->getSupportedLocales(); |
|
63
|
|
|
$namedLocales = []; |
|
64
|
|
|
foreach ($locales as $locale) { |
|
65
|
|
|
$namedLocales[Intl::getLanguageBundle()->getLanguageName($locale)] = $locale; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $namedLocales; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Read legacy locale.ini files and add those locales |
|
73
|
|
|
* @deprecated remove at Core-2.0 |
|
74
|
|
|
*/ |
|
75
|
|
|
private function addLegacyLocales() |
|
76
|
|
|
{ |
|
77
|
|
|
$legacyLocales = \ZLanguage::getInstalledLanguages(); |
|
78
|
|
|
foreach ($legacyLocales as $locale) { |
|
79
|
|
|
if (!in_array($locale, $this->supportedLocales)) { |
|
80
|
|
|
$this->supportedLocales[] = $locale; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.