KarlvonKarton Posted December 9, 2019 Share Posted December 9, 2019 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 More sharing options...
KarlvonKarton Posted December 9, 2019 Author Share Posted December 9, 2019 Some more info: Apparently the strange behavior only happens when a user is logged in... Link to comment Share on other sites More sharing options...
KarlvonKarton Posted December 9, 2019 Author Share Posted December 9, 2019 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 More sharing options...
matjazp Posted December 9, 2019 Share Posted December 9, 2019 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. 1 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted December 9, 2019 Author Share Posted December 9, 2019 16 minutes ago, matjazp said: And I get the same result, title is string. Correct. I noticed it is only an issue if you use _init.php Link to comment Share on other sites More sharing options...
dragan Posted December 9, 2019 Share Posted December 9, 2019 @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 Link to comment Share on other sites More sharing options...
KarlvonKarton Posted December 9, 2019 Author Share Posted December 9, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now