php Magento 获取模板文件中的语言代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6579287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:46:59  来源:igfitidea点击:

Magento get language code in template file

phpmagento

提问by chmoelders

I need a helper function to get the current language code. I want to use it in a templete file, like /products/view.phtml, only for testing purposes.

我需要一个辅助函数来获取当前的语言代码。我想在模板文件中使用它,例如 /products/view.phtml,仅用于测试目的。

Does it already exist?

它已经存在了吗?

I have something in mind like the URL-helper

我有一些像 URL-helper 一样的想法

$url = $this->helper('core/url')->getCurrentUrl();

回答by FbnFgc

You can get the current locale code this way :

您可以通过以下方式获取当前区域设置代码:

$locale = Mage::app()->getLocale()->getLocaleCode();

回答by Jordan VALNET

Result for answers given in this topic for "Belgium:French" (Be_Fr):

本主题中针对“比利时:法语”(Be_Fr) 给出的答案的结果:

  • strtolower(Mage::getStoreConfig('general/country/default'));= be
  • substr(Mage::getStoreConfig('general/locale/code'),0,2);= fr
  • Mage::app()->getLocale()->getLocaleCode();= fr_BE
  • strtolower(Mage::getStoreConfig('general/country/default'));= be
  • substr(Mage::getStoreConfig('general/locale/code'),0,2);= fr
  • Mage::app()->getLocale()->getLocaleCode();= fr_BE

Note that

注意

Mage::app()->getLocale()->getLocaleCode()== Mage::getStoreConfig('general/locale/code')

Mage::app()->getLocale()->getLocaleCode()== Mage::getStoreConfig('general/locale/code')

but with the second one, you can specify an other store than default one (Mage::getStoreConfig('general/locale/code', $storeId)), so I would recommand it.

但是对于第二个,您可以指定默认存储以外的其他存储 ( Mage::getStoreConfig('general/locale/code', $storeId)),因此我会推荐它。

回答by Jürgen Thelen

Afaik there is no such helper function, but you could of course build your own using:

Afaik 没有这样的辅助函数,但您当然可以使用以下方法构建自己的函数:

Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId());

回答by snh_nl

Try

尝试

$_language_code = substr(Mage::getStoreConfig('general/locale/code', $_store->getId()),0,2);

where $_store is current store object

$_store 是当前存储对象

回答by eInyzant

You can also use :

您还可以使用:

$languageCode = Mage::app()->getStore()->getLanguageCode();

$languageCode = Mage::app()->getStore()->getLanguageCode();

Don't forget to configure your store locales in your admin. Go to menu :

不要忘记在您的 admin 中配置您的商店区域设置。进入菜单:

System -> Configuration -> General -> Locale Options

系统 -> 配置 -> 常规 -> 区域设置选项

And set the correct Locale for each Websites or stores

并为每个网站或商店设置正确的区域设置

回答by ptz0n

For use in the html elements langattribute etc.

用于 html 元素lang属性等。

echo strtolower(Mage::getStoreConfig('general/country/default')); // "en"