Language-based address formatting in android

Hi, in my application I need to display the input address according to the locale set by the user. Someone worked or turned to formatting or had some pointers in the Android platform.

+6
source share
2 answers

Check out googlei18n / libaddressinput: Googles mail address library including Android and Chromium . There are two modules in the project: android and: general. You only need: general to format the address to display the locale.

import android.location.Address; import android.support.annotation.NonNull; import android.text.TextUtils; import com.google.i18n.addressinput.common.AddressData; import com.google.i18n.addressinput.common.FormOptions; import com.google.i18n.addressinput.common.FormatInterpreter; ... public static String getFormattedAddress(@NonNull final Address address, @NonNull final String regionCode) { final FormatInterpreter formatInterpreter = new FormatInterpreter(new FormOptions().createSnapshot()); final AddressData addressData = (new AddressData.Builder() .setAddress(address.getThoroughfare()) .setLocality(address.getLocality()) .setAdminArea(address.getAdminArea()) .setPostalCode(address.getPostalCode()) .setCountry(regionCode) // REQUIRED .build()); // Fetch the address lines using getEnvelopeAddress, List<String> addressFragments = formatInterpreter.getEnvelopeAddress(addressData); // join them, and send them to the thread. return TextUtils.join(System.getProperty("line.separator"), addressFragments); } 

NOTE. regionCode must be a valid iso2 country code because the format interpreter format is used here. (See RegionDataConstants for a list of formats if you're interested.)

Sets the 2-letter region code of the CLDR address; see AddressData # getPostalCountry (). Unlike other values ​​passed to builder, the region code can never be null.

Example: US

801 CHESTNUT ST
ST. LOUIS, MO 63101

Example: JP

〒1600023
Nishishinjuku
3-2-11 NISHISHINJUKU SHINJUKU-KU TOKYO

+4
source

I did a little localization on other platforms. Here is the localization guide for Android: http://developer.android.com/guide/topics/resources/localization.html#using-framework

One approach is to use localized resources to store MessageFormat strings corresponding to the address layout for each required locale. From there, you will need a link to global standards for address blocks and the creation of format strings for each required locale.

+1
source

Source: https://habr.com/ru/post/919357/


All Articles