cornernote / yii-embed-opencart

A lightweight Yii application embedded into OpenCart
http://cornernote.github.io/yii-embed-opencart/
Other
18 stars 7 forks source link

Yii Embed vs OpenCart language selection #10

Closed dfumagalli closed 10 years ago

dfumagalli commented 10 years ago

Hello again,

I don't know if there's a better way to ask you questions than opening GitHub issues - if there is please tell me, but here we go.

I need to deliver the Yii Embed app in 7 languages. At the top of our e-commerce website pages there's the default OpenCart language selector and it works within OpenCart itself.

What happens if I switch language in a page populated by Yii Embed? Did you create some glue code on the matter?

I mean, I have done a quick look around and have seen that Yii needs to persist such kind of user choices by using cookies:

<?php
/**
 * ApplicationConfigBehavior is a behavior for the application.
 * It loads additional config paramenters that cannot be statically 
 * written in config/main
 */
class ApplicationConfigBehavior extends CBehavior
{
    /**
     * Declares events and the event handler methods
     * See yii documentation on behaviour
     */
    public function events()
    {
        return array_merge(parent::events(), array(
            'onBeginRequest'=>'beginRequest',
        ));
    }

    /**
     * Load configuration that cannot be put in config/main
     */
    public function beginRequest()
    {
        if (isset(Yii::app()->request->cookies['pref_lang']))
          $this->owner->language= Yii::app()->request->cookies['pref_lang']->value; //set a language for each request
        else 
            $this->owner->language='en';
    }
}

Now, I suppose the code above could be tweaked to just use the OpenCart language cookie... unless you have already implemented the mechanism in your code.

Did you?

cornernote commented 10 years ago

Hey @dfumagalli,

Asking by opening issues is fine and I will answer quickly if I have time. But sometimes if I am flat out it can take a little while. If you want commercial level support you can contact me at brett@mrphp.com.au.

I haven't done anything with multi-language. Yii uses something like this to output a translated string:

Yii::t('app', 'Hello World');

The translated language is defined in your config/main.php as a setting like this:

return array(
    'language' => 'en',
    ... other config things ....
);

You can change this setting on the fly using:

Yii::app()->language = 'foo';

More info can be found here: http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n

Let me know if you need more info.

dfumagalli commented 10 years ago

Does Yii support something like OpenCart for translations when using its file based method?

That is, instead of using:

Yii::t('app', 'Hello World');

Use an "handle" to the translation. That is:

Yii::t('app', 'hello_world');

and every translation file associates hello_world (and not the literal "Hello World" string) to its own translation.

Doing so, I could do something more "abstract" and consistent. In OpenCart it'd be something like:

$_['hello_world']           = 'Hello World';

Best regards, dfumagalli

cornernote commented 10 years ago

Yes, it's very flexible. The following code will allow you to hook into the existing OpenCart language files.

config/main.php

return array(
    'sourceLanguage' => 'xx',
    'language' => 'en',
    'components' => array(
    'messages' => array(
        'class' => 'MessageSource',
    ),
);

yiiembed/components/MessageSource.php

class MessageSource extends CMessageSource 
{
    private $_messages=array();

    protected function loadMessages($category,$language)
    {
        // load the OC language file (eg: $category='module/bestseller')
        Yii::app()->registry->load->language($category);
        $this->_messages[$category.'/'.$language] = true;
    }

    protected function translateMessage($category,$message,$language)
    {
        if (!isset($this->_messages[$category.'/'.$language])) {
            $this->loadMessages($category,$language);
        }
        $message = Yii::app()->registry->language->get($message)
        if (!$message) {
            $message = parent::translateMessage($category,$message,$language);
        }
        return $message;
    }
}

Then translate like this:

echo Yii::t('module/bestseller', 'heading_title');

I haven't tested this code, but the concept should work.

more info: http://www.yiiframework.com/wiki/243/how-to-translate-and-do-the-translations-the-easy-way/

cornernote commented 10 years ago

I updated the above comment a few times, please view it on github for the latest.

cornernote commented 10 years ago

I thought this was a good idea, so I added the code into the yii-embed-opencart extension.

Now you can simply use it like this:

echo Yii::t('module/bestseller', 'heading_title');

No need to do the config.

Please let me know how it goes.

cornernote commented 10 years ago

A small note, you have to update your config/main.php as per the included code...

catalog/yiiembed/config/main.php

return array(
    'language' => $config['registry']->get('config')->get('config_language'),
);

admin/yiiembed/config/main.php

return array(
    'language' => $config['registry']->get('config')->get('config_admin_language'),
);
dfumagalli commented 10 years ago

Hello,

Having noticed you made a lot of changes, I removed and reuploaded the whole Yii Embed. Everything works, even the issues I mentioned in the other "ticket".

I have put in a little translation (I'd have proposed for addition to your source if I knew how to do it) for the demo files, the attached screenshot shows the translation in action. It's more than I even hoped to see, and I'll certainly suggest your name to my boss in case we'll need third party PHP consulting.

Translation

One final suggestion: I followed all the "walkthrough" to the solution so I was aware of what you have done. For others, I suggest you update the README to mention they have to copy / rename the admin and catalog main-dist.php files to main.php.

Best regards and a big thank you! dfumagalli

cornernote commented 10 years ago

There was already a note in the configuration section in the documentation. I moved it to the installation section so that it's more noticeable.

Glad you're all up and running.

Thanks for the feedback, it helps to make this a better extension.