<?php

/**
 * @file
 * Set up the WxT Extend Translation module.
 */

use Drupal\locale\SourceString;

/**
 * Implements hook_install().
 */
function wxt_translation_install() {
  // Enable translation for our core entities/bundles.
  $entities = [
    'menu_link_content' => [
      'menu_link_content',
    ],
  ];
  wxt_translation_enable_translation($entities);

  // Download the newest config translations from Drupal.org.
  wxt_translation_update_config_translation();

  // Translate User Account link.
  $storageMenuLink = \Drupal::service('entity_type.manager')->getStorage('menu_link_content');
  $links_title = [
    'User Account' => 'Compte de l\'utilisateur',
  ];

  foreach ($links_title as $link_title_en => $link_title_fr) {
    $link = $storageMenuLink->loadByProperties(['title' => $link_title_en]);
    $link = reset($link);
    if ($link && !$link->hasTranslation('fr')) {
      $link->addTranslation('fr', ['title' => $link_title_fr])->save();
    }
  }

  // Translate the "Did you find what you were looking for?" webform title.
  $storage = \Drupal::service('locale.storage');
  $source_string = "Did you find what you were looking for?";
  $string = $storage->findString([
    'source' => $source_string,
  ]);

  if (is_null($string)) {
    $string = new SourceString();
    $string->setString($source_string);
    $string->setStorage($storage);
    $string->save();
  }
  // Create the translation. If it already exists, it will be replaced.
  $storage->createTranslation([
    'lid' => $string->lid,
    'language' => "fr",
    'translation' => "Avez-vous trouvé ce que vous cherchiez?",
  ])->save();

  // Translate Date Format.
  $dateConfig = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'core.date_format.wxt_standard');
  $dateConfig->set('pattern', 'd F Y')
    ->save();
}
