Jump to content
KarlvonKarton

Strange behavior with uppercase and lowercase on home template

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?

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

I can't duplicate. I don't use _init.php, but I put this into home.php and basic-page.php:

bd($user);
bd($user->language);
bd($user->language->title);

And I get the same result, title is string.

  • Like 1

Share this post


Link to post
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 = ...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...