‪TYPO3CMS  11.5
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 
66  public function ‪__construct(
67  string ‪$locale = 'en',
68  array ‪$dependencies = []
69  ) {
70  ‪$locale = $this->‪normalize($locale);
71  if (str_contains(‪$locale, '@')) {
73  }
74  if (str_contains(‪$locale, '.')) {
75  [‪$locale, ‪$this->codeSet] = explode('.', ‪$locale);
76  }
77  if (strtolower(‪$locale) === 'c') {
78  $this->codeSet = 'C';
79  ‪$locale = 'en';
80  } elseif (strtolower(‪$locale) === 'posix') {
81  $this->codeSet = 'POSIX';
82  ‪$locale = 'en';
83  }
84  if (str_contains(‪$locale, '-')) {
85  [‪$this->languageCode, $tail] = explode('-', ‪$locale, 2);
86  if (str_contains($tail, '-')) {
87  [‪$this->languageScript, ‪$this->countryCode] = explode('-', $tail);
88  } elseif (strlen($tail) === 4) {
89  $this->languageScript = $tail;
90  } else {
91  $this->countryCode = $tail ?: null;
92  }
93  $this->languageCode = strtolower($this->languageCode);
94  $this->languageScript = $this->languageScript ? ucfirst(strtolower($this->languageScript)) : null;
95  $this->countryCode = $this->countryCode ? strtoupper($this->countryCode) : null;
96  } else {
97  $this->languageCode = strtolower(‪$locale);
98  }
99 
100  $this->locale = $this->languageCode . ($this->languageScript ? '-' . $this->languageScript : '') . ($this->countryCode ? '-' . $this->countryCode : '');
101  $this->dependencies = array_map(fn($dep) => $this->‪normalize($dep), ‪$dependencies);
102  }
103 
104  public function ‪getName(): string
105  {
106  return ‪$this->locale;
107  }
108 
109  public function ‪getLanguageCode(): string
110  {
111  return ‪$this->languageCode;
112  }
113 
114  public function ‪isRightToLeftLanguageDirection(): bool
115  {
116  return in_array($this->languageCode, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true) || in_array($this->locale, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true);
117  }
118 
119  public function ‪getLanguageScriptCode(): ?string
120  {
122  }
123 
124  public function ‪getCountryCode(): ?string
125  {
126  return ‪$this->countryCode;
127  }
128 
136  public function ‪posixFormatted(): string
137  {
138  ‪$charsetModifier = $this->charsetModifier ? '@' . $this->charsetModifier : '';
139  if ($this->codeSet === 'C' || $this->codeSet === 'POSIX') {
140  return $this->codeSet . ‪$charsetModifier;
141  }
142  $formatted = ‪$this->languageCode;
143  if ($this->countryCode) {
144  $formatted .= '_' . ‪$this->countryCode;
145  }
146  if ($this->codeSet) {
147  $formatted .= '.' . ‪$this->codeSet;
148  }
149  return $formatted . ‪$charsetModifier;
150  }
151 
155  public function ‪getPosixCodeSet(): ?string
156  {
157  return ‪$this->codeSet;
158  }
159 
160  public function ‪getDependencies(): array
161  {
162  return ‪$this->dependencies;
163  }
164 
165  protected function ‪normalize(string ‪$locale): string
166  {
167  if (‪$locale === 'default') {
168  return 'en';
169  }
170  if (str_contains(‪$locale, '_')) {
171  ‪$locale = str_replace('_', '-', ‪$locale);
172  }
173 
174  return ‪$locale;
175  }
176 
177  public function ‪__toString(): string
178  {
179  return ‪$this->locale;
180  }
181 }
‪TYPO3\CMS\Core\Localization\Locale\getLanguageCode
‪getLanguageCode()
Definition: Locale.php:109
‪TYPO3\CMS\Core\Localization\Locale\$charsetModifier
‪string $charsetModifier
Definition: Locale.php:37
‪TYPO3\CMS\Core\Localization\Locale\posixFormatted
‪posixFormatted()
Definition: Locale.php:136
‪TYPO3\CMS\Core\Localization\Locale\getLanguageScriptCode
‪getLanguageScriptCode()
Definition: Locale.php:119
‪TYPO3\CMS\Core\Localization\Locale\$codeSet
‪string $codeSet
Definition: Locale.php:35
‪TYPO3\CMS\Core\Localization\Locale\getPosixCodeSet
‪getPosixCodeSet()
Definition: Locale.php:155
‪TYPO3\CMS\Core\Localization\Locale\$dependencies
‪array $dependencies
Definition: Locale.php:64
‪TYPO3\CMS\Core\Localization\Locale\getDependencies
‪getDependencies()
Definition: Locale.php:160
‪TYPO3\CMS\Core\Localization\Locale\__toString
‪__toString()
Definition: Locale.php:177
‪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:66
‪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:165
‪TYPO3\CMS\Core\Localization
Definition: CacheWarmer.php:18
‪TYPO3\CMS\Core\Localization\Locale\getName
‪getName()
Definition: Locale.php:104
‪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:124
‪TYPO3\CMS\Core\Localization\Locale\isRightToLeftLanguageDirection
‪isRightToLeftLanguageDirection()
Definition: Locale.php:114
‪TYPO3\CMS\Core\Localization\Locale\$countryCode
‪string $countryCode
Definition: Locale.php:34
‪TYPO3\CMS\Core\Localization\Locale\$languageScript
‪string $languageScript
Definition: Locale.php:33