Sunday, March 23, 2014

#magento How to detect language from php code

Problem:
Yo need to change some content at low level in a phtml file on a multi language site using Magento, when you use multilingual you have to create content in a each language that you need, so, how you can call content choosing the correct language ?

Solution:
That's simple process using a sentence to read the loaded language on the site using the next sentence:

Mage::app()->getLocale()->getLocaleCode();

That sentence return that string for Spanish one:   en_EN

To load a Static Block you only need to use something like that:
$locale = Mage::app()->getLocale()->getLocaleCode();
$locale = substr($locale, 0, 2);
switch ($locale) {
case "es":
  echo $this->getLayout()->createBlock('cms/block')->setBlockId(CopyRight)->toHtml();
  break;
case "en":
  echo $this->getLayout()->createBlock('cms/block')->setBlockId(CopyRight_en)->toHtml();

  break;
...

2 comments: