Author Topic: Use a cookie for language selection - World Template  (Read 2156 times)

0 Members and 1 Guest are viewing this topic.

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Use a cookie for language selection - World Template
« on: May 29, 2021, 10:10:36 AM »
I am currently setting up a weather site in Mexico. I am using the World Template as my base. Nothing is live yet so I have lots of time to work on this. About 50% of my users/viewers are English and 50% are Spanish. I know this because I already have an "old" weather site up and running and I know my user base. I would like to be able to save and store the language setting for users so the next time they come back to the site, the language setting is restored for them. My default is English and I only allow two languages to be selected "English" and "Spanish". So I am thinking I would only need to create a cookie if someone selected Spanish and upon opening of the website I would have to see if a cookie exists for this user and restore the language.

Does this seem reasonable?
Has anyone attempted this yet?
I'm thinking to start somewhere in common.php
Ideas?

Thanks for any insight ...

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #1 on: May 29, 2021, 03:47:54 PM »
Currently, the common.php in the template uses only two cookie values: CSSstyle and CSSwidescreen used for the overall theme and wide/narrow aspect.  The language selected is stored in the PHPsession cookie value, so once selected, the lang= value persists for as long as the PHPsession exists.  I don't know the life expectancy of the PHPsession token, so it likely expires sometime.. just don't know when.

Yes, the common.php could be modified to also set a lang cookie.  I'm just not sure it's needed.  Since your site is in Mexico, I'd expect the default language to be Spanish, and if I needed it in English, it's an easy to change with the dropdown menu.  Just my thoughts...
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #2 on: May 29, 2021, 04:48:45 PM »
Thanks for the reply. I live in an Expat community so like I said about 50% of my viewers want English. But I do live in Mexico so I have many viewers that want Spanish. The site isn't live yet but I expect this "cookie" issue of holding the language across sessions will be number 1 issue. Second issue will be holding the "Imperial/Metric" setting across sessions as I have many Canadians that want to see the site in English yet want Metric settings.  Thanks for your input and any others that would like to input, please do.

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #3 on: May 30, 2021, 05:34:24 PM »
Ok, this was easier than I thought.  Try using this common.php V1.12 - 30-May-2021

It adds a lang= cookie that is stored when the language selection changes.  The cookie expires after 6 months.

Hope this helps...

Best regards,
Ken

ed. note: removed 30-May-2021 version attached as not quite working right
« Last Edit: May 31, 2021, 11:20:07 AM by saratogaWX »
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #4 on: May 31, 2021, 06:15:32 AM »
Hi Ken,

I tried the new common.php but are not seeing the results I expected. In fact the translation of the page is only occuring on the "Forecast/Storm Prediction" and WU Forecast areas on the home page. I see the cookie gets created but when I click to change the language to Spanish from English, English stays as the selected list item and the flag does not change.  If I change back to my old common.php translation works fine. I currently have your new common.php installed, you can take a look. This site isn't live so I can make changes as needed.

https://ajijicweather.com/wxindex.php

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #5 on: May 31, 2021, 11:19:12 AM »
I changed the logic a bit, so try this one.. it should be persistent for language selection.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #6 on: May 31, 2021, 01:04:48 PM »
I installed the latest common.php and still the same results. When you select Spanish the dropdown selection stays on English and not everything gets translated. Only the forecasts get translated. The header and current conditions do not get translated.

You can test it here: https://ajijicweather.com/wxindex

I tried some coding and got it to work with these additions in common.php. The coding might not be perfect, but it does work.

# New language cookie code
$cookie_name = 'AjijicLang';
if(isset($_COOKIE[$cookie_name])) {
     $SITE[lang] = $_COOKIE[$cookie_name];
}
# End New Cookie
--------------------------------------
# Set the language cookie to restore the language setting next session
  $cookie_name = 'AjijicLang';
  $cookie_value = $lang;
  setcookie($cookie_name, $cookie_value, time() + (86400 * 180), '/'); // 180 days expire
 # end set language cookie

I attached my common.php as a txt file. Just do a search for cookie in the code. The common.php that I modified is currently not installed on the website. Your latest one is.

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #7 on: May 31, 2021, 01:30:43 PM »
Your modified common is V1.11.. not the V1.12 I attached to the prior posting.. try that one please...
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #8 on: May 31, 2021, 04:16:55 PM »
Hi Ken,

Currently on my website https://ajijicweather.com/wxindex I have installed your latest (dated today) V1.12 modified common.php

$CMNVersion = 'common.php - Version 1.12 - 31-May-2021';

I tested it and it does not function correctly. Give it a try. Select Spanish and it doesn't change the flag nor does it translate much of the page. Only forecasts are translated.

The common.txt (php) I attached previously is a modification to the "distributed" common.php. I made the modification a couple of days ago before you came out with your 1.12 version. Thats why it shows 1.11.

I have another test site where I am running my own modified version of common.php (the one I attached above) and it seems to work:
http://darryl-gail.com/wxindex.php

