Pos Malaysia widget in Malay and Chinese (browser language detection)

March 2nd, 2010 | by Sean |
Browser language 'en'. Netherlands quote requests 1-to-NL.js

Shipping widget appearance for browser language 'en'.

Adding the Pos Parcel rates (ripped yesterday) to the previously ripped Pos Laju rates made it glaringly obvious how difficult it is to write software for data that’s in as bad a shape as is Pos Malaysia’s. With the problems in country names I’d already mentioned (Luxembourg/Luxzemboug, Netherlands/Netherland, United Kingdom/Great Britain), the benefit of replying to a shipping quote query by weight / country is lost. If the country names don’t match for different datasets, there’s no easy way of grouping different quotes for the same destination together.

I mentioned ISO3166-1 – the ISO Country Code standard before. I wrote a quick tool to map Pos Malaysia country names (there’s some really funny stuff in there!) onto ISO3166-1 2-letter codes in my version of their data, and instantly broke all my code. I’d written the demo code to use Pos Malaysia’s names, so I had to convert everything to use the 2-letter ISO codes. That finally merged all the strangely-named countries. Having done that, the only possibility left (it’s a good one!) for the API was to use the 2-letter codes. The query for 0.5kg to Netherlands is now:

http://spider.my/pos-malaysia-shipping-quote-2/0.5-to-NL.xml
(updated 31st Dec 2010)
http://spider.my/api/shipping-quote/0.5-to-NL-ex-MY.xml

Using two-letter codes also meant I lost my source of country names for the pop-up selection menus on my pages. Fortunately Java comes equipped with a Locale class which can provide a nice country name for a 2-letter code. Here’s the good bit though: it can give the name in any language for which it has the necessary platform data. Coverage is pretty good, so widely-spoken languages in Malaysia like Malay, English and Chinese are available for free! Unfortunately for the large local Tamil-speaking population, Tamil doesn’t seem to be included in the Java platform at the moment.

Since I was using Locale to provide country-code-to-name mapping, I added a small amount of extra code to detect browser language preference. Now if you use the widget demo, you’ll get it ‘your way’ if your browser is set to ask for Malay (ms), English (en – the default) or Chinese (zh). Other browser settings will probably get you a best-effort at country names and everything else in English.

Browser language 'ms' (Malay). Belanda (Netherlands) uses same 1-to-NL.js

Browser language 'ms' (Malay). Belanda (Netherlands) uses same 1-to-NL.js

Browser language 'zh' (Chinese). 荷兰 (Netherlands) uses same 1-to-NL.js

Browser language 'zh' (Chinese). 荷兰 (Netherlands) uses same 1-to-NL.js

That’s more or less as far as setting up an API for Pos shipping quotes can go. I’ve got one more demonstration of the API that I want to do, and then I’ll do a recap of the current state of the API. Not a hint of a reply to my emails to Pos yet, I’ll try phoning later in the week.

Post a Comment