b6e31ea1fb4a8c0be99f7bc7ab96b3a6539f5552
[yaffs-website] / vendor / drupal / console / src / Utils / TranslatorManager.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\TranslatorManager.
6  */
7
8 namespace Drupal\Console\Utils;
9
10 use Drupal\Console\Core\Utils\TranslatorManager as TranslatorManagerBase;
11 use Symfony\Component\Yaml\Exception\ParseException;
12 use Symfony\Component\Finder\Finder;
13
14 /**
15  * Class TranslatorManager
16  *
17  * @package Drupal\Console\Utils
18  */
19 class TranslatorManager extends TranslatorManagerBase
20 {
21     /**
22      * @param $extensionPath
23      */
24     private function addResourceTranslationsByExtensionPath($extensionPath)
25     {
26         $languageDirectory = sprintf(
27             '%s/console/translations/%s',
28             $extensionPath,
29             $this->language
30         );
31
32         if (!is_dir($languageDirectory)) {
33             return;
34         }
35         $finder = new Finder();
36         $finder->files()
37             ->name('*.yml')
38             ->in($languageDirectory);
39         foreach ($finder as $file) {
40             $resource = $languageDirectory . '/' . $file->getBasename();
41             $filename = $file->getBasename('.yml');
42             $key = 'commands.' . $filename;
43             try {
44                 $this->loadTranslationByFile($resource, $key);
45             } catch (ParseException $e) {
46                 echo $key . '.yml ' . $e->getMessage();
47             }
48         }
49     }
50
51     /**
52      * @param $module
53      */
54     private function addResourceTranslationsByModule($module)
55     {
56         if (!\Drupal::moduleHandler()->moduleExists($module)) {
57             return;
58         }
59         $extensionPath = \Drupal::moduleHandler()->getModule($module)->getPath();
60         $this->addResourceTranslationsByExtensionPath(
61             $extensionPath
62         );
63     }
64
65     /**
66      * @param $theme
67      */
68     private function addResourceTranslationsByTheme($theme)
69     {
70         $extensionPath = \Drupal::service('theme_handler')->getTheme($theme)->getPath();
71         $this->addResourceTranslationsByExtensionPath(
72             $extensionPath
73         );
74     }
75
76     /**
77      * @param $extension
78      * @param $type
79      */
80     public function addResourceTranslationsByExtension($extension, $type)
81     {
82         if ($type == 'module') {
83             $this->addResourceTranslationsByModule($extension);
84             return;
85         }
86         if ($type == 'theme') {
87             $this->addResourceTranslationsByTheme($extension);
88             return;
89         }
90     }
91 }