/
Locale.php
488 lines (431 loc) · 13.5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
namespace TractorCow\Fluent\Model;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use TractorCow\Fluent\Extension\FluentDirectorExtension;
use TractorCow\Fluent\State\FluentState;
/**
* @property string $Title
* @property string $Locale
* @property string $URLSegment
* @property bool $IsGlobalDefault
* @property int $DomainID
* @method HasManyList|FallbackLocale[] FallbackLocales()
* @method ManyManyList|Locale[] Fallbacks()
* @method Domain Domain() Raw SQL Domain (unfiltered by domain mode)
*/
class Locale extends DataObject
{
use CachableModel;
private static $table_name = 'Fluent_Locale';
private static $singular_name = 'Locale';
private static $plural_name = 'Locales';
private static $summary_fields = [
'Title' => 'Title',
'Locale' => 'Locale',
'URLSegment' => 'URL',
'IsGlobalDefault' => 'Global Default',
'Domain.Domain' => 'Domain',
];
/**
* @config
* @var array
*/
private static $db = [
'Title' => 'Varchar(100)',
'Locale' => 'Varchar(10)',
'URLSegment' => 'Varchar(100)',
'IsGlobalDefault' => 'Boolean',
];
private static $default_sort = '"Fluent_Locale"."Locale" ASC';
/**
* @config
* @var array
*/
private static $has_one = [
'Domain' => Domain::class,
];
private static $has_many = [
'FallbackLocales' => FallbackLocale::class . '.Parent',
];
private static $many_many = [
'Fallbacks' => [
'through' => FallbackLocale::class,
'from' => 'Parent',
'to' => 'Locale',
],
];
/**
* @var ArrayList
*/
protected $chain = null;
/**
* @var Locale[]
*/
protected static $locales_by_title;
/**
* Get internal title for this locale
*
* @return string
*/
public function getTitle()
{
$title = $this->getField('Title');
if ($title) {
return $title;
}
return $this->getDefaultTitle();
}
/**
* @return string
*/
protected function getDefaultTitle()
{
// Get default name from locale
return i18n::getData()->localeName($this->getLocale());
}
/**
* Locale code for this object
*
* @return string
*/
public function getLocale()
{
$locale = $this->getField('Locale');
if ($locale) {
return $locale;
}
return $this->getDefaultLocale();
}
/**
* Default locale for
*
* @return string
*/
public function getDefaultLocale()
{
return i18n::config()->get('default_locale');
}
/**
* Get a short segment of the locale code for display in things like badges
*
* @return string e.g. "NZ" for "en_NZ"
*/
public function getLocaleSuffix()
{
$bits = explode('_', $this->Locale);
return array_pop($bits);
}
/**
* Get URLSegment for this locale
*
* @return string
*/
public function getURLSegment()
{
$segment = $this->getField('URLSegment');
if ($segment) {
return $segment;
}
// Default to locale
return $this->getLocale();
}
public function getCMSFields()
{
$fields = FieldList::create(TabSet::create('Root'));
// Main tab
$fields->addFieldsToTab(
'Root.Main',
[
DropdownField::create(
'Locale',
_t(__CLASS__.'.LOCALE', 'Locale'),
i18n::getData()->getLocales()
),
TextField::create(
'Title',
_t(__CLASS__.'.LOCALE_TITLE', 'Title')
)->setAttribute('placeholder', $this->getDefaultTitle()),
TextField::create(
'URLSegment',
_t(__CLASS__.'.LOCALE_URL', 'URL Segment')
)->setAttribute('placeholder', $this->Locale),
$globalDefault = CheckboxField::create(
'IsGlobalDefault',
_t(__CLASS__.'.IS_DEFAULT', 'This is the global default locale')
)
->setAttribute('data-hides', 'ParentDefaultID')
->setDescription(_t(
__CLASS__.'.IS_DEFAULT_DESCRIPTION',
'Note: Per-domain specific locale can be assigned on the Locales tab'
. ' and will override this value for specific domains.'
)),
DropdownField::create(
'DomainID',
_t(__CLASS__.'.DOMAIN', 'Domain'),
Domain::get()->map('ID', 'Domain')
)->setEmptyString(_t(__CLASS__.'.DEFAULT_NONE', '(none)'))
]
);
if ($this->exists()) {
$config = GridFieldConfig::create()
->addComponents(
new GridFieldButtonRow(),
new GridFieldAddNewInlineButton(),
new GridFieldOrderableRows('Sort'),
$editable = new GridFieldEditableColumns(),
new GridFieldDeleteAction()
);
$editable->setDisplayFields([
'LocaleID' => function () {
return DropdownField::create(
'LocaleID',
_t(__CLASS__.'.LOCALE', 'Locale'),
Locale::getCached()->exclude('Locale', $this->Locale)->map('ID', 'Title')
);
}
]);
// Add default selection
$defaultField = GridField::create(
'FallbackLocales',
_t(__CLASS__.'.FALLBACKS', 'Fallback Locales'),
$this->FallbackLocales(),
$config
);
$fields->addFieldToTab('Root.Fallbacks', $defaultField);
} else {
$fields->addFieldToTab(
'Root.Fallbacks',
LiteralField::create(
'UnsavedNotice',
'<p>' . _t(__CLASS__ . '.UnsavedNotice', "You can add fallbacks once you've saved the locale.")
)
);
// If this is the first locale, it should be checked by default
if (static::getCached()->count() === 0) {
$globalDefault->setValue(true);
}
}
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* Get default locale
*
* @param string|null|true $domain If provided, the default locale for the given domain will be returned.
* If true, then the current state domain will be used (if in domain mode).
* @return Locale
*/
public static function getDefault($domain = null)
{
// Get by domain if it exists and has a default
$domainObject = Domain::getByDomain($domain);
if ($domainObject) {
$default = $domainObject->DefaultLocale();
if ($default) {
return $default;
}
}
// Get explicit or implicit default
$locales = static::getLocales();
return $locales->filter('IsGlobalDefault', 1)->first()
?: $locales->first();
}
/**
* Get current locale object
*
* @return Locale
*/
public static function getCurrentLocale()
{
$locale = FluentState::singleton()->getLocale();
return static::getByLocale($locale);
}
/**
* Get object by locale code.
*
* @param string|Locale $locale
* @return Locale
*/
public static function getByLocale($locale)
{
if (!$locale) {
return null;
}
if ($locale instanceof Locale) {
return $locale;
}
if (!static::$locales_by_title) {
static::$locales_by_title = [];
foreach (Locale::getCached() as $localeObj) {
static::$locales_by_title[$localeObj->Locale] = $localeObj;
}
}
// Get filtered locale
return isset(static::$locales_by_title[$locale]) ? static::$locales_by_title[$locale] : null;
}
/**
* Returns whether the given locale matches the current Locale object
*
* @param string $locale E.g. en_NZ, en-NZ, en-nz-1990
* @return bool
*/
public function isLocale($locale)
{
return stripos(i18n::convert_rfc1766($locale), i18n::convert_rfc1766($this->Locale)) === 0;
}
/**
* Check if this is the default (non-global).
* Use IsGlobalDefault check if global default otherwise.
*
* @return bool
*/
public function getIsDefault()
{
// Get default for own domain
$default = static::getDefault($this->getDomain());
// Compare best default with current locale
return $default && ((int)$default->ID === (int)$this->ID);
}
/**
* Get domain if in domain mode
*
* @return Domain|null Domain found, or null if not in domain mode (or no domain)
*/
public function getDomain()
{
if (FluentState::singleton()->getIsDomainMode() && $this->DomainID) {
return Domain::getCached()->byID($this->DomainID);
}
return null;
}
/**
* Determine if this locale is the sole locale on its domain,
* or globally if domain mode is disabled
*
* @return bool
*/
public function getIsOnlyLocale()
{
// Get locales filtered by same domain (in domain mode)
$locales = $this->getSiblingLocales();
return $locales->count() < 2;
}
/**
* Get available locales
*
* @param string|null|true $domain If provided, locales for the given domain will be returned.
* If true, then the current state domain will be used (if in domain mode).
* @return ArrayList
*/
public static function getLocales($domain = null)
{
// Optionally filter by domain
$domainObj = Domain::getByDomain($domain);
if ($domainObj) {
return $domainObj->getLocales();
}
return Locale::getCached();
}
public function onAfterWrite()
{
parent::onAfterWrite();
// If this is the default locale, remove default from other locales
if ($this->IsGlobalDefault) {
$table = $this->baseTable();
DB::prepared_query(
"UPDATE \"{$table}\" SET \"IsGlobalDefault\" = 0 WHERE \"ID\" != ?",
[ $this->ID ]
);
}
}
/**
* Get chain of all locales that should be preferred when this locale is current
*
* @return ArrayList
*/
public function getChain()
{
if ($this->chain) {
return $this->chain;
}
// Build list
$this->chain = ArrayList::create();
$this->chain->push($this);
$this->chain->merge($this->Fallbacks());
return $this->chain;
}
/**
* Fetch a native language string from the {@link i18n} class via the current locale code in the format "XX_xx". In
* the event a match cannot be found in any framework resource, an empty string is returned.
*
* @return string The native language string for the current locale e.g. "português (Brazil)"
*/
public function getNativeName()
{
$locales = i18n::getData();
// Attempts to fetch the native language string via the `i18n::$common_languages` array
if ($native = $locales->languageName($locales->langFromLocale($this->Locale))) {
return $native;
}
return '';
}
/**
* Determine the base URL within the current locale
*
* @return string
*/
public function getBaseURL()
{
$base = Director::baseURL();
// Prepend hostname for domain mode
$domain = $this->getDomain();
if ($domain) {
$base = Controller::join_links($domain->Link(), $base);
}
// Determine if base suffix should be appended
$append = true;
if ($this->getIsDefault()) {
// Apply config
$append = !(bool) Config::inst()->get(FluentDirectorExtension::class, 'disable_default_prefix');
}
if ($append) {
// Append locale url segment
$base = Controller::join_links($base, $this->getURLSegment(), '/');
}
return $base;
}
/**
* Get other locales that appear alongside this (including self)
*
* @return ArrayList
*/
public function getSiblingLocales()
{
$domain = $this->getDomain();
$locales = $domain
? $domain->getLocales()
: Locale::getCached();
return $locales;
}
}