Manager.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Locale;
  7. /**
  8. * Locale manager model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Manager
  15. {
  16. /**
  17. * @var \Magento\Backend\Model\Session
  18. */
  19. protected $_session;
  20. /**
  21. * @var \Magento\Backend\Model\Auth\Session
  22. */
  23. protected $_authSession;
  24. /**
  25. * @var \Magento\Framework\TranslateInterface
  26. */
  27. protected $_translator;
  28. /**
  29. * @var \Magento\Backend\App\ConfigInterface
  30. * @since 100.1.0
  31. */
  32. protected $_backendConfig;
  33. /**
  34. * Constructor
  35. *
  36. * @param \Magento\Backend\Model\Session $session
  37. * @param \Magento\Backend\Model\Auth\Session $authSession
  38. * @param \Magento\Framework\TranslateInterface $translator
  39. * @param \Magento\Backend\App\ConfigInterface $backendConfig
  40. */
  41. public function __construct(
  42. \Magento\Backend\Model\Session $session,
  43. \Magento\Backend\Model\Auth\Session $authSession,
  44. \Magento\Framework\TranslateInterface $translator,
  45. \Magento\Backend\App\ConfigInterface $backendConfig
  46. ) {
  47. $this->_session = $session;
  48. $this->_authSession = $authSession;
  49. $this->_translator = $translator;
  50. $this->_backendConfig = $backendConfig;
  51. }
  52. /**
  53. * Switch backend locale according to locale code
  54. *
  55. * @param string $localeCode
  56. * @return $this
  57. */
  58. public function switchBackendInterfaceLocale($localeCode)
  59. {
  60. $this->_session->setSessionLocale(null);
  61. $this->_authSession->getUser()->setInterfaceLocale($localeCode);
  62. $this->_translator->setLocale($localeCode)->loadData(null, true);
  63. return $this;
  64. }
  65. /**
  66. * Get general interface locale
  67. *
  68. * @return string
  69. * @since 100.1.0
  70. */
  71. public function getGeneralLocale()
  72. {
  73. return $this->_backendConfig->getValue('general/locale/code');
  74. }
  75. /**
  76. * Get user interface locale stored in session data
  77. *
  78. * @return string
  79. */
  80. public function getUserInterfaceLocale()
  81. {
  82. $userData = $this->_authSession->getUser();
  83. $interfaceLocale = \Magento\Framework\Locale\Resolver::DEFAULT_LOCALE;
  84. if ($userData && $userData->getInterfaceLocale()) {
  85. $interfaceLocale = $userData->getInterfaceLocale();
  86. } elseif ($this->getGeneralLocale()) {
  87. $interfaceLocale = $this->getGeneralLocale();
  88. }
  89. return $interfaceLocale;
  90. }
  91. }