Jump to content

Template caching aware of the current language


Alessio Dal Bianco
 Share

Recommended Posts

Template caching is language aware (and it includes the language as part of the cache file's id), but you'd have to have some aspect of the request determine the language before rendering the page. Otherwise there's no way to connect that cache file with the request. The simplest way to do that is to use the LanguageSupportPageNames module, to ensure that your homepage (for example) has a different URL for each language. If you are determining language from the hostname (i.e. en.domain.com, it.domain.com, etc.), then you'd have to have a before(Page::render) hook that sets the $user->language based on the hostname. I'm not sure what method you are using to determine language, so let me know and I can give you a better example. 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

If you are determining language from the hostname (i.e. en.domain.com, it.domain.com, etc.), then you'd have to have a before(Page::render) hook that sets the $user->language based on the hostname.

Hello.

I'm trying to implement language switching by using GET-parameter. Can you help me to understand how hook works?

if ( $_GET['lang'] == 'en'){
    wire()->addHookBefore('Page::render', function($event){
        $user->language = $languages->get("en");
        $user->save();
    });
}

What am I doing wrong?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...