setDefaultLangcode example

    $menu_name = mb_strtolower($this->randomMachineName(16));
    $label = $this->randomString();
    $edit = [
      'id' => $menu_name,
      'description' => '',
      'label' => $label,
      'langcode' => 'aa',
    ];
    $this->drupalGet('admin/structure/menu/add');
    $this->submitForm($edit, 'Save');
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
      ->setDefaultLangcode('bb')
      ->setLanguageAlterable(TRUE)
      ->save();

    // Check menu language.     $this->assertTrue($this->assertSession()->optionExists('edit-langcode', $edit['langcode'])->isSelected());

    // Test menu link language.     $link_path = '/';

    // Add a menu link.     $link_title = $this->randomString();
    
// Create a "Camelids" node type.     $camelids = NodeType::create([
      'name' => 'Camelids',
      'type' => 'camelids',
    ]);
    $camelids->save();

    $entity = ContentLanguageSettings::create([
      'target_entity_type_id' => 'node',
      'target_bundle' => 'camelids',
    ]);
    $entity->setDefaultLangcode('site_default')
      ->save();

    return $entity;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/language_content_settings/language_content_settings/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      
'administer content translation',
      'translate any entity',
      'create content translations',
      'administer languages',
      'administer content types',
      'administer node fields',
    ]);
    $this->drupalLogin($user);

    // Enable translation for article.     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
    $config->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT);
    $config->setLanguageAlterable(TRUE);
    $config->save();

    $content_translation_manager = $this->container->get('content_translation.manager');
    $content_translation_manager->setEnabled('node', 'article', TRUE);
    $content_translation_manager->setBundleTranslationSettings('node', 'article', [
      'untranslatable_fields_hide' => FALSE,
    ]);

    $this->rebuildContainer();

    

      }
    }

    // Initialize translations. Ensure we have at least an entry for the default     // language.     // We determine if the entity is new by checking in the entity values for     // the presence of the id entity key, as the usage of ::isNew() is not     // possible in the constructor.     $data = isset($values[$this->getEntityType()->getKey('id')]) ? ['status' => static::TRANSLATION_EXISTING] : ['status' => static::TRANSLATION_CREATED];
    $this->translations[LanguageInterface::LANGCODE_DEFAULT] = $data;
    $this->setDefaultLangcode();
    if ($translations) {
      foreach ($translations as $langcode) {
        if ($langcode != $this->defaultLangcode && $langcode != LanguageInterface::LANGCODE_DEFAULT) {
          $this->translations[$langcode] = $data;
        }
      }
    }
    if ($this->getEntityType()->isRevisionable()) {
      // Store the loaded revision ID the entity has been loaded with to       // keep it safe from changes.       $this->updateLoadedRevisionId();
    }
// Create a "Camelids" node type.     $camelids = NodeType::create([
      'name' => 'Camelids',
      'type' => 'camelids',
    ]);
    $camelids->save();

    $entity = ContentLanguageSettings::create([
      'target_entity_type_id' => 'node',
      'target_bundle' => 'camelids',
    ]);
    $entity->setDefaultLangcode('site_default')
      ->save();

    return $entity;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    return [
      'default_langcode' => 'site_default',
      

  public function testApiTranslation(): void {
    $this->container->get('module_installer')->install(['language', 'content_translation']);
    $this->resetAll();
    ConfigurableLanguage::create(['id' => 'fi'])->save();
    $this->container->get('config.factory')->getEditable('language.negotiation')
      ->set('url.source', 'path_prefix')
      ->set('url.prefixes.fi', 'fi')
      ->save();
    $this->rebuildContainer();
    ContentLanguageSettings::loadByEntityTypeBundle('media', 'image')
      ->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();
    $media_fi = Media::load($this->mediaImage->id())->addTranslation('fi');
    $media_fi->field_media_image->setValue([
      [
        'target_id' => '1',
        // cSpell:disable-next-line         'alt' => 'oletus alt-teksti kuvalle',
      ],
    ]);
    $media_fi->save();
    
// Add some custom languages.     foreach (['aa', 'bb', 'cc'] as $language_code) {
      ConfigurableLanguage::create([
        'id' => $language_code,
        'label' => $this->randomMachineName(),
      ])->save();
    }

    // Fixed language.     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
      ->setLanguageAlterable(TRUE)
      ->setDefaultLangcode('bb')
      ->save();

    $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
    $this->assertEquals('bb', $langcode);

    // Current interface.     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
      ->setLanguageAlterable(TRUE)
      ->setDefaultLangcode('current_interface')
      ->save();

    

  protected static $modules = ['language', 'locale'];

  public function testGetLanguages() {
    $this->installSchema('locale', ['locales_source', 'locales_target', 'locales_location']);
    $default_language = ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'default', 'weight' => 0]);
    $default_language->save();

    // Set new default language.     \Drupal::service('language.default')->set($default_language);
    \Drupal::service('string_translation')->setDefaultLangcode($default_language->getId());

    $languages = \Drupal::service('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
    $this->assertEquals(['default', 'und', 'zxx']array_keys($languages));

    $configurableLanguage = ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'test', 'weight' => 1]);
    // Simulate a configuration sync by setting the flag otherwise the locked     // language weights would be updated whilst saving.     // @see \Drupal\language\Entity\ConfigurableLanguage::postSave()     $configurableLanguage->setSyncing(TRUE)->save();

    $languages = \Drupal::service('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
    

  public function testFormatPlural($count$singular$plural, array $args, array $options$expected) {
    $langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
    $translator = $this->createMock('\Drupal\Core\StringTranslation\Translator\TranslatorInterface');
    $translator->expects($this->once())
      ->method('getStringTranslation')
      ->with($langcode$this->anything()$this->anything())
      ->willReturnCallback(function D$langcode$string$context) {
        return $string;
      });
    $this->translationManager->setDefaultLangcode('fr');
    $this->translationManager->addTranslator($translator);
    $result = $this->translationManager->formatPlural($count$singular$plural$args$options);
    $this->assertEquals($expected$result);
    $this->assertInstanceOf(MarkupInterface::class$result);
  }

  /** * Tests translation using placeholders. * * @param string $string * A string containing the English text to translate. * @param array $args * An associative array of replacements to make after translation. * @param string $expected_string * The expected translated string value. * * @dataProvider providerTestTranslatePlaceholder */
