Jump to content

Recommended Posts

Posted

Hi!

Just curious is it possible to get current language in init.php or init method of modules? 

 

Posted

It is. 

// _init.php

$lang = $user->language; 

//_main.php

<?= $lang->title ?> //English or German etc.

 

Posted
1 hour ago, Zeka said:

@Sergio Thanks, but not in templates/_init.php, but site/init.php

Ops! Sorry! Soma showed a way to get in on a module init method:

 

Posted

@Sergio  Thanks, I have tried all from that thread, but it also doesn't work. $this->wire("user")->language->name always returns 'default', but in ready.php or ready modules method it works as expected, so it seems that user language is determined and set on ready stage here

The issue that I'm trying to solve: I'm using Padloper and by accessing 'padloper/ajaxViewCart/' I can get the view of the cart for ajax requests. It works as expected for default language, but not for non-default languages. So, I was trying to find a way how to get current language, but now I assume that it's not going to help as on init stage there is no page object and I actually can't get non-default lang values of fields. Here the code which responsible for that.

public function init() {
	.....
	$this->addHookBefore('ProcessPageView::execute', $this, 'runPadloper');
	....
}

 public function runPadloper($event) {

    $it = isset($_GET['it']) ? $_GET['it'] : "";
    if (substr($it, 0, 1) === '/') $it = ltrim($it, "/");

    // We only allow urls like /padloper/ and /d/ here
    if(strpos($it, "padloper/") !== false || strpos($it, "d/") !== false) {
      // Do nothing
    } else {
      return;
    }

    switch ($it) {

	.....

      case 'padloper/ajaxViewCart/':
        echo $this->modules->get("PadRender")->viewCart();
        exit();
        break;

 

Posted

whats about detecting it and set it to a session var in site/finished.php and afterwards looking for it in site/init.php?

 

PS: it would need extra attention if a language change url was currently clicked.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...