vendor/pimcore/pimcore/lib/Localization/LocaleService.php line 97

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Localization;
  15. use Pimcore\Translation\Translator;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class LocaleService implements LocaleServiceInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $locale;
  23. /**
  24. * @var null|RequestStack
  25. */
  26. protected $requestStack;
  27. /**
  28. * @var Translator|null
  29. */
  30. protected $translator;
  31. /**
  32. * @param RequestStack|null $requestStack
  33. * @param Translator|null $translator
  34. */
  35. public function __construct(RequestStack $requestStack = null, Translator $translator = null)
  36. {
  37. $this->requestStack = $requestStack;
  38. $this->translator = $translator;
  39. }
  40. /**
  41. * @param string $locale
  42. *
  43. * @return bool
  44. */
  45. public function isLocale($locale)
  46. {
  47. $locales = array_flip($this->getLocaleList());
  48. $exists = isset($locales[$locale]);
  49. return $exists;
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function findLocale()
  55. {
  56. if ($requestLocale = $this->getLocaleFromRequest()) {
  57. return $requestLocale;
  58. }
  59. $defaultLocale = \Pimcore\Tool::getDefaultLanguage();
  60. if ($defaultLocale) {
  61. return $defaultLocale;
  62. }
  63. return '';
  64. }
  65. /**
  66. * @return null|string
  67. */
  68. protected function getLocaleFromRequest()
  69. {
  70. if ($this->requestStack) {
  71. $masterRequest = $this->requestStack->getMasterRequest();
  72. if ($masterRequest) {
  73. return $masterRequest->getLocale();
  74. }
  75. }
  76. return null;
  77. }
  78. /**
  79. * @return array
  80. */
  81. public function getLocaleList()
  82. {
  83. $locales = \ResourceBundle::getLocales(null);
  84. return $locales;
  85. }
  86. /**
  87. * @param string|null $locale
  88. *
  89. * @return array
  90. */
  91. public function getDisplayRegions($locale = null)
  92. {
  93. if (!$locale) {
  94. $locale = $this->findLocale();
  95. }
  96. $dataPath = PIMCORE_COMPOSER_PATH . '/umpirsky/country-list/data/';
  97. if (file_exists($dataPath . $locale . '/country.php')) {
  98. $regions = include($dataPath . $locale . '/country.php');
  99. } else {
  100. $regions = include($dataPath . 'en/country.php');
  101. }
  102. return $regions;
  103. }
  104. /**
  105. * @return string|null
  106. */
  107. public function getLocale()
  108. {
  109. if (null === $this->locale) {
  110. $this->locale = $this->getLocaleFromRequest();
  111. }
  112. return $this->locale;
  113. }
  114. /**
  115. * @param string|null $locale
  116. */
  117. public function setLocale($locale)
  118. {
  119. $this->locale = $locale;
  120. if ($locale && is_string($locale)) {
  121. if ($this->requestStack) {
  122. $masterRequest = $this->requestStack->getMasterRequest();
  123. if ($masterRequest) {
  124. $masterRequest->setLocale($locale);
  125. }
  126. $currentRequest = $this->requestStack->getCurrentRequest();
  127. if ($currentRequest) {
  128. $currentRequest->setLocale($locale);
  129. }
  130. }
  131. if ($this->translator) {
  132. $this->translator->setLocale($locale);
  133. }
  134. }
  135. }
  136. /**
  137. * @return bool
  138. */
  139. public function hasLocale()
  140. {
  141. return $this->getLocale() !== null;
  142. }
  143. }