Jump to content

Caching Pages in each language


adamspruijt
 Share

Recommended Posts

Hi all, hopefully this is an easy fix. It seems the default behaviour for PW is to cache only the first language of a page as it is accessed. So you get a file like 1234.cache. I tried search around the forums here and found some mention of "secondaryID" and that it could be used to allow alternates of cached pages? (correct me if that wrong).

Either way I was hoping to alter the cache file naming so that I could end up with something like this (or similar)

  • 1234.default.cache
  • 1234.fr.cache
  • 1234.cn.cache

Thanks!

  • Like 1
Link to comment
Share on other sites

This is actually fixed in the dev branch. You could switch to the dev branch, or you could copy the relevant file out of it (/wire/modules/PageRender.module) and replace the one you have. Here is the commit highlighting the changes:

https://github.com/ryancramerdesign/ProcessWire/commit/6c6f27ea0f0df4653ac241f5c8b54d44e1c36494

  • Like 1
Link to comment
Share on other sites

I'm actually using the dev branch as I notices some language related changes... still not working. So I think it has to do with my poorly written language selector, I have a simple select field in the top right of the site, I need this to be able to switch me to different subdomains based on the found langues... if that makes sense? I'll attach the code.

<select name="lang" id="lang_select">
<?php foreach ($languages as $lang): ?>
 <?php
 if($config->httpHost == "$lang->name.$domain") {
  $user->language = $languages->get($lang->name);
 }else{
  $user->language = $languages->get('default');
 }
 ?>

 <?php $selected = ($lang->name == $user->language->name ? 'selected="selected"' : '') ?>

 <?php if ($lang->name == 'default'): ?>
 <option <?php echo $selected ?> value="http://<?php echo $domain.$page->url ?>">English</option>
 <?php else: ?>
 <option <?php echo $selected ?> value="http://<?php echo $lang->name.'.'.$domain.$page->url ?>"><?php echo $lang->title ?></option>
 <?php endif ?>
<?php endforeach ?>
</select>

note** $domain is a predefined static variable... because I was being lazy and didn't want to grab the base domain with php.

Link to comment
Share on other sites

Ok... I think I understand WHY this doesn't work. In order for the language to be used by the PageRender module, the language need to be set before that runs, my selector is doing it after... correct? So is there a way to make a language selector that "hooks in" before this?

Link to comment
Share on other sites

That's correct. That language is going to need to be set before the [cached] page renders, since you are selecting a language based on subdomain. Best way to accomplish this might be with a module. I think this below would do it. You'd have to update the last function there to reflect your actual domains an language names.

/site/modules/LanguageSubdomain.module

class LanguageSubdomain extends WireData {
 public static function getModuleInfo() {
   return array('autoload' => true); 
 }
 public function init() {
   $this->addHookBefore('Page::render', $this, 'hookRender');
 }
 public function hookRender(HookEvent $event) {
   $host = $this->config->httpHost; 
   if($this->config->httpHost == 'es.domain.com') $lang = 'es';
       else if($this->config->httpHost == 'fr.domain.com') $lang = 'fr';
       else $lang = 'en';
   $this->user->language = $this->languages->get($lang); 
 }
}
Link to comment
Share on other sites

  • 5 years later...

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...