Jump to content

Don't convert thousand separator (point => comma)


suntrop
 Share

Recommended Posts

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

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

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"

field.png.5a9ecea27cbc06585df8b580bdd7f122.png

 

This field is of type "Textareas" and has two "Text" sub-fields "lat" and "long".

fields.png.f6d8423b354961e1e43c960f03517bd9.png

 

When I save a page the fields contents (the coordinates) are getting converted, the point turns into a comma

comma.png.4cae7b74d1a06e259b2a87de43d96a89.png

DB.png.ed0a471f6392537858fc02f4e15195d2.png

 

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

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

@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

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" :-)

  • Like 1
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...