Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internationalization docs : setting the application language at runtime - is not clear #6568

Closed
offline-online opened this issue Dec 18, 2014 · 16 comments

Comments

@offline-online
Copy link

I am new to Yii and new to Internationalization. I would like make my site to support many languages, and I need to allow users to chose their language. So I need to set the application language at runtime. This is all that we get in guide:

You may set the application language at runtime to set it to a language the user has chosen. This has to be done at a point before any output is generated so that it affects all the output correctly. Therefor just change the application property to the desired value:

\Yii::$app->language = 'zh-CN';

  1. Where should I put links that users can click to chose language?
  2. Is it some controller responsibility to take action and set the language ? Which controller and how ?
  3. If it is not controller responsibility to take action, where should we use \Yii::$app->language = 'zh-CN'; ? And how we are supposed to take user requested language and act based on that ?

Can you please provide real world example of this process ?

@samdark
Copy link
Member

samdark commented Dec 18, 2014

  1. Anywhere you want. That's application decision, not framework one.
  2. Yes. Any controller you chose. How? Via \Yii::$app->language = 'zh-CN'; as said in the guide.

@samdark
Copy link
Member

samdark commented Dec 18, 2014

I don't think there's anything missing in the guide in this regard. It is as simple as it's described. Just one line of the code.

@samdark samdark closed this as completed Dec 18, 2014
@offline-online
Copy link
Author

So I have links in my main.php layout file that point to SiteController/language action like this:

<?= Html::a('DE', ['site/language', 'lng' => 'de']); ?>

This is my language action:

    public function actionLanguage($lng)
    {
        if ($lng === 'de') {
            \Yii::$app->language = 'de';
        } else {
            \Yii::$app->language = 'gb';
        }
    }

When user press language link, all I get is white screen. Docs says that I should set language before any output, but how I am supposed to do that ? I dont get this...

@samdark
Copy link
Member

samdark commented Dec 18, 2014

You're doing it almost right except:

  1. In PHP everything is destroyed on each request. You have to store language somewhere.
  2. Action should save language to this "somewhere". btw., you're missing redirect there.
  3. Before each request processing you should get your language from where you've saved it and apply it via \Yii::$app->language = $language.

@samdark
Copy link
Member

samdark commented Dec 18, 2014

I'll cover it in cookbook but a bit later.

@offline-online
Copy link
Author

Thanks a lot for your help, I made it work, but I am not sure if this is the best way to do it. Here is what I did:

  1. Language action is storing chosen language in session.
    public function actionLanguage($lng)
    {
        if ($lng === 'de') {
            Yii::$app->session->set('language', 'de');
            return $this->goBack();
        } else {
           Yii::$app->session->set('language', 'en');
            return $this->goBack();
        }
    }

Inside my config file I am using on beforeRequest event to set language like this:

    'on beforeRequest' => function ($event) {
         \Yii::$app->language = Yii::$app->session->get('language');
    },

Is this good way to do it ?

@samdark
Copy link
Member

samdark commented Dec 18, 2014

I'd use cookies instead of session not to loose setting when coming back after period of inactivity. The rest seems good.

One thing is that if you're providing content using different language it worth having language in URL instead of just storing it in a cookie.

@offline-online
Copy link
Author

How is that supposed to work ? I mean having language in URL o.O. I store it there somehow ?

@samdark
Copy link
Member

samdark commented Dec 18, 2014

I mean having separate URLs for separate languages:

http://en.example.com/something/
http://ru.example.com/something/

or

http://example.com/en/something/
http://example.com/ru/something/

In this case you don't have to store. Instead you're just redirecting to another language URL.

There are many ways of implementing it. That's why there's no concrete example in the guide.

@offline-online
Copy link
Author

Thanks, but I have not idea how to implement that.

@cebe
Copy link
Member

cebe commented Dec 18, 2014

there is an extension for this: https://github.com/codemix/yii2-localeurls

@offline-online
Copy link
Author

I did exactly what installation says for that extension, and when I run my application I just get white screen...

@cebe
Copy link
Member

cebe commented Dec 18, 2014

@offline-online check your error logs

@offline-online
Copy link
Author

runtime debug and logs are empty

@offline-online
Copy link
Author

Ok, it was comma issue, I can run application now. BUT, it will always run with default en_US locale. How do I have to modify my code to allow users to chose language ?

code in main.php layout:

    <?= Html::a('DE', ['site/language', 'lng' => 'de']); ?>
    <?= Html::a('FR', ['site/language', 'lng' => 'fr']); ?>

Language action in SiteController :

public function actionLanguage($lng)
    {
        if ($lng === 'de') {
            Yii::$app->session->set('language', 'de');
            return $this->goBack();
        } else {
           Yii::$app->session->set('language', 'fr');
            return $this->goBack();
        }
    }

Setting language in cookies also do not help.

@offline-online
Copy link
Author

I got it. Thanks all for helping. Thanks cebe A LOT, it is good extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants