Comment 4 for bug 1508169

Revision history for this message
Kyle Nitzsche (knitzsche) wrote :

Here's how we may be able to provide localized addresses inside golang.

You can run C code in golang. So we can get the localized address format (which shows the order of address components).

Include these lines near the top of the nominatum .go file:
//#include <locale.h>
//#include <libintl.h>
//#include <langinfo.h>
import "C"

Now we can access those C functions in go. Also need to convert C strings to go strings as below.

We can use the following to get the current locale's address format in your go:

        //ensure C knows the current locale. convert it to go string, and log it (don't log it in released code though)
        log.Printf("ADDY. locale: %q\n", C.GoString(C.setlocale(C.LC_ALL, C.CString(""))))

        //get the current locale's address format and covert it to a golang string
        addFmt := C.GoString(C.nl_langinfo(C._NL_ADDRESS_POSTAL_FMT))

I logged addFmt and ran my sample scope twice, in English/US locale and in Spanis/Spain locale and logged the result:
        log.Printf("ADDY: %q\n", addFmt)

Here's what was in the log, en/USA and es/Spain:

2015/11/03 14:23:22 ADDY. locale: "es_ES.utf8"
2015/11/03 14:23:22 ADDY: "%f%N%a%N%d%N%b%N%s %h %e %r%N%z %T%N%c%N"

2015/11/03 14:28:29 ADDY. locale: "en_US.utf8"
2015/11/03 14:28:29 ADDY: "%a%N%f%N%d%N%b%N%h %s %e %r%N%T, %S %z%N%c%N"

Note the format strings are different.

To understand the format string components, check http://www.eki.ee/itstandard/2000/LC_ADDRESS.shtml

Notice for example that %a means C/O Address, which in en/USA comes *first* but in es_ES comes *third*.

With this, we should be able to determine the order of address components for the current locale. We would then need to split the address returned from the web API into the corresponding parts and reconstruct an address from those parts using the order as returned above, in theory :) (and of course we'd need to handle invalid format string returned from the C code too, perhaps in that case using the address from the web API without modification.)