-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Locale.php
178 lines (158 loc) · 4.34 KB
/
Locale.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\I18n;
use UserFrosting\Support\Repository\Loader\YamlFileLoader;
/**
* Act as a container for a Locale data loaded from filesystem data.
*/
class Locale implements LocaleInterface
{
/**
* @var string The locale config file path.
*/
protected string $configFile = '';
/**
* @var mixed[] Locale config data, loaded from the locale YAML file.
*/
protected array $config;
/**
* Create locale class.
*
* @param string $identifier The locale identifier (ie. "en_US")
* @param string|null $configFile The path to the locale config file
*/
public function __construct(protected string $identifier, ?string $configFile = null)
{
$this->configFile = $configFile ?? "locale://$identifier/locale.yaml";
$this->loadConfig();
}
/**
* Loads the config into the class property.
*
* @throws \UserFrosting\Support\Exception\FileNotFoundException if config file not found
*/
protected function loadConfig(): void
{
$loader = new YamlFileLoader($this->configFile);
$this->config = $loader->load(false);
// Load nested locales config
foreach ($this->getDependentLocales() as $locale) {
$this->config = array_merge($locale->getConfig(), $this->config);
}
}
/**
* Returns the list of authors of the locale.
*
* @return string[] The list of authors
*/
public function getAuthors(): array
{
if (!isset($this->config['authors'])) {
return [];
} else {
return (array) $this->config['authors'];
}
}
/**
* Returns defined configuration file.
*
* @return string
*/
public function getConfigFile(): string
{
return $this->configFile;
}
/**
* Returns the locale identifier.
*
* @return string
*/
public function getIdentifier(): string
{
return $this->identifier;
}
/**
* Return the raw configuration data.
*
* @return mixed[]
*/
public function getConfig(): array
{
return $this->config;
}
/**
* Return an array of parent locales.
*
* @return Locale[]
*/
public function getDependentLocales(): array
{
$parents = $this->getDependentLocalesIdentifier();
// Transform locale identifier to locale instance
$locales = array_map(function ($value) {
return new self($value);
}, $parents);
return $locales;
}
/**
* Return a list of parent locale identifier (eg. [fr_FR, en_US]).
*
* @return string[]
*/
public function getDependentLocalesIdentifier(): array
{
if (isset($this->config['parents']) && is_array($this->config['parents'])) {
return $this->config['parents'];
} else {
return [];
}
}
/**
* Return the name of the locale, in English form.
*
* @return string
*/
public function getName(): string
{
if (isset($this->config['name']) && is_string($this->config['name'])) {
return $this->config['name'];
} else {
return '';
}
}
/**
* Return the number representing the plural rule to use for this locale.
*
* @return int
*/
public function getPluralRule(): int
{
if (isset($this->config['plural_rule'])) {
return (int) $this->config['plural_rule'];
} else {
return 1;
}
}
/**
* Return the localized version of the locale name.
*
* @return string
*/
public function getRegionalName(): string
{
if (isset($this->config['regional']) && is_string($this->config['regional'])) {
return $this->config['regional'];
} elseif (isset($this->config['name']) && is_string($this->config['name'])) {
return $this->config['name'];
} else {
return $this->identifier;
}
}
}