Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / web / core / lib / Drupal / Core / StringTranslation / Translator / StaticTranslation.php
1 <?php
2
3 namespace Drupal\Core\StringTranslation\Translator;
4
5 /**
6  * String translator with a static cache for translations.
7  *
8  * This is a high performance way to provide a handful of string replacements.
9  */
10 class StaticTranslation implements TranslatorInterface {
11
12   /**
13    * String translations
14    *
15    * @var array
16    *   Array of cached translations indexed by language and context.
17    */
18   protected $translations;
19
20   /**
21    * Constructs a translator from an array of translations.
22    *
23    * @param array $translations
24    *   Array of override strings indexed by language and context
25    */
26   public function __construct($translations = []) {
27     $this->translations = $translations;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getStringTranslation($langcode, $string, $context) {
34     if (!isset($this->translations[$langcode])) {
35       $this->translations[$langcode] = $this->getLanguage($langcode);
36     }
37     if (isset($this->translations[$langcode][$context][$string])) {
38       return $this->translations[$langcode][$context][$string];
39     }
40     else {
41       return FALSE;
42     }
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function reset() {
49     $this->translations = [];
50   }
51
52   /**
53    * Retrieves translations for a given language.
54    *
55    * @param string $langcode
56    *   The langcode of the language.
57    *
58    * @return array
59    *   A multidimensional array of translations, indexed by the context the
60    *   source string belongs to. The second level is using original strings as
61    *   keys. An empty array will be returned when no translations are available.
62    */
63   protected function getLanguage($langcode) {
64     // This class is usually a base class but we do not declare as abstract
65     // because it can be used on its own, by passing a simple array on the
66     // constructor. This can be useful while testing, but it does not support
67     // loading specific languages. All available languages should be passed
68     // in the constructor array.
69     return [];
70   }
71
72 }