‪TYPO3CMS  ‪main
Locale.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
29 class ‪Locale implements \Stringable
30 {
31  protected string ‪$locale;
32  protected string ‪$languageCode;
33  protected ?string ‪$languageScript = null;
34  protected ?string ‪$countryCode = null;
35  protected ?string ‪$codeSet = null;
36  // see https://wiki.archlinux.org/title/locale#Generating_locales
37  protected ?string ‪$charsetModifier = null;
38 
39  // taken from https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code
40  protected const ‪RIGHT_TO_LEFT_LANGUAGE_CODES = [
41  'ar', // Arabic
42  'arc', // Aramaic
43  'arz', // Egyptian Arabic
44  'ckb', // Kurdish (Sorani)
45  'dv', // Divehi
46  'fa', // Persian
47  'ha', // Hausa
48  'he', // Hebrew
49  'khw', // Khowar
50  'ks', // Kashmiri
51  'ps', // Pashto
52  'sd', // Sindhi
53  'ur', // Urdu
54  'uz-AF', // Uzbeki Afghanistan
55  'yi', // Yiddish
56  ];
57 
64  protected array ‪$dependencies = [];
65 
70  public function ‪__construct(
71  string ‪$locale = 'en',
72  array ‪$dependencies = []
73  ) {
74  ‪$locale = $this->‪normalize($locale);
75  if (str_contains(‪$locale, '@')) {
77  }
78  if (str_contains(‪$locale, '.')) {
79  [‪$locale, ‪$this->codeSet] = explode('.', ‪$locale);
80  }
81  if (strtolower(‪$locale) === 'c') {
82  $this->codeSet = 'C';
83  ‪$locale = 'en';
84  } elseif (strtolower(‪$locale) === 'posix') {
85  $this->codeSet = 'POSIX';
86  ‪$locale = 'en';
87  }
88  if (str_contains(‪$locale, '-')) {
89  [‪$this->languageCode, $tail] = explode('-', ‪$locale, 2);
90  if (str_contains($tail, '-')) {
91  [‪$this->languageScript, ‪$this->countryCode] = explode('-', $tail);
92  } elseif (strlen($tail) === 4) {
93  $this->languageScript = $tail;
94  } else {
95  $this->countryCode = $tail ?: null;
96  }
97  $this->languageCode = strtolower($this->languageCode);
98  $this->languageScript = $this->languageScript ? ucfirst(strtolower($this->languageScript)) : null;
99  $this->countryCode = $this->countryCode ? strtoupper($this->countryCode) : null;
100  } else {
101  $this->languageCode = strtolower(‪$locale);
102  }
103 
104  $this->locale = $this->languageCode . ($this->languageScript ? '-' . $this->languageScript : '') . ($this->countryCode ? '-' . $this->countryCode : '');
105  $this->dependencies = array_map($this->‪normalize(...), $dependencies);
106  }
107 
108  public function ‪getName(): string
109  {
110  return ‪$this->locale;
111  }
112 
113  public function ‪getLanguageCode(): string
114  {
115  return ‪$this->languageCode;
116  }
117 
118  public function ‪isRightToLeftLanguageDirection(): bool
119  {
120  return in_array($this->languageCode, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true) || in_array($this->locale, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true);
121  }
122 
123  public function ‪getLanguageScriptCode(): ?string
124  {
126  }
127 
128  public function ‪getCountryCode(): ?string
129  {
130  return ‪$this->countryCode;
131  }
132 
140  public function ‪posixFormatted(): string
141  {
142  ‪$charsetModifier = $this->charsetModifier ? '@' . $this->charsetModifier : '';
143  if ($this->codeSet === 'C' || $this->codeSet === 'POSIX') {
144  return $this->codeSet . ‪$charsetModifier;
145  }
146  $formatted = ‪$this->languageCode;
147  if ($this->countryCode) {
148  $formatted .= '_' . ‪$this->countryCode;
149  }
150  if ($this->codeSet) {
151  $formatted .= '.' . ‪$this->codeSet;
152  }
153  return $formatted . ‪$charsetModifier;
154  }
155 
159  public function ‪getPosixCodeSet(): ?string
160  {
161  return ‪$this->codeSet;
162  }
163 
164  public function ‪getDependencies(): array
165  {
166  return ‪$this->dependencies;
167  }
168 
169  protected function ‪normalize(string ‪$locale): string
170  {
171  if (‪$locale === 'default') {
172  return 'en';
173  }
174  if (str_contains(‪$locale, '_')) {
175  ‪$locale = str_replace('_', '-', ‪$locale);
176  }
177 
178  return ‪$locale;
179  }
180 
181  public function ‪__toString(): string
182  {
183  return ‪$this->locale;
184  }
185 }
‪TYPO3\CMS\Core\Localization\Locale\getLanguageCode
‪getLanguageCode()
Definition: Locale.php:113
‪TYPO3\CMS\Core\Localization\Locale\$charsetModifier
‪string $charsetModifier
Definition: Locale.php:37
‪TYPO3\CMS\Core\Localization\Locale\posixFormatted
‪posixFormatted()
Definition: Locale.php:140
‪TYPO3\CMS\Core\Localization\Locale\getLanguageScriptCode
‪getLanguageScriptCode()
Definition: Locale.php:123
‪TYPO3\CMS\Core\Localization\Locale\$codeSet
‪string $codeSet
Definition: Locale.php:35
‪TYPO3\CMS\Core\Localization\Locale\getPosixCodeSet
‪getPosixCodeSet()
Definition: Locale.php:159
‪TYPO3\CMS\Core\Localization\Locale\$dependencies
‪array $dependencies
Definition: Locale.php:64
‪TYPO3\CMS\Core\Localization\Locale\getDependencies
‪getDependencies()
Definition: Locale.php:164
‪TYPO3\CMS\Core\Localization\Locale\__toString
‪__toString()
Definition: Locale.php:181
‪TYPO3\CMS\Core\Localization\Locale\$languageCode
‪string $languageCode
Definition: Locale.php:32
‪TYPO3\CMS\Core\Localization\Locale\__construct
‪__construct(string $locale='en', array $dependencies=[])
Definition: Locale.php:70
‪TYPO3\CMS\Core\Localization\Locale\RIGHT_TO_LEFT_LANGUAGE_CODES
‪const RIGHT_TO_LEFT_LANGUAGE_CODES
Definition: Locale.php:40
‪TYPO3\CMS\Core\Localization\Locale\normalize
‪normalize(string $locale)
Definition: Locale.php:169
‪TYPO3\CMS\Core\Localization
Definition: CacheWarmer.php:18
‪TYPO3\CMS\Core\Localization\Locale\getName
‪getName()
Definition: Locale.php:108
‪TYPO3\CMS\Core\Localization\Locale\$locale
‪string $locale
Definition: Locale.php:31
‪TYPO3\CMS\Core\Localization\Locale
Definition: Locale.php:30
‪TYPO3\CMS\Core\Localization\Locale\getCountryCode
‪getCountryCode()
Definition: Locale.php:128
‪TYPO3\CMS\Core\Localization\Locale\isRightToLeftLanguageDirection
‪isRightToLeftLanguageDirection()
Definition: Locale.php:118
‪TYPO3\CMS\Core\Localization\Locale\$countryCode
‪string $countryCode
Definition: Locale.php:34
‪TYPO3\CMS\Core\Localization\Locale\$languageScript
‪string $languageScript
Definition: Locale.php:33