您可以使用\Magento\Store\Api\Data\StoreInterface
或Magento\Framework\Locale\Resolver
类来获取商店语言。
1)通过使用\Magento\Store\Api\Data\StoreInterface
类
使用objectManager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$store = $objectManager->get('Magento\Store\Api\Data\StoreInterface');
echo $store->getLocaleCode();
有依赖注入
protected $_store;
public function __construct(
...
\Magento\Store\Api\Data\StoreInterface $store,
...
) {
...
$this->_store = $store;
...
}
现在用于getLocaleCode()
获取Laguage:
$currentStore = $this->_store->getLocaleCode();
if($currentStore == 'en_US'){
}
2)通过使用Magento\Framework\Locale\Resolver
类
使用objectManager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$store = $objectManager->get('Magento\Framework\Locale\Resolver');
echo $store->getLocale();
使用工厂方法
protected $_store;
public function __construct(
...
Magento\Framework\Locale\Resolver $store,
...
) {
...
$this->_store = $store;
...
}
现在用于getLocale()
获取Laguage:
$currentStore = $this->_store->getLocale();
if($currentStore == 'en_US'){
}