Django - Internationalisation and Google Maps api v3
I have a Django app and have integrated some Google maps via the v3 api. After a week of discovery and playing around everything was working fine, until...
I changed the language on my app by clicking on a flag on a form that POSTS the action to /i18n/setlang/, which is what Django uses to change the language. Now the new language is showing up, but the maps aren't. In the Chrome debugger it's giving the following error:
Failed to load resource: the server responded with a status of 400 (Bad Request)
StaticMapService.GetMapImage
The following is the Chrome debugger header content for the error:
1.
Request URL:
http://maps.googleapis.com/maps/api/js/StaticMapService.GetMapImage?1m2&1iNaN&2iNaN&2e2&3u8&4m2&1uNaN&2uNaN&5m3&1e3&2b1&5sen-US&token=128748
2.
Request Method:
GET
3.
Status Code:
[400 Bad Request]
400 Bad Request
4. Request Headers
1.
Referer:
http://127.0.0.1:8000/uns/uns_cities_form/Mu%C4%9Fla/
2.
User-Agent:
Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
5. Query String Parameters
1.
1m2:
2.
1iNaN:
3.
2iNaN:
4.
2e2:
5.
3u8:
6.
4m2:
7.
1uNaN:
8.
2uNaN:
9.
5m3:
10.
1e3:
11.
开发者_开发技巧 2b1:
12.
5sen-US:
13.
token:
128748
6. Response Headers
1.
Content-Length:
1350
2.
Content-Type:
text/html; charset=UTF-8
3.
Date:
Sun, 06 Feb 2011 20:29:59 GMT
4.
Server:
staticmap
5.
X-XSS-Protection:
1; mode=block
If I set the language back to English all works fine again...
Ok, so there is nothing to do with any translation whilst loading the map, but I'm figuring that Django has changed something or other which is disrupting the Http request, although to be honest I have no idea what is going on. The following is the options and the call to the map
//Map Options
myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID,
streetViewControl: false,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
}
//Create the map
map = new google.maps.Map(elem,myOptions);
Has anyone come across this, or can anyone throw some light on what might be happening?
I suspect Django's Format Localization could cause that.
When using Django's formatting system, dates and numbers on templates will be displayed using the format specified for the current locale. Two users accessing the same content, but in different language, will see date and number fields formatted in different ways, depending on the format for their current locale.
So depending on the language, it could use a dot or comma as separater. This could mean, that instead of 2 parameters, you actually got 4 parameters, because their decimal separator is a comma.
You could try to apply floatformat to prevent this from happening.
{{ city.0.0.latitude|floatformat:6 }},{{ city.0.0.longitude|floatformat:6 }}
Ok, got the core problem.
The Django latitude and longitude variables in my database are Decimal Fields. When I pass them from the template to the javascript function they are passing the first part of the decimal to the first argument and the second part of the decimal to the second argument instead of being two separate arguments.
for example... 36.620556 is a latitude in the following
onclick='showmap({{city.0.0.latitude}},{{city.0.0.longitude}});'
The function for showmap header is
showmap(lat,lng)
{...
However, lat and lan come out as follows
lat = 36 and lan = 620556
However the strange thing is that this doesn't happen if the default language is in English...
I'll play some more and see what I can find out...
SOLUTION:
As Reiner suggested it was the localisation that was the problem. I was getting commas in my decimal points so I wrote a quick template tag to replace the commas with a dot.
def fixlatlan(Object):
o = str(Object)
o = o.replace(",", ".")
return o
register.filter('fixlatlan', fixlatlan)
精彩评论