So https://ajijicweather.com/wxindex.php  (This is running your latest modified 1.12 common.php)
and
http://darryl-gail.com/wxindex.php (This is running my modified common.php)


Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #9 on: June 01, 2021, 11:44:30 AM »
Oooh... this is a tricky one.   

I've tried the 31-May-2021 code for common.php on two different servers.. both worked fine.  Grrrr.

I've updated the code to add some diagnostics that will show when a view-source of wx...php page with ?debug=y is used.

Please install the attached 01-Jun-2021 version on ajijicweather.com so I can take a look at the diagnostics, and thanks for your patience while we work through this issue.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #10 on: June 01, 2021, 02:54:26 PM »
Hi Ken,

I just intalled your V1.12 common.php (dated June 1) on https://ajijicweather.com/wxindex.php

Thanks for looking at this ...

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #11 on: June 01, 2021, 03:36:19 PM »
Ok, thanks.

I just did first load - view-source:https://ajijicweather.com/wxindex.php?debug=y
Quote
<!-- common.php - Version 1.12 - 01-Jun-2021 -->
<!-- input load_langtrans SITE['lang'] = 'en; COOKIE['lang'] = 'en; REQUEST['lang'] = 'en;  -->
<!-- lang_input='en' used for $lang -->
<!-- set_lang_cookie setcookie('lang','en'); was successful -->
<!-- result load_langtrans SITE['lang'] = 'en; SESSION['lang'] = 'en; COOKIE['lang'] = 'en; REQUEST['lang'] = 'en;  -->
<!-- langfile 'language-en.txt' loading -->

then did view-source:https://ajijicweather.com/wxindex.php?debug=y&lang=es with the lang=es parm the same as when the language selector changes to Spanish.  It returns:
Quote
<!-- common.php - Version 1.12 - 01-Jun-2021 -->
<!-- input load_langtrans SITE['lang'] = 'en; SESSION['lang'] = 'en; COOKIE['lang'] = 'en; REQUEST['lang'] = 'en;  -->
<!-- lang_input='en' used for $lang -->
<!-- set_lang_cookie setcookie('lang','en'); was successful -->
<!-- result load_langtrans SITE['lang'] = 'en; SESSION['lang'] = 'en; COOKIE['lang'] = 'en; REQUEST['lang'] = 'en;  -->
<!-- langfile 'language-en.txt' loading -->

The problem seems to be with your webserver in populating $_GET items into $_REQUEST (which would contain $_GET, $_POST, and in some instances $_COOKIE too).

Using check-fetch-times.php?show=info shows you're running PHP Version: 7.2.34 on Linux.  According to the PHP docs for $_REQUEST it should contain all the $_GET arguments on the URL, but for some strange reason, your webserver doesn't do that, and that's the cause of the failure.

It looks like your webserver's PHP has a null entry for 'request_order' and that's likely why it's not populating.

Try adding
Code: [Select]
ini_set('variables_order','GPCS');
ini_set('request_order','GP');
to Settings.php right before the $SITE = array(); entry and let's see if that fixes the $_REQUEST not being set with the $_GET['lang'] value when it's on the URL.


« Last Edit: June 01, 2021, 03:38:30 PM by saratogaWX »
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #12 on: June 01, 2021, 04:21:50 PM »
Ok just added two lines of code in settings.php as instructed:

ini_set('variables_order','GPCS');
ini_set('request_order','GP');
$SITE          = array();

It's in https://ajijicweather.com/wxindex

Just FYI, for ajijicweather.com I use godaddy as my hosting service. 

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #13 on: June 01, 2021, 04:30:49 PM »
Grrr... no change in response.

Could you put a php.ini in your document root with
Code: [Select]
request_order = "GP";
variables_order = "GPCS";

You may have to add those PHP directives into your cPanel PHP settings area on GoDaddy instead.

You can take out the two ini_set() entries in Settings.php.. they didn't work.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #14 on: June 01, 2021, 04:47:23 PM »
Ok I added a file php.ini with the two lines to the ROOT of ajijicweather.com.  I just FTPed the file to the ROOT, I did not use CPANEL. I also removed the two lines from settings.php

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #15 on: June 01, 2021, 05:35:25 PM »
If you mean the document root at /home/mxo4kz4ohhnx/public_html then it's not working (the settings aren't being honored, so $_GET->$_REQUEST global is not happening).

You should contact GoDaddy tech support to have them fix the issue of $_GET variables not appearing in $_REQUEST variables for your PHP.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #16 on: June 01, 2021, 06:12:45 PM »
I put the php.ini into ROOT and not public_html.  I have now just put the php.ini file into public_html and I think we have success!  Give it a try!

https://ajijicweather.com/wxindex

I will do some more testing tomorrow as I have to leave for a while but it looks good.

Thanks so much for your detective work.

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #17 on: June 01, 2021, 06:23:02 PM »
One other question. When I logged onto godaddy.com to look at some PHP stuff there was a message that I can upgrade to PHP 8.0. I'm currently at 7.3.6. Is this something I should upgrade or stay lower level for now?

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #18 on: June 01, 2021, 06:41:33 PM »
WooHoo...  \:D/ it's now working as it should.  The $_GET args are also in $_REQUEST so the new common.php is working for you now.  The language selection cookie is being saved and when you visit the site again, that language preference will be used.

