suntrop Posted October 25, 2017 Share Posted October 25, 2017 I have got a Textareas field (latlong) with two Text fields (lat and long). I query Google Maps for the coordinates and use a hook to save both fields. The problem is, after I installed the German language pack those fields are saved with a comma as decimal separator. But that doesn't work with Google Maps, because it needs 50.43578373748 and not 50,43578373748. As I remember Float can only take up to 7 points after the decimal point – so I can't just change it from Text to Float. Currently I just str_replace the decimal to a point in the template for my JSON fiel. But I was wondering if thee is a way I can tell PW to not convert the point to decimal – regardless of of the language. Link to comment Share on other sites More sharing options...
suntrop Posted November 1, 2017 Author Share Posted November 1, 2017 Does anybody know how to NOT convert these numbers ? Link to comment Share on other sites More sharing options...
wbmnfktr Posted November 1, 2017 Share Posted November 1, 2017 None of my text fields or textarea fields convert 50.43578373748 to 50,43578373748. Can you explain your field setup a bit more detailed as your description sounds kind of weird to me right now. On 25.10.2017 at 12:35 PM, suntrop said: I have got a Textareas field (latlong) with two Text fields (lat and long). Link to comment Share on other sites More sharing options...
suntrop Posted November 2, 2017 Author Share Posted November 2, 2017 Sure! I have something like a store locator and need to store the latitude and longitude for each place. Therefore I created a field named "latlong" This field is of type "Textareas" and has two "Text" sub-fields "lat" and "long". When I save a page the fields contents (the coordinates) are getting converted, the point turns into a comma This happens since I installed the German language pack. Prior saved locations don't have a comma saved. While the default German decimal separator is a comma, this conversion makes things very complicated. That is my hook in /site/ready.php $pages->addHookBefore('saveReady', function($event) { $page = $event->arguments[0]; if($page->template == 'location') { if ( empty($page->street) || empty($page->city) ) { return; } // Get address $address = $page->street . ', ' . $page->zip . ' ' . $page->city . ', ' . $page->country; // Get JSON results from this request $geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false'); $geo = json_decode($geo, true); if ($geo['status'] == 'OK') { $page->latlong->lat = $geo['results'][0]['geometry']['location']['lat']; $page->latlong->long = $geo['results'][0]['geometry']['location']['lng']; $this->message('Saved for: ' . $geo['results'][0]['formatted_address']); } else { $this->error('Error msg...'); } } }); Link to comment Share on other sites More sharing options...
AndZyk Posted November 2, 2017 Share Posted November 2, 2017 Hello @suntrop, have you considered using the fieldtype Map Marker for storing your coordinates? Maybe you already have your reasons why you choose the ProField Textareas. I am just curious. This fieldtype would be a better fit for your need. Also in case you have an active license, you could ask in the dedicated ProFields Textareas thread this question. As far as I know, language packs don't change any output on the front-end. Regards, Andreas Link to comment Share on other sites More sharing options...
wbmnfktr Posted November 2, 2017 Share Posted November 2, 2017 @AndZyk MapMarker is a nice module and does most of the things most people want. But there is still some overhead if you build and use your own Google Maps with custom scripts and markup. All @suntrop does and probably needs is grabbing lat/long from Google and saving it to text fields. A super lightweight solution btw. I played around with Profields: Textareas and the lat/long script. Still everything is fine. With and without language support and german language pack. Link to comment Share on other sites More sharing options...
suntrop Posted November 3, 2017 Author Share Posted November 3, 2017 17 hours ago, AndZyk said: have you considered using the fieldtype Map Marker for storing your coordinates? Maybe you already have your reasons Yes, I have. I would have to fork and modify Map Maker, because I need address in separate fields and with more data. And I need them to be manageable by PHP … I guess Map Maker counts on the JS API. And as wbmnfktr says, I really just need the lat/long … nothing more. I just figured out, where the conversion takes place. I put this into my wwwroot <?php include 'index.php'; // setlocale(LC_NUMERIC, 'C'); $json = json_decode( '{"test": 48.8555648}' , true); var_dump($json); die(); The code prints: array(1) { ["test"]=> float(48,8555648) } When I don't include the PW bootstrap file (index.php) it prints: array(1) { ["test"]=> float(48.8555648) } When I include the index.php and uncomment the setlocale() it prints: array(1) { ["test"]=> float(48.8555648) } I don't think it has to do with the Textareas module, but how the locale is set in PW. I couldn't find much about setlocale(LC_NUMERIC, 'C') but it seems the C sets numeric values to C++ default type (e.g. 3.14 not 3,14). https://processwire.com/talk/topic/4123-decimal-point-changed-to-in-base-of-setlocale/?do=findComment&comment=142861 It doesn't work if I put the setlocale() into my /site/config.php or /site/template/_init.php However, it looks more like a bug and not like "this is how it should work" 1 Link to comment Share on other sites More sharing options...
matjazp Posted November 7, 2017 Share Posted November 7, 2017 @suntrop put setlocale() in /site/init.php There is also $languages->setLocale(); https://processwire.com/api/ref/languages/set-locale/ 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