-
Notifications
You must be signed in to change notification settings - Fork 821
/
DBLocale.php
72 lines (63 loc) · 1.6 KB
/
DBLocale.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
<?php
namespace SilverStripe\ORM\FieldType;
use SilverStripe\i18n\i18n;
/**
* Locale database field, mainly used in {@link Translatable} extension.
*
* @todo Allowing showing locale values in different languages through Nice()
*/
class DBLocale extends DBVarchar
{
public function __construct($name = null, $size = 16)
{
parent::__construct($name, $size);
}
/**
* See {@link getShortName()} and {@link getNativeName()}.
*
* @param Boolean $showNative Show a localized version of the name instead, based on the
* field's locale value.
* @return String
*/
public function Nice($showNative = false)
{
if ($showNative) {
return $this->getNativeName();
}
return $this->getShortName();
}
public function RFC1766()
{
return i18n::convert_rfc1766($this->value);
}
/**
* Resolves the locale to a common english-language
* name through {@link i18n::get_common_locales()}.
*
* @return string
*/
public function getShortName()
{
return i18n::getData()->languageName($this->value);
}
/**
* @return string
*/
public function getLongName()
{
return i18n::getData()->localeName($this->value);
}
/**
* Returns the localized name based on the field's value.
* Example: "de_DE" returns "Deutsch".
*
* @return string
*/
public function getNativeName()
{
$locale = $this->value;
return i18n::with_locale($locale, function () {
return $this->getShortName();
});
}
}