*/ protected array $dependencies = []; /** * Only use the constructor directly if you know what you are doing and want to create custom locales with custom dependencies. * Otherwise, instantiate a new Locale object via Locales->createLocale() as it deals with registered dependencies automatically. */ public function __construct( string $locale = 'en', array $dependencies = [] ) { $locale = $this->normalize($locale); if (str_contains($locale, '@')) { [$locale, $this->charsetModifier] = explode('@', $locale); } if (str_contains($locale, '.')) { [$locale, $this->codeSet] = explode('.', $locale); } if (strtolower($locale) === 'c') { $this->codeSet = 'C'; $locale = 'en'; } elseif (strtolower($locale) === 'posix') { $this->codeSet = 'POSIX'; $locale = 'en'; } if (str_contains($locale, '-')) { [$this->languageCode, $tail] = explode('-', $locale, 2); if (str_contains($tail, '-')) { [$this->languageScript, $this->countryCode] = explode('-', $tail); } elseif (strlen($tail) === 4) { $this->languageScript = $tail; } else { $this->countryCode = $tail ?: null; } $this->languageCode = strtolower($this->languageCode); $this->languageScript = $this->languageScript ? ucfirst(strtolower($this->languageScript)) : null; $this->countryCode = $this->countryCode ? strtoupper($this->countryCode) : null; } else { $this->languageCode = strtolower($locale); } $this->locale = $this->languageCode . ($this->languageScript ? '-' . $this->languageScript : '') . ($this->countryCode ? '-' . $this->countryCode : ''); $this->dependencies = array_map($this->normalize(...), $dependencies); } public function getName(): string { return $this->locale; } public function getLanguageCode(): string { return $this->languageCode; } public function isRightToLeftLanguageDirection(): bool { return in_array($this->languageCode, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true) || in_array($this->locale, self::RIGHT_TO_LEFT_LANGUAGE_CODES, true); } public function getLanguageScriptCode(): ?string { return $this->languageScript; } public function getCountryCode(): ?string { return $this->countryCode; } /** * Return the locale as ISO/IEC 15897 format, including a possible POSIX charset * "cs_CZ.UTF-8" * see https://en.wikipedia.org/wiki/ISO/IEC_15897 * https://en.wikipedia.org/wiki/Locale_(computer_software)#POSIX_platforms * @internal */ public function posixFormatted(): string { $charsetModifier = $this->charsetModifier ? '@' . $this->charsetModifier : ''; if ($this->codeSet === 'C' || $this->codeSet === 'POSIX') { return $this->codeSet . $charsetModifier; } $formatted = $this->languageCode; if ($this->countryCode) { $formatted .= '_' . $this->countryCode; } if ($this->codeSet) { $formatted .= '.' . $this->codeSet; } return $formatted . $charsetModifier; } /** * @internal */ public function getPosixCodeSet(): ?string { return $this->codeSet; } public function getDependencies(): array { return $this->dependencies; } protected function normalize(string $locale): string { if ($locale === 'default') { return 'en'; } if (str_contains($locale, '_')) { $locale = str_replace('_', '-', $locale); } return $locale; } public function __toString(): string { return $this->locale; } }