-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathGraphQLLanguageContext.php
More file actions
121 lines (109 loc) · 3.44 KB
/
GraphQLLanguageContext.php
File metadata and controls
121 lines (109 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Drupal\graphql;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationManager;
/**
* Simple service that stores the current GraphQL language state.
*/
class GraphQLLanguageContext {
/**
* Indicates if the GraphQL context language is currently active.
*
* @var bool
*/
protected $isActive;
/**
* The current language context.
*
* @var string
*/
protected $currentLanguage;
/**
* @var \SplStack
*/
protected $languageStack;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The string translation service
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $translationManager;
/**
* GraphQLLanguageContext constructor.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager service.
*/
public function __construct(LanguageManagerInterface $languageManager, TranslationManager $translationManager) {
$this->languageManager = $languageManager;
$this->translationManager = $translationManager;
$this->languageStack = new \SplStack();
}
/**
* Retrieve the current language.
*
* @return string|null
* The current language code, or null if the context is not active.
*/
public function getCurrentLanguage() {
return $this->isActive
? ($this->currentLanguage ?: $this->languageManager->getCurrentLanguage()->getId())
: NULL;
}
/**
* Executes a callable in a defined language context.
*
* @param callable $callable
* The callable to be executed.
* @param string $language
* The langcode to be set.
*
* @return mixed
* The callables result.
*
* @throws \Exception
* Any exception caught while executing the callable.
*/
public function executeInLanguageContext(callable $callable, $language) {
$this->languageStack->push($this->currentLanguage);
$this->currentLanguage = $language;
$this->isActive = TRUE;
$this->languageManager->reset();
// This is needed to be able to use the string translation with the
// requested language.
$this->translationManager->setDefaultLangcode($language);
// Override the configuration language so that config entities (like menus)
// are loaded using the proper translation.
$currentConfigLanguage = $this->languageManager->getConfigOverrideLanguage();
if ($currentConfigLanguage->getId() !== $language) {
$configLanguage = $this->languageManager->getLanguage($language);
$this->languageManager->setConfigOverrideLanguage($configLanguage);
}
// Extract the result array.
try {
return call_user_func($callable);
}
catch (\Exception $exc) {
throw $exc;
}
finally {
// In any case, set the language context back to null.
$this->currentLanguage = $this->languageStack->pop();
$this->isActive = FALSE;
$this->languageManager->reset();
// Restore the languages for the translation and language managers.
$defaultLangcode = !empty($this->currentLanguage)
? $this->currentLanguage
: $this->languageManager->getCurrentLanguage()->getId();
$this->translationManager->setDefaultLangcode($defaultLangcode);
$this->languageManager->setConfigOverrideLanguage($currentConfigLanguage);
}
}
}