Now you can use view-source:https://ajijicweather.com/wxindex.php?show=missing while in Spanish mode to see what missing language-es.txt entries might be made to improve the Spanish language display.

Yes, the template (with current Saratoga scripts) works fine with PHP8, but...some older 3rd party scripts that you may have used may not be ready for PHP8.   I'd suggest trying the update for your site, looking at each page and noting any issues.  Capture the error_log (which will have details), then if you encounter errors, switch back to PHP7 and work through the script issues. 
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #19 on: June 02, 2021, 10:38:30 AM »
"Now you can use view-source:https://ajijicweather.com/wxindex.php?show=missing while in Spanish mode to see what missing language-es.txt entries might be made to improve the Spanish language display."

THIS IS SO COOL !  This is exactly what I need. I just ran it and saw what I need to fix in my language translation file.

Thanks so much ...

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #20 on: June 02, 2021, 10:51:53 AM »
One other note. My test site of http://darryl-gail.com/wxindex.php which is hosted by Network Solutions had the same problem with needing the php.ini file. It wasn't as easy as putting it in the public_html folder like it was on godaddy.com hosting.  I had to use tools on my Netsol account and had to place the php.ini file in the cgi_bin folder. Got it working though.

A thought about this. If you release this new common.php it might cause some issues to current websites that do not have a php.ini or the right settings in php.ini. Perhaps a new setting in Settings.php like "$SITE['uselangcookie'] = false;"  with a note that if you change to true you might need a php.ini file with "these" settings.

Just a thought...

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #21 on: June 02, 2021, 01:45:59 PM »
Very odd that two hosters (GoDaddy and Network Solutions) didn't enable the $_GET -> $_REQUEST passthrough.  It's the first I'd encountered that configuration issue.  Prior to adding the cookie for language selection, the template used PHP's $_SESSION to 'carry forward' the selected language for subsequent pages (along with jamming the selection into $_REQUEST).  As you've found out, the selection is only 'sticky' as long as the PHP session has not expired, then the site display reverts to the $SITE['lang'] default language.

Both V1.11 and V1.12 use
Code: [Select]
  # always store in a PHP Session
  $_SESSION['lang'] = $lang;
  $_REQUEST['lang'] = $lang;  // establish for other scripts too
  $SITE['lang'] = $lang;
to pass along to the next pages (mostly via $_SESSION) the selected language source.  With the new diagnostic code in V1.12, it's easy to spot a PHP that doesn't pass the $_GET->$_REQUEST globals, so we can spot it if need be.

p.s. Glad you like the ?show=missing hack -- that's been with the template set since the release in February, 2011
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #22 on: June 02, 2021, 03:10:42 PM »
Cool sounds good.

One last question while I have your attention.  As I said in an earlier post I have about a 50% English user base and 50% Spanish user base. When a Spanish user wants spanish they will also want "Metric" instead of "Imperial" uom 100% of the time. My site by default is English because that is what I use. I was wondering if there is a simple way where if the user chooses "Spanish" language then we can force an "onclick" event on the "Metric/Imperial" area.

I've been looking at ajaxWDwx3.js in this area of code:

function ajax_changeUnits () {
   useUOM  = (useUOM==="E") ? "M" : "E" ;
   uom     = (useUOM==="M") ? metricUOM : imperialUOM;
   dp      = (useUOM==="M") ? metricDP  : imperialDP ;
   colorM  = (useUOM==="E") ? "gray"    : "blue";
   colorE  = (useUOM==="M") ? "gray"    : "blue";

And also in ajax-dashboard6.php in this area of code:

<div onclick="javascript:ajax_changeUnits();" style="cursor: pointer;" title="Toggle units-of-measure &amp; restart"><b>
                        <span id="uomM" style="color: blue;"><?php langtrans('METRIC'); ?></span> |
                        <span id="uomE" style="color: gray;"><?php langtrans('IMPERIAL'); ?></span></b>
</div>

If when the page loads and language is "Spanish" then force the onclick="javascript:ajax_changeUnits();"  I don't even know if that is programmatically possible.

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Use a cookie for language selection - World Template
« Reply #23 on: June 02, 2021, 03:44:02 PM »
Ahh... an extra credit problem to solve :)

Yes, easy to do.  Change the wxindex/index.php from
Code: [Select]
    </div>
</div><!-- end center -->
to
Code: [Select]
    </div>
<?php 
  
if($_SESSION['lang'] !== 'en') {
    echo 
"<script type=\"text/javascript\">ajax_changeUnits();</script>\n";
}?>

</div><!-- end center -->
That will inject the script to run ajax_changeUnits() JavaScript function in the alternative dashboard when the page loads with a non-English language.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline mexbound

  • Senior Member
  • **
  • Posts: 66
Re: Use a cookie for language selection - World Template
« Reply #24 on: June 02, 2021, 05:20:59 PM »
Simple, yet beautiful. Works great!

Thanks Ken.