/** * Get name format based on locale, if locale is not passed locale from system configuration will be used. * * @param string|null $locale * @throws \RuntimeException */ public function getNameFormat($locale = null) { if (!$locale) { $locale = $this->localeSettings->getLocale(); } $nameFormats = $this->localeSettings->getNameFormats(); // match by locale (for example - "fr_CA") if (isset($nameFormats[$locale])) { return $nameFormats[$locale]; } // match by locale language (for example - "fr") $localeParts = \Locale::parseLocale($locale); if (isset($localeParts[\Locale::LANG_TAG])) { $match = $localeParts[\Locale::LANG_TAG]; if (isset($match, $nameFormats[$match])) { return $nameFormats[$match]; } } // match by default locale in system configuration settings $match = $this->localeSettings->getLocale(); if ($match !== $locale && isset($nameFormats[$match])) { return $nameFormats[$match]; } // fallback to default constant locale $match = LocaleConfiguration::DEFAULT_LOCALE; if (isset($nameFormats[$match])) { return $nameFormats[$match]; } throw new \RuntimeException(sprintf('Cannot get name format for "%s"', $locale)); }
public function testAddNameFormats() { $enFormat = '%first_name% %middle_name% %last_name%'; $enFormatModified = '%prefix% %%first_name% %middle_name% %last_name% %suffix%'; $ruFormat = '%last_name% %first_name% %middle_name%'; $this->assertEmpty($this->localeSettings->getNameFormats()); $this->localeSettings->addNameFormats(['en' => $enFormat]); $this->assertEquals(['en' => $enFormat], $this->localeSettings->getNameFormats()); $this->localeSettings->addNameFormats(['en' => $enFormatModified, 'ru' => $ruFormat]); $this->assertEquals(['en' => $enFormatModified, 'ru' => $ruFormat], $this->localeSettings->getNameFormats()); }