vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/Translation.php line 101

Open in your IDE?
  1. <?php
  2. /*
  3.  * Isotope eCommerce for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2009 - 2019 terminal42 gmbh & Isotope eCommerce Workgroup
  6.  *
  7.  * @link       https://isotopeecommerce.org
  8.  * @license    https://opensource.org/licenses/lgpl-3.0.html
  9.  */
  10. namespace Isotope;
  11. use Contao\Model\Collection;
  12. use Contao\StringUtil;
  13. use Isotope\Model\Label;
  14. /**
  15.  * Translates labels
  16.  */
  17. class Translation
  18. {
  19.     /**
  20.      * Labels
  21.      * @var array
  22.      */
  23.     protected static $arrLabels = array();
  24.     /**
  25.      * Labels loaded
  26.      * @var array
  27.      */
  28.     protected static $arrLoaded = array();
  29.     /**
  30.      * Get a translation of a value using the translation label
  31.      *
  32.      * @param mixed  $varLabel
  33.      * @param string $strLanguage
  34.      *
  35.      * @return mixed
  36.      */
  37.     public static function get($varLabel$strLanguage null)
  38.     {
  39.         if (!\Database::getInstance()->tableExists(Label::getTable())) {
  40.             return $varLabel;
  41.         }
  42.         if (null === $strLanguage) {
  43.             $strLanguage $GLOBALS['TL_LANGUAGE'];
  44.         }
  45.         // Convert Language Tag to Locale ID
  46.         $strLanguage str_replace('-''_'$strLanguage);
  47.         // Recursively translate label array
  48.         if (\is_array($varLabel)) {
  49.             foreach ($varLabel as $k => $v) {
  50.                 $varLabel[$k] = static::get($v$strLanguage);
  51.             }
  52.             return $varLabel;
  53.         }
  54.         // Load labels
  55.         static::initialize($strLanguage);
  56.         $varLabel StringUtil::decodeEntities($varLabel);
  57.         if (isset(static::$arrLabels[$strLanguage][$varLabel])) {
  58.             return static::$arrLabels[$strLanguage][$varLabel];
  59.         }
  60.         return $varLabel;
  61.     }
  62.     /**
  63.      * Add a translation that is not stored in translation table
  64.      *
  65.      * @param string $strLabel       The label
  66.      * @param string $strReplacement The replacement
  67.      * @param string $strLanguage    The language
  68.      */
  69.     public static function add($strLabel$strReplacement$strLanguage null)
  70.     {
  71.         if (null === $strLanguage) {
  72.             $strLanguage $GLOBALS['TL_LANGUAGE'];
  73.         }
  74.         static::initialize($strLanguage);
  75.         static::$arrLabels[$strLanguage][StringUtil::decodeEntities($strLabel)] = $strReplacement;
  76.     }
  77.     /**
  78.      * Initialize the data in translation table
  79.      *
  80.      * @param string $strLanguage The language
  81.      */
  82.     protected static function initialize($strLanguage null)
  83.     {
  84.         if (null === $strLanguage) {
  85.             $strLanguage $GLOBALS['TL_LANGUAGE'];
  86.         }
  87.         if (!isset(static::$arrLoaded[$strLanguage])) {
  88.             /** @var Label[]|Collection $objLabels */
  89.             $objLabels Label::findBy('language'$strLanguage);
  90.             if (null !== $objLabels) {
  91.                 while ($objLabels->next()) {
  92.                     static::$arrLabels[$strLanguage][StringUtil::decodeEntities($objLabels->label)] = $objLabels->replacement;
  93.                 }
  94.             }
  95.             static::$arrLoaded[$strLanguage] = true;
  96.         }
  97.     }
  98. }