Jump to content

Strange behavior with uppercase and lowercase on home template


KarlvonKarton
 Share

Recommended Posts

Is this a bug in Processwire or some PHP behavior?

In _init.php there are two variables ($LANG and $sortNames):

$LANG = $user->language->title; // in uppercase NL or FR

$sortNames = array(
	'NL' => array(
		'apartment'=>'Appartement/Studio',
		'house'=>'Huis/Villa',
		'terrain'=>'Grond',
		'parking'=>'Garage/Parkeerplaats',
		'other'=>'Andere',
		'commerce'=>'Commercieel'
	),
	'FR' => array(
		'apartment'=>'Appartement/Studio',
		'house'=>'Maison/Villa',
		'terrain'=>'Terrain',
		'parking'=>'Garage/Place',
		'other'=>'Autre',
		'commerce'=>'Commerce'
	)
);

If I use the variables in several templates, the the $LANG variable stays uppercase:

echo $LANG;  // gives the desired NL or FR

foreach($sortNames[$LANG] as $k => $v){
   echo $k.' '.$v.'<br>'; // gives the desired output from $sortNames
}

But if I do the same on the home template, then the $sortNames gives no output:

echo $LANG;  // gives the desired NL or FR

foreach($sortNames[$LANG] as $k => $v){
   echo $k.' '.$v.'<br>'; // stays empty !!!
}

The solution is simple but highly unusual, but in _init.php I done the following to correct the strange behavior:

$LANG = strtoupper($user->language->title);

Anyone has a clue why this is happening on home?

Link to comment
Share on other sites

I have done a var_dump on the variable $LANG (user logged in or not logged in).

When the user is NOT logged in, then the variable is a string:
string(2) "FR"

When the user is logged in, then the variable is an object:
object(ProcessWire\LanguagesPageFieldValue)#331 (2) { ["default"]=> string(2) "FR" ["fr"]=> string(2) "FR" }

Is this a bug or not?

EDIT: $LANG is only an object on the home page and when logged in...

Link to comment
Share on other sites

4 minutes ago, dragan said:

@KarlvonKarton Not an answer to your OP, but why don't you use PW's built-in multilang feature with single or double underscores anyway? ($this->_() or __())

https://processwire.com/docs/multi-language-support/code-i18n/#translatable-strings

I guess you are commenting on sortNames? In the development it is easier for me to use a multidimensional array.  It will eventually change.

PS: But if it wasn't for the array, then I would never have noticed the behavior of _init.php and $LANG = ...

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

  • Recently Browsing   0 members

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