// Use '?:' for empty string check
$locale = $locale ?: $requestLocale;
returnnewLanguage($locale); }
/**
* The Logger class is a PSR-3 compatible Logging class that supports
* multiple handlers that process the actual logging.
*
* @return Logger
*/ publicstaticfunctionlogger(bool $getShared = true) { if($getShared){
$locked_language = [ 'default' => FALSE, 'locked' => TRUE, 'direction' => LanguageInterface::DIRECTION_LTR, ]; // This is called very early while initializing the language system. Prevent
// early t() calls by using the TranslatableMarkup.
$languages[LanguageInterface::LANGCODE_NOT_SPECIFIED] = newLanguage([ 'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'name' => newTranslatableMarkup('Not specified'), 'weight' => ++$weight, ] + $locked_language);
// Set the mock language manager.
$language_manager = $this->createMock('Drupal\Core\Language\LanguageManagerInterface'); $language_manager->expects($this->once()) ->method('getLanguages') ->willReturn([$source => [], $target => []]); $language_manager->expects($this->any()) ->method('getLanguage') ->willReturnMap([ [$source, newLanguage(['id' => $source])], [$target, newLanguage(['id' => $target])], ]);
// Set the mock entity. We need to use ContentEntityBase for mocking due to
// issues with phpunit and multiple interfaces.
$entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') ->disableOriginalConstructor() ->getMock(); $entity->expects($this->once()) ->method('getEntityTypeId'); $entity->expects($this->once())
// If the specified language empty it falls back the same way as in the entity system
// @see \Drupal\Core\Entity\EntityBase::language()
$this->language = $language ?: newLanguage(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); }
/**
* Creates a new ResourceObject from an entity.
*
* @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
* The JSON:API resource type of the resource object.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be represented by this resource object.
* @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links
* (optional) Any links for the resource object, if a `self` link is not
* provided, one will be automatically added if the resource is locatable
* and is not an internal entity.
*
* @return static
* An instantiated resource object.
*/
/**
* {@inheritdoc}
*/ protectedfunctiongetLanguages(){ if(empty($this->languages)){ $this->languages = $this->languageManager()->getLanguages(LanguageInterface::STATE_ALL); // If the entity references a language that is not or no longer available,
// we return a mock language object to avoid disrupting the consuming
// code.
if(!isset($this->languages[$this->defaultLangcode])){ $this->languages[$this->defaultLangcode] = newLanguage(['id' => $this->defaultLangcode]); } } return$this->languages; }
if(!isset($languages[$original_langcode])){ // If the language is not configured on the site, create a dummy language
// object for this listing only to ensure the user gets useful info.
$language_name = $this->languageManager->getLanguageName($original_langcode); $languages[$original_langcode] = newLanguage(['id' => $original_langcode, 'name' => $language_name]); }
// We create a fake request object to pass into
// ConfigMapperInterface::populateFromRouteMatch() for the different languages.
// Creating a separate request for each language and route is neither easily
// possible nor performant.
$fake_request = $request->duplicate();
// Place a site branding block in the header region.
$this->drupalPlaceBlock('system_branding_block', [ 'region' => 'header', 'id' => 'site-branding', ]);
// Access the front page without specifying any valid URL language prefix
// and having as browser language preference a non-default language.
$http_header = ["Accept-Language" => "$langcode_browser_fallback;q=1"]; $language = newLanguage(['id' => '']); $this->drupalGet('', ['language' => $language], $http_header);
// Check that the language switcher active link matches the given browser
// language.
$href = Url::fromRoute('<front>')->toString() . $langcode_browser_fallback; $this->assertSession()->elementTextEquals('xpath', "//div[@id='block-test-language-block']//a[@class='language-link is-active' and starts-with(@href, '$href')]", $languages[$langcode_browser_fallback]->getName());
// Check that URLs are rewritten using the given browser language.
$this->assertSession()->elementTextEquals('xpath', "//div[@id='block-site-branding']/a[@rel='home' and @href='$href'][2]", 'Drupal'); }