Last updated by 1 year ago

Page: Draft_I18NTagLib, Version:3

Draft 18NTagLIb

Embryonic support for country selection drop-downs, and localized date display. Country names in English only currently but I have them in 4 or so languages once I work out how to do this.

In future will support restricting range of countries listed, and setting the order (default = server-locale sort order of country name).

Requires Grails 0.4

import java.text.DateFormat

/** * An embryonic I18N taglib for providing tools relating to making applications * internationalized with respect to language selections, country selections etc * This is badly named but we need to work out where this stuff really should live. * TODO: I believe there is an encoding issue - this file is not loaded in UTF-8 so the Åland Islands comes out funny * in the browser as it is mutated before the browser gets it */ class I18NTagLib { static final ISO3166_3 = [ "AFG":"Afghanistan", "ALB":"Albania", "ATA":"Antarctica", "DZA":"Algeria", "ASM":"American Samoa", "AND":"Andorra", "AGO":"Angola", "ATG":"Antigua and Barbuda", "AZE":"Azerbaijan", "ARG":"Argentina", "AUS":"Australia", "AUT":"Austria", "BHS":"Bahamas", "BHR":"Bahrain", "BGD":"Bangladesh", "ARM":"Armenia", "BRB":"Barbados", "BEL":"Belgium", "BMU":"Bermuda", "BTN":"Bhutan", "BOL":"Bolivia", "BIH":"Bosnia and Herzegovina", "BWA":"Botswana", "BVT":"Bouvet Island", "BRA":"Brazil", "BLZ":"Belize", "IOT":"British Indian Ocean Territory", "SLB":"Solomon Islands", "VGB":"British Virgin Islands", "BRN":"Brunei Darussalam", "BGR":"Bulgaria", "MMR":"Myanmar", "BDI":"Burundi", "BLR":"Belarus", "KHM":"Cambodia", "CMR":"Cameroon", "CAN":"Canada", "CPV":"Cape Verde", "CYM":"Cayman Islands", "CAF":"Central African", "LKA":"Sri Lanka", "TCD":"Chad", "CHL":"Chile", "CHN":"China", "TWN":"Taiwan", "CXR":"Christmas Island", "CCK":"Cocos (Keeling) Islands", "COL":"Colombia", "COM":"Comoros", "MYT":"Mayotte", "COG":"Republic of the Congo", "COD":"The Democratic Republic Of The Congo", "COK":"Cook Islands", "CRI":"Costa Rica", "HRV":"Croatia", "CUB":"Cuba", "CYP":"Cyprus", "CZE":"Czech Republic", "BEN":"Benin", "DNK":"Denmark", "DMA":"Dominica", "DOM":"Dominican Republic", "ECU":"Ecuador", "SLV":"El Salvador", "GNQ":"Equatorial Guinea", "ETH":"Ethiopia", "ERI":"Eritrea", "EST":"Estonia", "FRO":"Faroe Islands", "FLK":"Falkland Islands", "SGS":"South Georgia and the South Sandwich Islands", "FJI":"Fiji", "FIN":"Finland", "ALA":"Åland Islands", "FRA":"France", "GUF":"French Guiana", "PYF":"French Polynesia", "ATF":"French Southern Territories", "DJI":"Djibouti", "GAB":"Gabon", "GEO":"Georgia", "GMB":"Gambia", "PSE":"Occupied Palestinian Territory", "DEU":"Germany", "GHA":"Ghana", "GIB":"Gibraltar", "KIR":"Kiribati", "GRC":"Greece", "GRL":"Greenland", "GRD":"Grenada", "GLP":"Guadeloupe", "GUM":"Guam", "GTM":"Guatemala", "GIN":"Guinea", "GUY":"Guyana", "HTI":"Haiti", "HMD":"Heard Island and McDonald Islands", "VAT":"Vatican City State", "HND":"Honduras", "HKG":"Hong Kong", "HUN":"Hungary", "ISL":"Iceland", "IND":"India", "IDN":"Indonesia", "IRN":"Islamic Republic of Iran", "IRQ":"Iraq", "IRL":"Ireland", "ISR":"Israel", "ITA":"Italy", "CIV":"Côte d'Ivoire", "JAM":"Jamaica", "JPN":"Japan", "KAZ":"Kazakhstan", "JOR":"Jordan", "KEN":"Kenya", "PRK":"Democratic People's Republic of Korea", "KOR":"Republic of Korea", "KWT":"Kuwait", "KGZ":"Kyrgyzstan", "LAO":"Lao People's Democratic Republic", "LBN":"Lebanon", "LSO":"Lesotho", "LVA":"Latvia", "LBR":"Liberia", "LBY":"Libyan Arab Jamahiriya", "LIE":"Liechtenstein", "LTU":"Lithuania", "LUX":"Luxembourg", "MAC":"Macao", "MDG":"Madagascar", "MWI":"Malawi", "MYS":"Malaysia", "MDV":"Maldives", "MLI":"Mali", "MLT":"Malta", "MTQ":"Martinique", "MRT":"Mauritania", "MUS":"Mauritius", "MEX":"Mexico", "MCO":"Monaco", "MNG":"Mongolia", "MDA":"Republic of Moldova", "MSR":"Montserrat", "MAR":"Morocco", "MOZ":"Mozambique", "OMN":"Oman", "NAM":"Namibia", "NRU":"Nauru", "NPL":"Nepal", "NLD":"Netherlands", "ANT":"Netherlands Antilles", "ABW":"Aruba", "NCL":"New Caledonia", "VUT":"Vanuatu", "NZL":"New Zealand", "NIC":"Nicaragua", "NER":"Niger", "NGA":"Nigeria", "NIU":"Niue", "NFK":"Norfolk Island", "NOR":"Norway", "MNP":"Northern Mariana Islands", "UMI":"United States Minor Outlying Islands", "FSM":"Federated States of Micronesia", "MHL":"Marshall Islands", "PLW":"Palau", "PAK":"Pakistan", "PAN":"Panama", "PNG":"Papua New Guinea", "PRY":"Paraguay", "PER":"Peru", "PHL":"Philippines", "PCN":"Pitcairn", "POL":"Poland", "PRT":"Portugal", "GNB":"Guinea-Bissau", "TLS":"Timor-Leste", "PRI":"Puerto Rico", "QAT":"Qatar", "REU":"Réunion", "ROU":"Romania", "RUS":"Russian Federation", "RWA":"Rwanda", "SHN":"Saint Helena", "KNA":"Saint Kitts and Nevis", "AIA":"Anguilla", "LCA":"Saint Lucia", "SPM":"Saint-Pierre and Miquelon", "VCT":"Saint Vincent and the Grenadines", "SMR":"San Marino", "STP":"Sao Tome and Principe", "SAU":"Saudi Arabia", "SEN":"Senegal", "SYC":"Seychelles", "SLE":"Sierra Leone", "SGP":"Singapore", "SVK":"Slovakia", "VNM":"Vietnam", "SVN":"Slovenia", "SOM":"Somalia", "ZAF":"South Africa", "ZWE":"Zimbabwe", "ESP":"Spain", "ESH":"Western Sahara", "SDN":"Sudan", "SUR":"Suriname", "SJM":"Svalbard and Jan Mayen", "SWZ":"Swaziland", "SWE":"Sweden", "CHE":"Switzerland", "SYR":"Syrian Arab Republic", "TJK":"Tajikistan", "THA":"Thailand", "TGO":"Togo", "TKL":"Tokelau", "TON":"Tonga", "TTO":"Trinidad and Tobago", "ARE":"United Arab Emirates", "TUN":"Tunisia", "TUR":"Turkey", "TKM":"Turkmenistan", "TCA":"Turks and Caicos Islands", "TUV":"Tuvalu", "UGA":"Uganda", "UKR":"Ukraine", "MKD":"The Former Yugoslav Republic of Macedonia", "EGY":"Egypt", "GBR":"United Kingdom", "IMN":"Isle of Man", "TZA":"United Republic Of Tanzania", "USA":"United States", "VIR":"U.S. Virgin Islands", "BFA":"Burkina Faso", "URY":"Uruguay", "UZB":"Uzbekistan", "VEN":"Venezuela", "WLF":"Wallis and Futuna", "WSM":"Samoa", "YEM":"Yemen", "SCG":"Serbia and Montenegro", "ZMB":"Zambia", ]

static final COUNTRY_CODES_BY_NAME_ORDER = ISO3166_3.entrySet().sort( { a, b -> a.value.compareTo(b.value) } ).collect() { it.key }

/** * Display a country selection combo box. * @todo Update this to select names from message bundle based on client's locale, and to allow setting of list of * permitted country codes and to change the sort order (same thing - a list of country codes) */ def selectCountry = { attrs -> attrs['from'] = COUNTRY_CODES_BY_NAME_ORDER attrs['optionValue'] = { ISO3166_3[it] } attrs['value'] = attrs['value'] ? attrs['value'] : attrs['default'] select( attrs) }

/** * Display a date value using client's locale for formatting * Options for "style" attribute include 'long', 'medium' and 'short'. It will use the Java VM's * localized interpretation of these. */ def date = { attrs -> def value = attrs['value'] if (value) { def style = attrs['style']?.toLowerCase(request.locale) def styleCode = DateFormat.SHORT if (style) { switch (style) { case 'long' : styleCode = DateFormat.LONG; break; case 'medium' : styleCode = DateFormat.MEDIUM; break; case 'short' : styleCode = DateFormat.SHORT; break; } } def formatter = DateFormat.getDateInstance(styleCode) out << formatter.format(value).encodeHTML() } }

/** *Display the country name given the iso3 code * Tag takes attribute countryCode */ def displayCountry = {attrs-> out << ISO3166_3[attrs.countryCode.toUpperCase()] }

}