<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /bin/false

# vim: set autoindent shiftwidth=4 tabstop=4:

# Portable methods for locale handling.
# Copyright (C) 2002-2013 Guido Flohr &lt;guido@imperia.net&gt;,
# all rights reserved.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2, or (at your option)
# any later version.
                                         
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public 
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
# USA.

package Locale::Util;

use strict;

use constant DEBUG =&gt; 0;

use base qw (Exporter);

use vars qw (@EXPORT_OK);

@EXPORT_OK = qw (parse_http_accept_language
				 parse_http_accept_charset
				 set_locale set_locale_cache get_locale_cache
				 web_set_locale);

# The following list maps languages to a rough guess of the country that
# is most likely to be meant if no locale info for the country alone is
# found.  I have assembled the list to the best of my knowledge, preferring
# the country that has the language as its official language, and in doubt
# listing the country that has the most speakers of that language.  Corrections
# are welcome.
use constant LANG2COUNTRY =&gt; {
    aa =&gt; 'ET', # Afar =&gt; Ethiopia
    ab =&gt; 'AB', # Abkhazian =&gt; Georgia
    # ae =&gt; '??', # Avestan =&gt; ??, Iran?
    af =&gt; 'za',  # Afrikaans =&gt; South Africa
    am =&gt; 'ET', # Amharic =&gt; Ethiopia
    ar =&gt; 'EG', # Arabic =&gt; Egypt
    as =&gt; 'IN', # Assamese =&gt; India
    ay =&gt; 'BO', # Aymara =&gt; Bolivia
    az =&gt; 'AZ', # Azerbaijani =&gt; Azerbaijan
    ba =&gt; 'RU', # Bashkir =&gt; Russia
    be =&gt; 'BY', # Belarusian =&gt; Belarus
    bg =&gt; 'BG', # Bulgarian =&gt; Bulgaria
    bh =&gt; 'IN', # Bihari =&gt; India
    bi =&gt; 'VU', # Bislama =&gt; Vanuatu
    bn =&gt; 'BD', # Bengali =&gt; Bangladesh
    bo =&gt; 'CN', # Tibetan =&gt; China
    br =&gt; 'FR', # Breton =&gt; France
    bs =&gt; 'BA', # Bosnian =&gt; Bosnia and Herzegovina
    ca =&gt; 'ES', # Catalan =&gt; Spain
    ce =&gt; 'RU', # Chechen =&gt; Russia
    ch =&gt; '??', # Chamorro =&gt; Guam (or mp?)
    co =&gt; 'FR', # Corsican =&gt; France
    cs =&gt; 'CZ', # Czech =&gt; Czech Republic
    cu =&gt; 'BG', # Church Slavic =&gt; Bulgaria
    cv =&gt; 'RU', # Chuvash =&gt; Russia
    cy =&gt; 'GB', # Welsh =&gt; United Kingdom
    da =&gt; 'DK', # Danish =&gt; Denmark
    de =&gt; 'DE', # German =&gt; Germany
    dz =&gt; 'BT', # Dzongkha =&gt; Bhutan
    el =&gt; 'GR', # Greek =&gt; Greece
    en =&gt; 'US', # English =&gt; United States
    es =&gt; 'ES', # Actually Mexico and the US have more Spanish speakers
                # than Spain.  But it can be assumed that they either add
                # the country to their browser settings or will not care
                # to much.
    et =&gt; 'EE', # Estonian =&gt; Estonia
    fa =&gt; 'IR', # Iran, Islamic Republic of
    fi =&gt; 'FI', # Finnish =&gt; Finland
    fj =&gt; 'FJ', # Fijian =&gt; Fiji
    fo =&gt; 'FO', # Faeroese =&gt; Faroe Islands
    fr =&gt; 'FR', # French =&gt; France
    fy =&gt; 'FY', # Frisian =&gt; Netherlands
    ga =&gt; 'IE', # Irish =&gt; Ireland
    gd =&gt; 'GB', # Gaelic (Scots) =&gt; United Kingdom
    gl =&gt; 'ES', # Gallegan =&gt; Spain
    gn =&gt; 'PY', # Guarani =&gt; Paraguay
    gu =&gt; 'IN', # Gujarati =&gt; IN
    gv =&gt; 'GB', # Manx =&gt; United Kingdom
    ha =&gt; 'NE', # Hausa =&gt; Niger (ng?)
    he =&gt; 'IL', # Hebrew =&gt; Israel
    hi =&gt; 'IN', # Hindi =&gt; India
    ho =&gt; 'PG', # Hiri Motu =&gt; Papua New Guinea
    hr =&gt; 'HR', # Croatian
    hu =&gt; 'HU', # Hungarian =&gt; Hungary
    hy =&gt; 'AM', # Armenian =&gt; Armenia
    hz =&gt; 'NA', # Herero =&gt; Namibia
    # ia =&gt; '??', # Interlingua (aka "latino sine flexione") =&gt; ??
    id =&gt; 'ID', # Indonesian =&gt; Indonesia
    # ie =&gt; '??', # Interlingue =&gt; ???
    ik =&gt; 'US', # Inupiaq =&gt; United States
    is =&gt; 'IS', # Icelandic =&gt; Iceland
    it =&gt; 'IT', # Italian =&gt; Italy
    iu =&gt; 'CA', # Inuktitut =&gt; Canada
    iw =&gt; 'IL', # Hebrew =&gt; Israel
    ja =&gt; 'JP', # Japanese =&gt; Japan
    jw =&gt; 'ID', # Javanese =&gt; Indonesia
    ka =&gt; 'GE', # Georgian =&gt; Georgia
    ki =&gt; 'KE', # Kikuyu =&gt; Kenya
    kj =&gt; 'AO', # Kuanyama =&gt; Angola (na?)
    kk =&gt; 'KZ', # Kazakh =&gt; Kazakhstan
    kl =&gt; 'GL', # Kalaallisut =&gt; Greenland
    km =&gt; 'KH', # Khmer =&gt; Cambodia
    kn =&gt; 'IN', # Kannada =&gt; India
    ko =&gt; 'KR', # Korean =&gt; Korea, Republic of (more speakers than North Korea)
    ks =&gt; 'IN', # Kashmiri =&gt; India
    ku =&gt; 'TR', # Kurdish =&gt; Turkey
    kv =&gt; 'RU', # Komi =&gt; Russia
    kw =&gt; 'GB', # Cornish =&gt; United Kingdom
    ky =&gt; 'KG', # Kirghyz =&gt; Kyrgyzstan
    la =&gt; 'VA', # Latin =&gt; Holy See (Vatican City State)
    lb =&gt; 'LU', # Letzeburgesch =&gt; Luxembourg
    ln =&gt; 'CG', # Lingala =&gt; Republic of the Congo (cd?)
    lo =&gt; 'LA', # Lao =&gt; Lao People's Democratic Republic
    lt =&gt; 'LT', # Lithuanian =&gt; Lithuania
    lv =&gt; 'LV', # Latvian =&gt; Latvia
    mg =&gt; 'MG', # Malagasy =&gt; Madagascar
    mh =&gt; 'MH', # Marshall =&gt; Marshall Islands
    mi =&gt; 'NZ', # Maori =&gt; New Zealand
    mk =&gt; 'MK', # Macedonian =&gt; Macedonia, the Former Yugoslav Republic of
    ml =&gt; 'IN', # Malayalam =&gt; India
    mn =&gt; 'MN', # Mongolian =&gt; Mongolia
    mr =&gt; 'IN', # Marathi =&gt; India
    ms =&gt; 'MY', # Malay =&gt; Malaysia (FIXME: not really sure ...)
    mt =&gt; 'MT', # Maltese =&gt; Malta
    my =&gt; 'MM', # Burmese =&gt; Myanmar
    na =&gt; 'NR', # Nauru =&gt; Nauru
    nb =&gt; 'NO', # Norwegian BokmĂĄl =&gt; Norway
    nd =&gt; 'ZA', # Ndebele, North =&gt; South Africa
    ne =&gt; 'NP', # Nepali =&gt; Nepal
    ng =&gt; 'NA', # Ndonga =&gt; Namibia
    nl =&gt; 'NL', # Dutch =&gt; Netherlands
    nn =&gt; 'NO', # Norwegian Nynorsk =&gt; Norway
    no =&gt; 'NO', # Norwegian =&gt; Norway
    nr =&gt; 'ZA', # Ndebele, South =&gt; South Africa
    nv =&gt; 'US', # Navajo =&gt; United States
    ny =&gt; 'MW', # Chichewa; Nyanja =&gt; Malawi
    oc =&gt; 'FR', # Occitan (post 1500) =&gt; France
    om =&gt; 'ET', # Oromo =&gt; Ethiopia
    or =&gt; 'IN', # Oriya =&gt; India
    os =&gt; 'RU', # Ossetian; Ossetic =&gt; Russia (FIXME: Or Georgia?)
    pa =&gt; 'IN', # Panjabi =&gt; India
    pi =&gt; 'IN', # Pali =&gt; India (FIXME: Or Thailand, Sri Lanka, Myanmar,
                # Cambodia)
    pl =&gt; 'PL', # Polish =&gt; Poland
    ps =&gt; 'PK', # Pushto =&gt; Pakistan
    pt =&gt; 'PT', # Portuguese =&gt; Portugal (following our rules this should
                # actually be Brazil but that would be to unrealistic,
                # people from Brazil set their locale to pt_BR).
    qu =&gt; 'PE', # Quechua =&gt; Peru
    rm =&gt; 'CH', # Rhaeto-Romance =&gt; Switzerland
    rn =&gt; 'RW', # Rundi =&gt; Rwanda
    ro =&gt; 'RO', # Romanian =&gt; Romania
    ru =&gt; 'RU', # Russian =&gt; Russia
    rw =&gt; 'RW', # Kinyarwanda =&gt; Rwanda
    sa =&gt; 'IN', # Sanskrit =&gt; India
    sc =&gt; 'IT', # Sardinian =&gt; Italy
    sd =&gt; 'IN', # Sindhi =&gt; India
    se =&gt; 'SE', # Sami =&gt; Sweden (Totally unsure here.  The Sami languages 
	            # are also spoken in Norway, Finland and Russia, but the 
                # largest part of the area seems to be in Sweden. 
    sg =&gt; '??', # Sango =&gt; Central African Republic
    si =&gt; 'LK', # Sinhalese =&gt; Sri Lanka
    sk =&gt; 'SK', # Slovakian =&gt; Slovakia
    sl =&gt; 'SI', # Slovenian =&gt; Slovenia
    sm =&gt; 'WS', # Samoan =&gt; Samoa
    sh =&gt; 'ZW', # Shona =&gt; Zimbabwe (FIXME: Rather Mozambique?)
    so =&gt; 'SO', # Somali =&gt; Somalia
    sq =&gt; 'AL', # Albanian =&gt; Albania
    sr =&gt; 'YU', # Serbian =&gt; Yugoslavia
    ss =&gt; '??', # Swati =&gt; Swaziland (za?)
    st =&gt; 'LS', # Sotho =&gt; Lesotho
    su =&gt; 'IN', # Sundanese =&gt; Indonesia
    sv =&gt; 'SE', # Swedish =&gt; Sweden
    sw =&gt; 'TZ', # Suaheli =&gt; Tanzania, United Republic of
    ta =&gt; 'LK', # Tamil =&gt; Sri Lanka
    te =&gt; 'IN', # Telugu =&gt; India
    tg =&gt; 'TJ', # Tajik =&gt; Tajikistan
    th =&gt; 'TH', # Thai =&gt; Thailand
    ti =&gt; 'ER', # Tigrinya =&gt; Eritrea
    tk =&gt; 'TM', # Turkmen =&gt; Turkmenistan
    tl =&gt; 'PH', # Tagalog =&gt; Philippines
    tn =&gt; 'BW', # Tswana =&gt; Botswana
    to =&gt; 'TO', # Tonga =&gt; Tonga
    tr =&gt; 'TR', # Turkish =&gt; Turkish
    tt =&gt; 'RU', # Tatar =&gt; Russia
    tw =&gt; 'GH', # Twi =&gt; Ghana
    ug =&gt; 'CN', # Uighur =&gt; China
    uk =&gt; 'UA', # Ukrainian =&gt; Ukraine
    ur =&gt; 'PK', # Urdu =&gt; Pakistan
    uz =&gt; 'UZ', # Uzbek =&gt; Uzbekistan
    vi =&gt; 'VN', # Vietnamese =&gt; Vietnam
    # vo =&gt; '??', # Volapuk =&gt; Nowhere
    wo =&gt; 'SN', # Wolof =&gt; Senegal
    xh =&gt; 'ZA', # Xhosa =&gt; South Africa
    yi =&gt; 'IL', # Yiddish =&gt; Israel (FIXME: Rather United States?)
    yo =&gt; 'NG', # Yoruba =&gt; Nigeria
    za =&gt; 'CN', # Zhuang =&gt; China
    zh =&gt; 'CN', # Chinese =&gt; China
    zu =&gt; 'ZA', # Zulu =&gt; South Africa
};