// We are rendering this topic for search indexing or search results,     // possibly in a different language than the current language. The topic     // title and body come from translatable things in the Twig template, so we     // need to set the default language to the desired language, render them,     // then restore the default language so we do not affect other cron     // processes. Also, just in case there is an exception, wrap the whole     // thing in a try/finally block, and reset the language in the finally part.     $old_language = $this->defaultLanguage->get();
    try {
      if ($old_language->getId() !== $language->getId()) {
        $this->defaultLanguage->set($language);
        $this->translationManager->setDefaultLangcode($language->getId());
        $this->languageManager->reset();
      }
      $topic = [];

      // Render the title in this language.       $title_build = [
        'title' => [
          '#type' => '#markup',
          '#markup' => $plugin->getLabel(),
        ],
      ];
      

  public function testEntityFieldFilter() {
    NodeType::create(['type' => 'bundle1'])->save();
    NodeType::create(['type' => 'bundle2'])->save();

    // Create some example content.     ConfigurableLanguage::create(['id' => 'es'])->save();
    ConfigurableLanguage::create(['id' => 'fr'])->save();

    ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle1')
      ->setDefaultLangcode('es')
      ->setLanguageAlterable(TRUE)
      ->save();
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'bundle2')
      ->setDefaultLangcode('es')
      ->setLanguageAlterable(TRUE)
      ->save();

    $field_translation_map = [
      1 => ['bundle1' => TRUE, 'bundle2' => TRUE],
      2 => ['bundle1' => FALSE, 'bundle2' => FALSE],
      3 => ['bundle1' => TRUE, 'bundle2' => FALSE],
    ];
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity_types = $form_state->getValue('entity_types');
    foreach ($form_state->getValue('settings') as $entity_type => $entity_settings) {
      foreach ($entity_settings as $bundle => $bundle_settings) {
        $config = ContentLanguageSettings::loadByEntityTypeBundle($entity_type$bundle);
        if (empty($entity_types[$entity_type])) {
          $bundle_settings['settings']['language']['language_alterable'] = FALSE;
        }
        $config->setDefaultLangcode($bundle_settings['settings']['language']['langcode'])
          ->setLanguageAlterable($bundle_settings['settings']['language']['language_alterable'])
          ->save();
      }
    }
    $this->messenger()->addStatus($this->t('Settings successfully updated.'));
  }

}

  private function setLanguageOverrides() {
    $this->negotiator->setCurrentUser($this->currentUser);
    if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
      $this->languageManager->setNegotiator($this->negotiator);
      $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage());
    }
    // After the language manager has initialized, set the default langcode for     // the string translations.     $langcode = $this->languageManager->getCurrentLanguage()->getId();
    $this->translation->setDefaultLangcode($langcode);
  }

  /** * Registers the methods in this class that should be listeners. * * @return array * An array of event listener definitions. */
  public static function getSubscribedEvents(): array {
    $events[KernelEvents::REQUEST][] = ['onKernelRequestLanguage', 255];
    $events[DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED][] = ['onContainerInitializeSubrequestFinished', 255];

    
    $this->assertSourceAttributeSame('alt', NULL);
  }

  /** * Tests the CKEditor 5 media plugin loads the translated alt attribute. */
  public function testTranslationAlt() {
    \Drupal::service('module_installer')->install(['language', 'content_translation']);
    $this->resetAll();
    ConfigurableLanguage::create(['id' => 'fr'])->save();
    ContentLanguageSettings::loadByEntityTypeBundle('media', 'image')
      ->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();
    $media = Media::create([
      'bundle' => 'image',
      'name' => 'Screaming hairy armadillo',
      'field_media_image' => [
        [
          'target_id' => 1,
          'alt' => 'default alt',
          'title' => 'default title',
        ],
      ],
$user = $this->createUser([], '', TRUE);
    $this->drupalLogin($user);
    ConfigurableLanguage::createFromLangcode('es')->save();

    // Create a page node type and make it translatable.     NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ])->save();

    $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
    $config->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();

    // Create a Node with title 'English' and translate it to Spanish.     $node = Node::create([
      'type' => 'page',
      'title' => 'English',
    ]);
    $node->save();
    $node->addTranslation('es', ['title' => 'EspaƱol']);
    $node->save();

    
Home | Imprint | This part of the site doesn't use cookies.