Magento – Load language based on a custom user option

language

Apologies is this is one of those elementary sort of questions. Well I've spent a couple of hours searching for the last piece of what I need for my project to work, but I just like every other topic I search for about Magento, can't find that last bit of magic information that makes everything work.

In short, I inherited a project for which I wouldn't pick Magento to be the framework, and I'm basically stuck with Magento as too much work has already been done. There are pieces of native Magento that are used, but there is a great deal of coding completed in custom classes and custom tables. The app as a whole is behind a login screen and has no public areas.

The client is fairly happy with the way things are working so we just can't up and redevelop the thing.

My challenge at this point is to get the translation parts working. When a user logs in, the app needs to translate the site into the language according to custom options set in the user's profile. I need to do this with code. There is no dropdown or link to click on to switch between languages.

This is not a typical ecommerce website, but a custom B2B where the clientele is in Europe. The site needs to translate, at this moment, into four other languages other than English.

Poking around the interwebs I gathered this much:

i. I need a csv for my stuff at app\locale\en_US\xxxx_ssss.csv

— added the file, entered a test case: "New Order","Neue Ordnung"

ii. Updated the config.xml with this:

<frontend>
    <translate>
        <modules>
            <xxxx_ssss>
                <files>
                    <default>xxxx_ssss.csv</default>
                </files>
            </xxxx_ssss>
        </modules>
    </translate>
</frontend>

iii. Enclosed the text to be test translated with the $this->__() function.

— this is works. I see "Neue Ordung" where I expect to see it, instead of the "New Order"

This is all good and well, however, taking it one step further, things don't work. I need to set the language, via code, according to the user's profile.

I found a bit of code I've only seen once:

Mage::app()>getLocale()>setLocale('nl_NL');

This seems to be able to "force" Magento to use that locale.

I added a test file at app\locale\nl_NL\xxxx_ssss.csv with a different translation to see if Magento picks it up.

It doesn't. I still see my test translation from above.

If I print out Mage::app()->getLocale() we can see (below) that the localCode has been set to nl_NL

(
    [_defaultLocale:protected] => en_US
    [_locale:protected] =>
    [_localeCode:protected] => nl_NL
    [_emulatedLocales:protected] => Array
    (
    )
)

I also added another store view with the locale default language, for this test, to Dutch.

This still doesn't seem to work. I cleared out the cache (even though it's turned off, it's in development).

I just don't know what else I need to do to get this to work. (I'm basically "desperate" as I've clicked to the second and third pages of a Google search 😉 )

Best Answer

Tested, this and I can swap locales at will, and get the translations.

See Mage_Core_Model_Translate::getLocale()

which I changed to :

        /**
         * Retrieve locale
         *
         * @return string
         */
        public function getLocale()
        {
// custom code start to get current logged in customer            

if(Mage::getSingleton('customer/session')->isLoggedIn()) {
                $customerData = Mage::getSingleton('customer/session')->getCustomer();
                $this->_locale = 'en_US'; // here you would pull the locale from a customer attribute
            }

// custom code ends

            if (is_null($this->_locale)) {
                $this->_locale = Mage::app()->getLocale()->getLocaleCode();
            }
            return $this->_locale;
        }

Now when I view the frontend, the locale is loaded from the files in app/locale/en_US (as compared to my default locale of en_AU)

Naturally the above code shoudl be done via a rewrite. Don't go edit core files ;)

Hope this helps, or at least puts you on the right track.

Related Topic