use constant WIN32LANGUAGE =&gt; {
    aa =&gt; "Afar",
    ab =&gt; "Abkhazian",
    ae =&gt; "Avestan",
    af =&gt; "Afrikaans",
    am =&gt; "Amharic",
    ar =&gt; "Arabic",
    as =&gt; "Assamese",
    ay =&gt; "Aymara",
    az =&gt; "Azerbaijani",
    ba =&gt; "Bashkir",
    be =&gt; "Belarusian",
    bg =&gt; "Bulgarian",
    bh =&gt; "Bihari",
    bi =&gt; "Bislama",
    bn =&gt; "Bengali",
    bo =&gt; "Tibetan",
    br =&gt; "Breton",
    bs =&gt; "Bosnian",
    ca =&gt; "Catalan",
    ce =&gt; "Chechen",
    ch =&gt; "Chamorro",
    co =&gt; "Corsican",
    cs =&gt; "Czech",
    cu =&gt; "Church Slavic",
    cv =&gt; "Chuvash",
    cy =&gt; "Welsh",
    da =&gt; "Danish",
    de =&gt; "German",
    dz =&gt; "Dzongkha",
    el =&gt; "Greek",
    en =&gt; "English",
    eo =&gt; "Esperanto",
    es =&gt; "Spanish",
    et =&gt; "Estonian",
    eu =&gt; "Basque",
    fa =&gt; "Persian",
    fi =&gt; "Finnish",
    fj =&gt; "Fijian",
    fo =&gt; "Faeroese",
    fr =&gt; "French",
    fy =&gt; "Frisian",
    ga =&gt; "Irish",
    gd =&gt; "Gaelic (Scots)",
    gl =&gt; "Gallegan",
    gn =&gt; "Guarani",
    gu =&gt; "Gujarati",
    gv =&gt; "Manx",
    ha =&gt; "Hausa",
    he =&gt; "Hebrew",
    hi =&gt; "Hindi",
    ho =&gt; "Hiri Motu",
    hr =&gt; "Croatian",
    hu =&gt; "Hungarian",
    hy =&gt; "Armenian",
    hz =&gt; "Herero",
    ia =&gt; "Interlingua",
    id =&gt; "Indonesian",
    ie =&gt; "Interlingue",
    ik =&gt; "Inupiaq",
    is =&gt; "Icelandic",
    it =&gt; "Italian",
    iu =&gt; "Inuktitut",
    ja =&gt; "Japanese",
    jw =&gt; "Javanese",
    ka =&gt; "Georgian",
    ki =&gt; "Kikuyu",
    kj =&gt; "Kuanyama",
    kk =&gt; "Kazakh",
    kl =&gt; "Kalaallisut",
    km =&gt; "Khmer",
    kn =&gt; "Kannada",
    ko =&gt; "Korean",
    ks =&gt; "Kashmiri",
    ku =&gt; "Kurdish",
    kv =&gt; "Komi",
    kw =&gt; "Cornish",
    ky =&gt; "Kirghiz",
    la =&gt; "Latin",
    lb =&gt; "Letzeburgesch",
    ln =&gt; "Lingala",
    lo =&gt; "Lao",
    lt =&gt; "Lithuanian",
    lv =&gt; "Latvian",
    mg =&gt; "Malagasy",
    mh =&gt; "Marshall",
    mi =&gt; "Maori",
    # Sorry, lads, but that is what M$ calls your language ...
    mk =&gt; "FYRO Macedonian",
    ml =&gt; "Malayalam",
    mn =&gt; "Mongolian",
    mo =&gt; "Moldavian",
    mr =&gt; "Marathi",
    ms =&gt; "Malay",
    mt =&gt; "Maltese",
    my =&gt; "Burmese",
    na =&gt; "Nauru",
    nb =&gt; "Norwegian (BokmĂĄl)",
    nd =&gt; "Ndebele, North",
    ne =&gt; "Nepali",
    ng =&gt; "Ndonga",
    nl =&gt; "Dutch",
    nn =&gt; "Norwegian-Nynorsk",
    no =&gt; "Norwegian-Nynorsk",
    nr =&gt; "Ndebele, South",
    nv =&gt; "Navajo",
    ny =&gt; "Chichewa",
    oc =&gt; "Occitan (post 1500)",
    om =&gt; "Oromo",
    or =&gt; "Oriya",
    os =&gt; "Ossetian",
    pa =&gt; "Panjabi",
    pi =&gt; "Pali",
    pl =&gt; "Polish",
    ps =&gt; "Pushto",
    pt =&gt; "Portuguese",
    qu =&gt; "Quechua",
    rm =&gt; "Rhaeto-Romance",
    rn =&gt; "Rundi",
    ro =&gt; "Romanian",
    ru =&gt; "Russian",
    rw =&gt; "Kinyarwanda",
    sa =&gt; "Sanskrit",
    sc =&gt; "Sardinian",
    sd =&gt; "Sindhi",
    se =&gt; "Sami",
    sg =&gt; "Sango",
    si =&gt; "Sinhalese",
    sk =&gt; "Slovak",
    sl =&gt; "Slovenian",
    sm =&gt; "Samoan",
    sn =&gt; "Shona",
    so =&gt; "Somali",
    sq =&gt; "Albanian",
    sr =&gt; "Serbian",
    ss =&gt; "Swati",
    st =&gt; "Sotho",
    su =&gt; "Sundanese",
    sv =&gt; "Swedish",
    sw =&gt; "Swahili",
    ta =&gt; "Tamil",
    te =&gt; "Telugu",
    tg =&gt; "Tajik",
    th =&gt; "Thai",
    ti =&gt; "Tigrinya",
    tk =&gt; "Turkmen",
    tl =&gt; "Tagalog",
    tn =&gt; "Tswana",
    to =&gt; "Tonga",
    tr =&gt; "Turkish",
    ts =&gt; "Tsonga",
    tt =&gt; "Tatar",
    tw =&gt; "Twi",
    ug =&gt; "Uighur",
    uk =&gt; "Ukrainian",
    ur =&gt; "Urdu",
    uz =&gt; "Uzbek",
    vi =&gt; "Vietnamese",
    vo =&gt; "Volapuk",
    wo =&gt; "Wolof",
    xh =&gt; "Xhosa",
    yi =&gt; "Yiddish",
    yo =&gt; "Yoruba",
    za =&gt; "Zhuang",
    zh =&gt; "Chinese",
    zu =&gt; "Zulu",
};

