19

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.

Does it already exist?

I have something in mind like the URL-helper

$url = $this->helper('core/url')->getCurrentUrl();
chmoelders
  • 18,584
  • 6
  • 22
  • 24

6 Answers6

77

You can get the current locale code this way :

$locale = Mage::app()->getLocale()->getLocaleCode();
FbnFgc
  • 1,666
  • 16
  • 19
  • With that one you get the locale currently used in translate model, but both are technically correct. – FbnFgc Jul 05 '11 at 09:16
  • How would we get the LocaleName? (like we see in backend in the dropdown when we set the locale) – snh_nl Nov 07 '14 at 13:24
14

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

  • 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')

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.

gioele
  • 9,748
  • 5
  • 55
  • 80
Jordan VALNET
  • 156
  • 1
  • 2
9

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

Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId());
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
2

Try

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

where $_store is current store object

snh_nl
  • 2,877
  • 6
  • 32
  • 62
0

You can also use :

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

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

System -> Configuration -> General -> Locale Options

And set the correct Locale for each Websites or stores

eInyzant
  • 269
  • 2
  • 5
-1

For use in the html elements lang attribute etc.

echo strtolower(Mage::getStoreConfig('general/country/default')); // "en"
ptz0n
  • 461
  • 3
  • 14