You are here

class I18n in Bamboo Twig 8.5

Same name and namespace in other branches
  1. 8.2 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n
  2. 8.3 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n
  3. 8.4 bamboo_twig_i18n/src/TwigExtension/I18n.php \Drupal\bamboo_twig_i18n\TwigExtension\I18n

Provides some 'Internationalization' Twig Extensions.

Hierarchy

  • class \Drupal\bamboo_twig\TwigExtension\TwigExtensionBase extends \Drupal\bamboo_twig\TwigExtension\Twig_Extension uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
    • class \Drupal\bamboo_twig_i18n\TwigExtension\I18n

Expanded class hierarchy of I18n

1 string reference to 'I18n'
bamboo_twig_i18n.services.yml in bamboo_twig_i18n/bamboo_twig_i18n.services.yml
bamboo_twig_i18n/bamboo_twig_i18n.services.yml
1 service uses I18n
bamboo_twig_i18n.twig.i18n in bamboo_twig_i18n/bamboo_twig_i18n.services.yml
Drupal\bamboo_twig_i18n\TwigExtension\I18n

File

bamboo_twig_i18n/src/TwigExtension/I18n.php, line 14

Namespace

Drupal\bamboo_twig_i18n\TwigExtension
View source
class I18n extends TwigExtensionBase {

  /**
   * List of all Twig functions.
   */
  public function getFilters() {
    return [
      new TwigFilter('bamboo_i18n_format_date', [
        $this,
        'formatDate',
      ], [
        'needs_environment' => TRUE,
      ]),
      new TwigFilter('bamboo_i18n_get_translation', [
        $this,
        'getTranslation',
      ]),
    ];
  }

  /**
   * List of all Twig functions.
   */
  public function getFunctions() {
    return [
      new TwigFunction('bamboo_i18n_current_lang', [
        $this,
        'getCurrentLanguage',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig_i18n.twig.i18n';
  }

  /**
   * Retrieve the current language.
   */
  public function getCurrentLanguage() {
    return $this
      ->getLanguageManager()
      ->getCurrentLanguage()
      ->getId();
  }

  /**
   * Render a custom date format with Twig.
   *
   * Use the internal helper "format_date" to render the date
   * using the current language for texts.
   *
   * @param \Drupal\Core\Template\TwigEnvironment $env
   *   A Twig_Environment instance.
   * @param int|string|DateTime $date
   *   A string, integer timestamp or DateTime object to convert.
   * @param string $type
   *   (optional) The format to use, one of:
   *   - One of the built-in formats: 'short', 'medium',
   *     'long', 'html_datetime', 'html_date', 'html_time',
   *     'html_yearless_date', 'html_week', 'html_month', 'html_year'.
   *   - The name of a date type defined by a date format config entity.
   *   - The machine name of an administrator-defined date format.
   *   - 'custom', to use $format.
   *   Defaults to 'medium'.
   * @param string $format
   *   (optional) If $type is 'custom', a PHP date format string suitable for
   *   input to date(). Use a backslash to escape ordinary text, so it does not
   *   get interpreted as date format characters.
   * @param string|null $timezone
   *   (optional) Time zone identifier, as described at
   *   http://php.net/manual/timezones.php Defaults to the time zone used to
   *   display the page.
   * @param string|null $langcode
   *   (optional) Language code to translate to. NULL (default) means to use
   *   the user interface language for the page.
   *
   * @return string|null
   *   A translated date string in the requested format. Since the format may
   *   contain user input, this value should be escaped when output.
   */
  public function formatDate(TwigEnvironment $env, $date, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
    $date = twig_date_converter($env, $date);
    if ($date instanceof \DateTime) {
      return $this
        ->getDateFormatter()
        ->format($date
        ->getTimestamp(), $type, $format, $timezone, $langcode);
    }
    return NULL;
  }

  /**
   * Gets a translation of the entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to translate.
   * @param string|null $langcode
   *   (optional) The language code of the translation to get.
   *   NULL (default) means to use the user interface language for the page.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   An entity object when translations exists or the original entity.
   */
  public function getTranslation(EntityInterface $entity, $langcode = NULL) {
    $entityRepository = $this
      ->getEntityRepository();
    if (!$langcode) {
      $langcode = $this
        ->getCurrentLanguage();
    }
    return $entityRepository
      ->getTranslationFromContext($entity, $langcode);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
I18n::formatDate public function Render a custom date format with Twig.
I18n::getCurrentLanguage public function Retrieve the current language.
I18n::getFilters public function List of all Twig functions.
I18n::getFunctions public function List of all Twig functions.
I18n::getName public function Unique identifier for this Twig extension. Overrides TwigExtensionBase::getName
I18n::getTranslation public function Gets a translation of the entity.
TwigExtensionBase::getBlockStorage protected function Return the block storage.
TwigExtensionBase::getConfigFactory protected function Provides an interface for a configuration object factory.
TwigExtensionBase::getCurrentRouteMatch protected function Return the current route match.
TwigExtensionBase::getCurrentUser protected function Lazy loading for the Drupal current user account proxy.
TwigExtensionBase::getDateFormatter protected function Provides a service to handle various date related functionality.
TwigExtensionBase::getEntityRepository protected function Lazy loading for the Drupal entity repository.
TwigExtensionBase::getEntityTypeManager protected function Lazy loading for the Drupal entity type manager.
TwigExtensionBase::getExtensionGuesser protected function Return a singleton mime type to file extension guesser.
TwigExtensionBase::getFieldTypeManager protected function Return the factory for image objects.
TwigExtensionBase::getFileStorage protected function Return the file storage.
TwigExtensionBase::getFileSystemObject protected function Provides helpers to operate on files and stream wrappers.
TwigExtensionBase::getFormBuilder protected function Provides an interface for form building and processing.
TwigExtensionBase::getImageFactory protected function Return the factory for image objects.
TwigExtensionBase::getImageStyleStorage protected function Provides an interface defining an image style.
TwigExtensionBase::getLanguageManager protected function Returns the language manager service.
TwigExtensionBase::getMenuLinkTree protected function Interface for loading, transforming and rendering menu link trees.
TwigExtensionBase::getPluginManagerBlock protected function Manages discovery and instantiation of block plugins.
TwigExtensionBase::getSettingsSingleton protected function Read only settings singleton.
TwigExtensionBase::getStateFactory protected function The state storage service.
TwigExtensionBase::getStreamWrapperManager protected function Provides a StreamWrapper manager.
TwigExtensionBase::getToken protected function Return the token service.
TwigExtensionBase::getUserStorage protected function Return the user storage.