use constant WIN32COUNTRY =&gt; {
    ad =&gt; "Andorra",
    ae =&gt; "United Arab Emirates",
    af =&gt; "Afghanistan",
    ag =&gt; "Antigua and Barbuda",
    ai =&gt; "Anguilla",
    al =&gt; "Albania",
    am =&gt; "Armenia",
    an =&gt; "Netherlands Antilles",
    ao =&gt; "Angola",
    aq =&gt; "Antarctica",
    ar =&gt; "Argentina",
    as =&gt; "American Samoa",
    at =&gt; "Austria",
    au =&gt; "Australia",
    aw =&gt; "Aruba",
    ax =&gt; "Aland Islands",
    az =&gt; "Azerbaijan",
    ba =&gt; "Bosnia and Herzegovina",
    bb =&gt; "Barbados",
    bd =&gt; "Bangladesh",
    be =&gt; "Belgium",
    bf =&gt; "Burkina Faso",
    bg =&gt; "Bulgaria",
    bh =&gt; "Bahrain",
    bi =&gt; "Burundi",
    bj =&gt; "Benin",
    bm =&gt; "Bermuda",
    bn =&gt; "Brunei Darussalam",
    bo =&gt; "Bolivia",
    br =&gt; "Brazil",
    bs =&gt; "Bahamas",
    bt =&gt; "Bhutan",
    bv =&gt; "Bouvet Island",
    bw =&gt; "Botswana",
    by =&gt; "Belarus",
    bz =&gt; "Belize",
    ca =&gt; "Canada",
    cc =&gt; "Cocos (Keeling) Islands",
    cd =&gt; "Congo, The Democratic Republic of the",
    cf =&gt; "Central African Republic",
    cg =&gt; "Congo",
    ch =&gt; "Switzerland",
    ci =&gt; "Cote D'Ivoire",
    ck =&gt; "Cook Islands",
    cl =&gt; "Chile",
    cm =&gt; "Cameroon",
    cn =&gt; "China",
    co =&gt; "Colombia",
    cr =&gt; "Costa Rica",
    cs =&gt; "Serbia and Montenegro",
    cu =&gt; "Cuba",
    cv =&gt; "Cape Verde",
    cx =&gt; "Christmas Island",
    cy =&gt; "Cyprus",
    cz =&gt; "Czech Republic",
    de =&gt; "Germany",
    dj =&gt; "Djibouti",
    dk =&gt; "Denmark",
    dm =&gt; "Dominica",
    do =&gt; "Dominican Republic",
    dz =&gt; "Algeria",
    ec =&gt; "Ecuador",
    ee =&gt; "Estonia",
    eg =&gt; "Egypt",
    eh =&gt; "Western Sahara",
    er =&gt; "Eritrea",
    es =&gt; "Spain",
    et =&gt; "Ethiopia",
    fi =&gt; "Finland",
    fj =&gt; "Fiji",
    fk =&gt; "Falkland Islands (Malvinas)",
    fm =&gt; "Micronesia, Federated States of",
    fo =&gt; "Faroe Islands",
    fr =&gt; "France",
    fx =&gt; "France, Metropolitan",
    ga =&gt; "Gabon",
    gb =&gt; "United Kingdom",
    gd =&gt; "Grenada",
    ge =&gt; "Georgia",
    gf =&gt; "French Guiana",
    gh =&gt; "Ghana",
    gi =&gt; "Gibraltar",
    gl =&gt; "Greenland",
    gm =&gt; "Gambia",
    gn =&gt; "Guinea",
    gp =&gt; "Guadeloupe",
    gq =&gt; "Equatorial Guinea",
    gr =&gt; "Greece",
    gs =&gt; "South Georgia and the South Sandwich Islands",
    gt =&gt; "Guatemala",
    gu =&gt; "Guam",
    gw =&gt; "Guinea-Bissau",
    gy =&gt; "Guyana",
    hk =&gt; "Hong Kong",
    hm =&gt; "Heard Island and McDonald Islands",
    hn =&gt; "Honduras",
    hr =&gt; "Croatia",
    ht =&gt; "Haiti",
    hu =&gt; "Hungary",
    id =&gt; "Indonesia",
    ie =&gt; "Ireland",
    il =&gt; "Israel",
    in =&gt; "India",
    io =&gt; "British Indian Ocean Territory",
    iq =&gt; "Iraq",
    ir =&gt; "Iran",
    is =&gt; "Iceland",
    it =&gt; "Italy",
    jm =&gt; "Jamaica",
    jo =&gt; "Jordan",
    jp =&gt; "Japan",
    ke =&gt; "Kenya",
    kg =&gt; "Kyrgyzstan",
    kh =&gt; "Cambodia",
    ki =&gt; "Kiribati",
    km =&gt; "Comoros",
    kn =&gt; "Saint Kitts and Nevis",
    kp =&gt; "North-Korea",
    kr =&gt; "Korea",
    kw =&gt; "Kuwait",
    ky =&gt; "Cayman Islands",
    kz =&gt; "Kazakhstan",
    la =&gt; "Laos",
    lb =&gt; "Lebanon",
    lc =&gt; "Saint Lucia",
    li =&gt; "Liechtenstein",
    lk =&gt; "Sri Lanka",
    lr =&gt; "Liberia",
    ls =&gt; "Lesotho",
    lt =&gt; "Lithuania",
    lu =&gt; "Luxembourg",
    lv =&gt; "Latvia",
    ly =&gt; "Libyan",
    ma =&gt; "Morocco",
    mc =&gt; "Monaco",
    md =&gt; "Moldova",
    mg =&gt; "Madagascar",
    mh =&gt; "Marshall Islands",
    mk =&gt; "Former Yugoslav Republic of Macedonia",
    ml =&gt; "Mali",
    mm =&gt; "Myanmar",
    mn =&gt; "Mongolia",
    mo =&gt; "Macao",
    mp =&gt; "Northern Mariana Islands",
    mq =&gt; "Martinique",
    mr =&gt; "Mauritania",
    ms =&gt; "Montserrat",
    mt =&gt; "Malta",
    mu =&gt; "Mauritius",
    mv =&gt; "Maldives",
    mw =&gt; "Malawi",
    mx =&gt; "Mexico",
    my =&gt; "Malaysia",
    mz =&gt; "Mozambique",
    na =&gt; "Namibia",
    nc =&gt; "New Caledonia",
    ne =&gt; "Niger",
    nf =&gt; "Norfolk Island",
    ng =&gt; "Nigeria",
    ni =&gt; "Nicaragua",
    nl =&gt; "Netherlands",
    no =&gt; "Norway",
    np =&gt; "Nepal",
    nr =&gt; "Nauru",
    nu =&gt; "Niue",
    nz =&gt; "New Zealand",
    om =&gt; "Oman",
    pa =&gt; "Panama",
    pe =&gt; "Peru",
    pf =&gt; "French Polynesia",
    pg =&gt; "Papua New Guinea",
    ph =&gt; "Philippines",
    pk =&gt; "Pakistan",
    pl =&gt; "Poland",
    pm =&gt; "Saint Pierre and Miquelon",
    pn =&gt; "Pitcairn",
    pr =&gt; "Puerto Rico",
    ps =&gt; "Palestinian Territory, Occupied",
    pt =&gt; "Portugal",
    pw =&gt; "Palau",
    py =&gt; "Paraguay",
    qa =&gt; "Qatar",
    re =&gt; "Reunion",
    ro =&gt; "Romania",
    ru =&gt; "Russian Federation",
    rw =&gt; "Rwanda",
    sa =&gt; "Saudi Arabia",
    sb =&gt; "Solomon Islands",
    sc =&gt; "Seychelles",
    sd =&gt; "Sudan",
    se =&gt; "Sweden",
    sg =&gt; "Singapore",
    sh =&gt; "Saint Helena",
    si =&gt; "Slovenia",
    sj =&gt; "Svalbard and Jan Mayen",
    sk =&gt; "Slovakia",
    sl =&gt; "Sierra Leone",
    sm =&gt; "San Marino",
    sn =&gt; "Senegal",
    so =&gt; "Somalia",
    sr =&gt; "Suriname",
    st =&gt; "Sao Tome and Principe",
    sv =&gt; "El Salvador",
    sy =&gt; "Syrian Arab Republic",
    sz =&gt; "Swaziland",
    tc =&gt; "Turks and Caicos Islands",
    td =&gt; "Chad",
    tf =&gt; "French Southern Territories",
    tg =&gt; "Togo",
    th =&gt; "Thailand",
    tj =&gt; "Tajikistan",
    tk =&gt; "Tokelau",
    tl =&gt; "Timor-Leste",
    tm =&gt; "Turkmenistan",
    tn =&gt; "Tunisia",
    to =&gt; "Tonga",
    tr =&gt; "Turkey",
    tt =&gt; "Trinidad and Tobago",
    tv =&gt; "Tuvalu",
    tw =&gt; "Taiwan, Province of China",
    tz =&gt; "Tanzania, United Republic of",
    ua =&gt; "Ukraine",
    ug =&gt; "Uganda",
    um =&gt; "United States Minor Outlying Islands",
    us =&gt; "United States",
    uy =&gt; "Uruguay",
    uz =&gt; "Uzbekistan",
    va =&gt; "Holy See (Vatican City State)",
    vc =&gt; "Saint Vincent and the Grenadines",
    ve =&gt; "Venezuela",
    vg =&gt; "Virgin Islands, British",
    vi =&gt; "Virgin Islands, U.S.",
    vn =&gt; "Vietnam",
    vu =&gt; "Vanuatu",
    wf =&gt; "Wallis and Futuna",
    ws =&gt; "Samoa",
    ye =&gt; "Yemen",
    yt =&gt; "Mayotte",
    za =&gt; "South Africa",
    zm =&gt; "Zambia",
    zw =&gt; "Zimbabwe",
};

my $locale_cache;

sub parse_http_accept_language {
	my ($string) = @_;

	my @tokens = split / *, */, $string;
	
	my %retval;
	foreach my $token (@tokens) {
		my $quality = 1;
		# This RE is more forgiving than the standard.  It accepts
		# values greater than 1.0 and with more fractional digits
		# than 3.
		if ($token =~ s/ *; *q *= *([0-9]+(?:\.([0-9]+))?)$//) {
			$quality = $1;
		}
		$retval{$token} = $quality;
	}

	# RFC 2616 only allows 1-8 characters for language and country
	# but we are more forgiving.
	return grep { 
		/^[A-Za-z]+(?:-[A-Za-z]+)?$/
		} map {
			$_ = 'C' if $_ eq '*'; $_
		}	sort { 
			$retval{$b} &lt;=&gt; $retval{$a} 
		} keys %retval;
}

sub parse_http_accept_charset {
	my ($string) = @_;

	my @tokens = split / *, */, $string;
	
	my %retval;
	foreach my $token (@tokens) {
		my $quality = 1;
		# This RE is more forgiving than the standard.  It accepts
		# values greater than 1.0 and with more fractional digits
		# than 3.
		if ($token =~ s/ *; *q *= *([0-9]+(?:\.([0-9]+))?)$//) {
			$quality = $1;
		}
		$retval{$token} = $quality;
	}

	return grep { 
		# This is really allowed in character set names ...
		/^[-!\#\$\%\&amp;\'\+\.0-9A-Z_\`a-z\|\~]+$/
		} map {
			$_ = undef if $_ eq '*'; $_
		}	sort { 
			$retval{$b} &lt;=&gt; $retval{$a} 
		} keys %retval;
}

sub set_locale {
	my ($category, $language, $country, $charset) = @_;
	
	require POSIX;

    $country = '' unless defined $country;
    $charset = '' unless defined $charset;
    
	my $set_locale;
	# Look up the cache first.
    if (my $retval = $locale_cache-&gt;{$language}-&gt;{$country}-&gt;{$charset}) {
    	my ($locale, $country) = @$retval;
	POSIX::setlocale ($category, $locale);
        return @$retval;
    }

	# Initialize the cache with the undefined value so that we can do
	# error returns without setting it.
	$locale_cache-&gt;{$language}-&gt;{$country}-&gt;{$charset} = undef;

	my $windows = ($^O !~ /darwin/i &amp;&amp; $^O =~ /win/i) ? 1 : 0;
	if ($windows) {
		return &amp;__set_locale_windows;
	}
	
	my $set_language;
	my $set_country;

	# First we try to only use the language.
	my @languages = ($language);
	my @lc_languages = map { lc $_ } @languages;
	my @uc_languages = map { uc $_ } @languages;
	my %seen = ();

	foreach my $language (@languages, @lc_languages, @uc_languages) {
		next if $seen{$language}++;
		warn "Trying lingua only setlocale '$language'.\n" if DEBUG;
		my $result = POSIX::setlocale ($category, $language);
		if ($result) {
			$set_locale = $set_language = $result if $result;
			last;
		}
	}

	# Now try it with the country appended.
	my @countries = length $country ? ($country) : ();
	my @uc_countries = map { uc $_ } @countries;
	my @lc_countries = map { uc $_ } @countries;
	push @countries, @uc_countries, @lc_countries;
 
 LINGUA: foreach my $language (@languages, @lc_languages, @uc_languages) {
		my $count = 0;
		my @guessed_countries = (LANG2COUNTRY-&gt;{lc $language},
								 lc LANG2COUNTRY-&gt;{lc $language},
								 uc LANG2COUNTRY-&gt;{lc $language});
		foreach my $c (@countries, @guessed_countries) {
			++$count;
			next unless defined $c &amp;&amp; length $c;
			my $try = $language . '_' . $c;
			next if $seen{$try}++;
			warn "Trying setlocale '$try'.\n" if DEBUG;
			my $result = POSIX::setlocale ($category, $try);
			if ($result) {
				$set_locale = $result;
				if ($count &gt;= @countries) {
					$set_country = $c; 
				} else {
					$set_country = $country;
				}

				last LINGUA;
			}
		}
	}
	
	unless (length $charset) {
		return unless defined $set_locale &amp;&amp; length $set_locale;
		
		$locale_cache-&gt;{$language}-&gt;{$country}-&gt;{$charset} = 
			[$set_locale, $set_country];
		return wantarray ? ($set_locale, $set_country) : $set_locale;
	}
	
	my @charsets = ($charset);
	my $cleaned = $charset;
	push @charsets, $cleaned if $cleaned =~ s/-//g;
	my @lc_charsets = map { lc $charset } @charsets;
	my @uc_charsets = map { uc $charset } @charsets;
	push @charsets, @lc_charsets, @uc_charsets;
	
	%seen = ();
 LINGUA2: foreach my $language (@languages, 
								@lc_languages, @uc_languages) {
	     my @guessed_countries = (LANG2COUNTRY-&gt;{lc $language},
								 lc LANG2COUNTRY-&gt;{lc $language},
								 uc LANG2COUNTRY-&gt;{lc $language});
	     my $count = 0;
		 foreach my $c (@countries, @guessed_countries) {
			 ++$count;
			 $c = '' unless defined $c &amp;&amp; length $c;
			 my $country_try = $language;
			 $country_try .= (length $c) ? "_$c" : '';
			 
			 foreach my $ch (@charsets, @lc_charsets, @uc_charsets) {
				 my $try = $country_try . '.' . $ch;
				 next if $seen{$try}++;
				 warn "Trying setlocale '$try'.\n" if DEBUG;
				 
				 my $result = POSIX::setlocale ($category, $try);
				 if ($result) {
					 $set_locale = $result;
					 if ($count &gt;= @countries) {
						 $set_country = $c; 
					 } else {
						 $set_country = $country;
					 }
					 
					 last LINGUA2;
				 }
			 }
		 } 
	 }

	return unless defined $set_locale &amp;&amp; length $set_locale;

	$locale_cache-&gt;{$language}-&gt;{$country}-&gt;{$charset} = 
		[$set_locale, $set_country];

	return wantarray ? ($set_locale, $set_country) : $set_locale;
}

sub __set_locale_windows {
	my ($category, $language, $country, $charset) = @_;

    my $set_locale;

    $country = '' unless defined $country;
    $charset = '' unless defined $charset;
    	
	# First we try to only use the language.
	my $long_language = WIN32LANGUAGE-&gt;{lc $language};
	my @languages = ($long_language, $language);
	my %seen = ();
	foreach my $language (@languages) {
		next if $seen{$language}++;
		warn "Trying lingua only setlocale '$language'.\n" if DEBUG;
		my $result = POSIX::setlocale ($category, $language);
		if ($result) {
			$set_locale = $result if $result;
			last;
		}
	}
	
	# Now try it with the country appended.
	my $set_country;
	if (length $country) {
        COMBI: foreach my $language (@languages) {
            # We do not need a fallback country here, because the "system" already
            # provides the information.
	        my @short_countries = ($country);
		    my @countries = map { 
			    WIN32COUNTRY-&gt;{lc $_} 
			    } grep { length $_ } @short_countries;
		    foreach my $c (@countries) {
			    next unless defined $c &amp;&amp; length $c;
			    my $try = $language . '_' . $c;
			    next if $seen{$try}++;
			    warn "Trying setlocale '$try'.\n" if DEBUG;
			    my $result = POSIX::setlocale ($category, $try);
			    if ($result) {
				    $set_locale = $result;
					$set_country = $c;
				    last COMBI;
			    }
		    }
	    }
	}

	return unless defined $set_locale &amp;&amp; length $set_locale;

    # Apparently, there is no point in setting a charset.  Even the new
    # MS-DOS versions like 2000 or XP still have the concept of more or
    # less fixed codepages.  Switching to UTF-8 does not work.
	$locale_cache-&gt;{$language}-&gt;{$country}-&gt;{$charset} = 
		[$set_locale, $set_country];
	return wantarray ? ($set_locale, $set_country) : $set_locale;
}

sub get_locale_cache {
	$locale_cache;
}

sub set_locale_cache {
	if (ref $_[0] &amp;&amp; 'HASH' eq ref $_[0]) {
		$locale_cache = $_[0];
    } else {
		my %locale_cache = @_;
		$locale_cache = \%locale_cache;
	}
}

sub web_set_locale {
	my ($accept_language, $accept_charset, $category, $available) = @_;

	my %available;
	if ($available) {
		foreach (@$available) {
			my $locale = $_;
			$locale =~ s/[_\@\.].*//;
			$available{lc $locale} = 1;
		}
	}

	my @languages;
	if (ref $accept_language &amp;&amp; 'ARRAY' eq ref $accept_language) {
		@languages = @$accept_language;
	} else {
		@languages = parse_http_accept_language $accept_language;
	}

	if ($available) {
		my @all = @languages;
		@languages = ();
		foreach my $locale (@all) {
			my $language = lc $locale;
			$language =~ s/[_\@\.].*//;
			push @languages, $locale if $available{$language};
		}
	}

	my @charsets;
	if (defined $accept_charset) {
		if (ref $accept_charset &amp;&amp; 'ARRAY' eq ref $accept_charset) {
			@charsets = @$accept_charset;
		} else {
			@charsets = parse_http_accept_charset $accept_charset;
		}
	}

	unless (defined $category) {
		require POSIX;
		$category = POSIX::LC_ALL();
	}

	my ($set_locale, $set_language, $set_country, $set_charset);
	foreach my $lang (@languages) {
		my ($language, $country) = split /-/, $lang, 2;

		my ($locale, $country_used) = 
			set_locale ($category, $language, $country, $charsets[0]);
		
		if (defined $locale) {
			# If a country was specified, we have to check whether it
			# was actually selected.
			if (defined $country) {
				if (!defined $country
					|| ($country ne $country_used)) {
					$set_language = $language;
					$set_locale = $locale;
					$set_country = $country_used;
					$set_charset = $charsets[0];
				}
			}

			if (wantarray) {
				return $locale, $lang, $country_used, $charsets[0];
			} else {
				return $locale;
			}
		}
	}
	
	if (defined $set_locale) {
		if (wantarray) {
			return $set_locale, $set_language, $set_country, $set_charset;
		} else {
			return $set_locale;
		}
	}

	return;
}

1;

__END__

=head1 NAME

Locale::Util - Portable l10n and i10n functions

=head1 SYNOPSIS

  use Locale::Util;

  my @linguas = parse_http_accept_language $ENV{HTTP_ACCEPT_LANGUAGE};

  my @charsets = parse_http_accept_charset $ENV{HTTP_ACCEPT_CHARSET};

  # Trie to set the locale to Brasilian Portuguese in UTF-8.
  my $set_locale = set_locale LC_ALL, 'pt', 'BR', 'utf-8';

  set_locale_cache $last_cache;
  
  my $cache = get_locale_cache;

  web_set_locale ($ENV{HTTP_ACCEPT_LANGUAGE}, $ENV_ACCEPT_CHARSET);

  web_set_locale (['fr-BE', 'fr', 'it'], ['cp1252', 'utf-8']);

=head1 DESCRIPTION

This module provides portable functions dealing with localization
(l10n) and internationalization(i10n).  It doesn't export anything
by default, you have to specify each function you need in the import
list, or use the fully qualified name.

The functions here have a focus on web development, although they 
are general enough to have them in the Locale:: namespace.

This module is considered alpha code.  The interface is not stable.
Please contact the author if you want to use it in production code.

This module was introduced in libintl-perl 1.17.

=head1 FUNCTIONS

=over 4

=item B&lt;parse_http_accept_language STRING&gt;

Parses a string as passed in the HTTP header "Accept-Language".
It returns a list of tokens sorted by the quality value, see RFC 2616 
for details.

Example:

  parse_http_accept ("fr-fr, fr; q=0.7, de; q=0.3");

This means: Give me French for France with a quality value of 1.0
(the maximum).  Otherwise I will take any other French version 
(quality 0.7), German has a quality of 0.3 for me.

The function will return a list of tokens in the order of their quality
values, in this case "fr-fr", "fr" and "de".

The function is more forgiving than RFC 2616.  It accepts quality
values greater than 1.0 and with more than 3 decimal places.  It
also accepts languages and country names with more than 8 characters.
The language "*" is translated into "C".

=item B&lt;parse_http_accept_charset STRING&gt;

Parses a string as passed in the HTTP header "Accept-Charset".
It returns a list of tokens sorted by the quality value, see RFC 2616 
for details.

The special character set "*" (means all character sets) will be
translated to the undefined value.

=item B&lt;set_locale CATEGORY, LANGUAGE[, COUNTRY, CHARSET]&gt;

Tries to set the user locale by means of POSIX::setlocale().  The latter
function has the disadvantage, that its second argument (the locale
description string) is completely non-standard and system-dependent.
This function tries its best at guessing the system's notion of a locale
dientifier, with the arguments supplied:

=over 8

=item B&lt;CATEGORY&gt;

An integer argument for a valid locale category.  These are the
LC_* constants (LC_ALL, LC_CTIME, LC_COLLATE, ...) defined in both
Locale::Messages(3pm) and POSIX(3pm).

=item B&lt;LANGUAGE&gt;

A 2-letter language identifier as per ISO 639.  Case doesn't matter,
but an unchanged version (ie. not lower-cased) of the language you
provided will always be tried to.

=item B&lt;COUNTRY&gt;

A 2-letter language identifier as per ISO 639.  Case doesn't matter,
but an unchanged version (ie. not lower-cased) of the language you
provided will always be tried to.

This parameter is optional.  If it is not defined, the function will
try to guess an appropriate country, otherwise leave it to the 
operating system.

=item B&lt;CHARSET&gt;

A valid charset name.  Valid means valid!  The charset "utf8" is not
valid (it is "utf-8").  Charset names that are accepted by the
guessing algorithms in Encode(3pm) are also not necessarily valid.

If the parameter is undefined, it is ignored.  It is always ignored
under Windows.

=back

The function tries to approach the desired locale in loops, refining
it on every success.  It will first try to set the language (for 
any country), then try to select the correct language, and finally
try to select the correct charset.

The return value is false in case of failure, or the return value
of the underlying POSIX::setlocale() call in case of success.

In array context, the function returns the country name 
that was passed in the successful
call to POSIX::setlocale().  If this string is equal to the country
name you passed as an argument, you can be reasonably sure that
the settings for this country are really used.  If it is not
equal, the function has taken a guess at the country (it has a list
of "default" countries for each language).  It seems that under
Windows, POSIX::setlocale() also succeeds, if you pass a country
name that is actually not supported.  Therefore, the information
is not completely reliable.

Please note that this function is intended for server processes 
(especially web applications) that need to switch in a portable
way to a certain locale.  It is B&lt;not&gt; the recommended way to set 
the program locale for a regular application.  In a regular application
you should do the following:

    use POSIX qw (setlocale LC_ALL);
    setlocale LC_ALL, '';

The empty string as the second argument means, that the system
should switch to the user's default locale.

=item B&lt;get_locale_cache&gt;

The function set_locale() is potentially expansive, especially when
it fails, because it can try a lot of different combinations, and 
the system may have to load a lot of locale definitions from its
internal database.

In order to speed up things, results are internally cached in a 
hash, keys are the languages, subkeys countries, subsubkeys the
charsets.  You can get a reference to this hash with get_locale_cache().

The function cannot fail.

=item B&lt;set_locale_cache HASH&gt;

Sets the internal cache.  You can either pass a hash or a hash reference.
The function will use this as its cache, discarding its old cache.
This allows you to keep the hash persistent.

The function cannot fail.

=item B&lt;web_set_locale (ACCEPT_LANGUAGE, ACCEPT_CHARSET, CATEGORY,
                        AVAILABLE)&gt;

Try to change the locale to the settings described by ACCEPT_LANGUAGE
and ACCEPT_CHARSET.  For each argument you can either pass a string
as in the corresponding http header, or a reference to an array
of language resp. charset identifiers.

Currently only the first charset passed is used as an argument.
You are strongly encouraged to pass a hard-coded value here, so
that you have control about your output.

The argument B&lt;CATEGORY&gt; specifies the category (one of the LC_*
constants as defined in Locale::Messages(3pm) or in POSIX(3pm)).
The category defaults to LC_ALL.

You can pass an optional reference to a list of locales in 
XPG4 format that are available in your application.  This is
useful if you know which languages are supported by your application.
In fact, only the language part of the values in the list are
considered (for example for "en_US", only "en" is used).  The
country or other parts are ignored.

The function returns the return value of the underlying set_locale()
call, or false on failure.

The function returns false on failure.  On success it returns the 
return value of the underlying set_locale() call.  This value can
be used directly in subsequent calls to POSIX::setlocale().  In 
array context, it additionally returns the identifiers for the language, 
the country, and the charset actually used.

=back

=head1 BUGS

The function set_locale() probably fails to guess the correct locale
identifier on a lot of systems.  If you have found such a case,
please submit it as a bug report.

The bug tracking system for this packags is at
http://rt.cpan.org/NoAuth/Bugs.html?libintl-perl

Please note that this module is considered alpha code, and the interface
is not stable.  Please contact the author, if you want to use it in
production code.

=head1 AUTHOR

Copyright (C) 2002-2013, Guido Flohr E&lt;lt&gt;guido@imperia.netE&lt;gt&gt;, all
rights reserved.  See the source code for details.

This software is contributed to the Perl community by Imperia 
(L&lt;http://www.imperia.net/&gt;).

=head1 SEE ALSO

POSIX(3pm), perl(1)

=cut
Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
perl-label-offset: -4
cperl-indent-level: 4
cperl-continued-statement-offset: 2
tab-width: 4
End:
=cut
</pre></body></html>