{
    "mode": "perldoc",
    "parameter": "I18N::LangTags",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/I18N%3A%3ALangTags/json",
    "generated": "2026-06-13T21:28:53Z",
    "synopsis": "use I18N::LangTags();\n...or specify whichever of those functions you want to import, like so:\nuse I18N::LangTags qw(implicatesupers similaritylanguagetag);\nAll the exportable functions are listed below -- you're free to import only some, or none at\nall. By default, none are imported. If you say:\nuse I18N::LangTags qw(:ALL)\n...then all are exported. (This saves you from having to use something less obvious like \"use\nI18N::LangTags qw(/./)\".)\nIf you don't import any of these functions, assume a &I18N::LangTags:: in front of all the\nfunction names in the following examples.",
    "sections": {
        "NAME": {
            "content": "I18N::LangTags - functions for dealing with RFC3066-style language tags\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use I18N::LangTags();\n\n...or specify whichever of those functions you want to import, like so:\n\nuse I18N::LangTags qw(implicatesupers similaritylanguagetag);\n\nAll the exportable functions are listed below -- you're free to import only some, or none at\nall. By default, none are imported. If you say:\n\nuse I18N::LangTags qw(:ALL)\n\n...then all are exported. (This saves you from having to use something less obvious like \"use\nI18N::LangTags qw(/./)\".)\n\nIf you don't import any of these functions, assume a &I18N::LangTags:: in front of all the\nfunction names in the following examples.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Language tags are a formalism, described in RFC 3066 (obsoleting 1766), for declaring what\nlanguage form (language and possibly dialect) a given chunk of information is in.\n\nThis library provides functions for common tasks involving language tags as they are needed in a\nvariety of protocols and applications.\n\nPlease see the \"See Also\" references for a thorough explanation of how to correctly use language\ntags.\n\n*   the function islanguagetag($lang1)\n\nReturns true iff $lang1 is a formally valid language tag.\n\nislanguagetag(\"fr\")            is TRUE\nislanguagetag(\"x-jicarilla\")   is FALSE\n(Subtags can be 8 chars long at most -- 'jicarilla' is 9)\n\nislanguagetag(\"sgn-US\")    is TRUE\n(That's American Sign Language)\n\nislanguagetag(\"i-Klikitat\")    is TRUE\n(True without regard to the fact noone has actually\nregistered Klikitat -- it's a formally valid tag)\n\nislanguagetag(\"fr-patois\")     is TRUE\n(Formally valid -- altho descriptively weak!)\n\nislanguagetag(\"Spanish\")       is FALSE\nislanguagetag(\"french-patois\") is FALSE\n(No good -- first subtag has to match\n/^([xXiI]|[a-zA-Z]{2,3})$/ -- see RFC3066)\n\nislanguagetag(\"x-borg-prot2532\") is TRUE\n(Yes, subtags can contain digits, as of RFC3066)\n\n*   the function extractlanguagetags($whatever)\n\nReturns a list of whatever looks like formally valid language tags in $whatever. Not very\nsmart, so don't get too creative with what you want to feed it.\n\nextractlanguagetags(\"fr, fr-ca, i-mingo\")\nreturns:   ('fr', 'fr-ca', 'i-mingo')\n\nextractlanguagetags(\"It's like this: I'm in fr -- French!\")\nreturns:   ('It', 'in', 'fr')\n(So don't just feed it any old thing.)\n\nThe output is untainted. If you don't know what tainting is, don't worry about it.\n\n*   the function samelanguagetag($lang1, $lang2)\n\nReturns true iff $lang1 and $lang2 are acceptable variant tags representing the same\nlanguage-form.\n\nsamelanguagetag('x-kadara', 'i-kadara')  is TRUE\n(The x/i- alternation doesn't matter)\nsamelanguagetag('X-KADARA', 'i-kadara')  is TRUE\n(...and neither does case)\nsamelanguagetag('en',       'en-US')     is FALSE\n(all-English is not the SAME as US English)\nsamelanguagetag('x-kadara', 'x-kadar')   is FALSE\n(these are totally unrelated tags)\nsamelanguagetag('no-bok',    'nb')       is TRUE\n(no-bok is a legacy tag for nb (Norwegian Bokmal))\n\n\"samelanguagetag\" works by just seeing whether \"encodelanguagetag($lang1)\" is the same\nas \"encodelanguagetag($lang2)\".\n\n(Yes, I know this function is named a bit oddly. Call it historic reasons.)\n\n*   the function similaritylanguagetag($lang1, $lang2)\n\nReturns an integer representing the degree of similarity between tags $lang1 and $lang2 (the\norder of which does not matter), where similarity is the number of common elements on the\nleft, without regard to case and to x/i- alternation.\n\nsimilaritylanguagetag('fr', 'fr-ca')           is 1\n(one element in common)\nsimilaritylanguagetag('fr-ca', 'fr-FR')        is 1\n(one element in common)\n\nsimilaritylanguagetag('fr-CA-joual',\n'fr-CA-PEI')             is 2\nsimilaritylanguagetag('fr-CA-joual', 'fr-CA')  is 2\n(two elements in common)\n\nsimilaritylanguagetag('x-kadara', 'i-kadara')  is 1\n(x/i- doesn't matter)\n\nsimilaritylanguagetag('en',       'x-kadar')   is 0\nsimilaritylanguagetag('x-kadara', 'x-kadar')   is 0\n(unrelated tags -- no similarity)\n\nsimilaritylanguagetag('i-cree-syllabic',\n'i-cherokee-syllabic')   is 0\n(no B<leftmost> elements in common!)\n\n*   the function isdialectof($lang1, $lang2)\n\nReturns true iff language tag $lang1 represents a subform of language tag $lang2.\n\nGet the order right! It doesn't work the other way around!\n\nisdialectof('en-US', 'en')            is TRUE\n(American English IS a dialect of all-English)\n\nisdialectof('fr-CA-joual', 'fr-CA')   is TRUE\nisdialectof('fr-CA-joual', 'fr')      is TRUE\n(Joual is a dialect of (a dialect of) French)\n\nisdialectof('en', 'en-US')            is FALSE\n(all-English is a NOT dialect of American English)\n\nisdialectof('fr', 'en-CA')            is FALSE\n\nisdialectof('en',    'en'   )         is TRUE\nisdialectof('en-US', 'en-US')         is TRUE\n(B<Note:> these are degenerate cases)\n\nisdialectof('i-mingo-tom', 'x-Mingo') is TRUE\n(the x/i thing doesn't matter, nor does case)\n\nisdialectof('nn', 'no')               is TRUE\n(because 'nn' (New Norse) is aliased to 'no-nyn',\nas a special legacy case, and 'no-nyn' is a\nsubform of 'no' (Norwegian))\n\n*   the function superlanguages($lang1)\n\nReturns a list of language tags that are superordinate tags to $lang1 -- it gets this by\nremoving subtags from the end of $lang1 until nothing (or just \"i\" or \"x\") is left.\n\nsuperlanguages(\"fr-CA-joual\")  is  (\"fr-CA\", \"fr\")\n\nsuperlanguages(\"en-AU\")  is  (\"en\")\n\nsuperlanguages(\"en\")  is  empty-list, ()\n\nsuperlanguages(\"i-cherokee\")  is  empty-list, ()\n...not (\"i\"), which would be illegal as well as pointless.\n\nIf $lang1 is not a valid language tag, returns empty-list in a list context, undef in a\nscalar context.\n\nA notable and rather unavoidable problem with this method: \"x-mingo-tom\" has an \"x\" because\nthe whole tag isn't an IANA-registered tag -- but superlanguages('x-mingo-tom') is\n('x-mingo') -- which isn't really right, since 'i-mingo' is registered. But this module has\nno way of knowing that. (But note that samelanguagetag('x-mingo', 'i-mingo') is TRUE.)\n\nMore importantly, you assume *at your peril* that superordinates of $lang1 are mutually\nintelligible with $lang1. Consider this carefully.\n\n*   the function locale2languagetag($localeidentifier)\n\nThis takes a locale name (like \"en\", \"enUS\", or \"enUS.ISO8859-1\") and maps it to a\nlanguage tag. If it's not mappable (as with, notably, \"C\" and \"POSIX\"), this returns\nempty-list in a list context, or undef in a scalar context.\n\nlocale2languagetag(\"en\") is \"en\"\n\nlocale2languagetag(\"enUS\") is \"en-US\"\n\nlocale2languagetag(\"enUS.ISO8859-1\") is \"en-US\"\n\nlocale2languagetag(\"C\") is undef or ()\n\nlocale2languagetag(\"POSIX\") is undef or ()\n\nlocale2languagetag(\"POSIX\") is undef or ()\n\nI'm not totally sure that locale names map satisfactorily to language tags. Think REAL hard\nabout how you use this. YOU HAVE BEEN WARNED.\n\nThe output is untainted. If you don't know what tainting is, don't worry about it.\n\n*   the function encodelanguagetag($lang1)\n\nThis function, if given a language tag, returns an encoding of it such that:\n\n* tags representing different languages never get the same encoding.\n\n* tags representing the same language always get the same encoding.\n\n* an encoding of a formally valid language tag always is a string value that is defined, has\nlength, and is true if considered as a boolean.\n\nNote that the encoding itself is not a formally valid language tag. Note also that you\ncannot, currently, go from an encoding back to a language tag that it's an encoding of.\n\nNote also that you must consider the encoded value as atomic; i.e., you should not consider\nit as anything but an opaque, unanalysable string value. (The internals of the encoding\nmethod may change in future versions, as the language tagging standard changes over time.)\n\n\"encodelanguagetag\" returns undef if given anything other than a formally valid language\ntag.\n\nThe reason \"encodelanguagetag\" exists is because different language tags may represent the\nsame language; this is normally treatable with \"samelanguagetag\", but consider this\nsituation:\n\nYou have a data file that expresses greetings in different languages. Its format is\n\"[language tag]=[how to say 'Hello']\", like:\n\nen-US=Hiho\nfr=Bonjour\ni-mingo=Hau'\n\nAnd suppose you write a program that reads that file and then runs as a daemon, answering\nclient requests that specify a language tag and then expect the string that says how to\ngreet in that language. So an interaction looks like:\n\ngreeting-client asks:    fr\ngreeting-server answers: Bonjour\n\nSo far so good. But suppose the way you're implementing this is:\n\nmy %greetings;\ndie unless open(IN, \"<\", \"in.dat\");\nwhile(<IN>) {\nchomp;\nnext unless /^([^=]+)=(.+)/s;\nmy($lang, $expr) = ($1, $2);\n$greetings{$lang} = $expr;\n}\nclose(IN);\n\nat which point %greetings has the contents:\n\n\"en-US\"   => \"Hiho\"\n\"fr\"      => \"Bonjour\"\n\"i-mingo\" => \"Hau'\"\n\nAnd suppose then that you answer client requests for language $wanted by just looking up\n$greetings{$wanted}.\n\nIf the client asks for \"fr\", that will look up successfully in %greetings, to the value\n\"Bonjour\". And if the client asks for \"i-mingo\", that will look up successfully in\n%greetings, to the value \"Hau'\".\n\nBut if the client asks for \"i-Mingo\" or \"x-mingo\", or \"Fr\", then the lookup in %greetings\nfails. That's the Wrong Thing.\n\nYou could instead do lookups on $wanted with:\n\nuse I18N::LangTags qw(samelanguagetag);\nmy $response = '';\nforeach my $l2 (keys %greetings) {\nif(samelanguagetag($wanted, $l2)) {\n$response = $greetings{$l2};\nlast;\n}\n}\n\nBut that's rather inefficient. A better way to do it is to start your program with:\n\nuse I18N::LangTags qw(encodelanguagetag);\nmy %greetings;\ndie unless open(IN, \"<\", \"in.dat\");\nwhile(<IN>) {\nchomp;\nnext unless /^([^=]+)=(.+)/s;\nmy($lang, $expr) = ($1, $2);\n$greetings{\nencodelanguagetag($lang)\n} = $expr;\n}\nclose(IN);\n\nand then just answer client requests for language $wanted by just looking up\n\n$greetings{encodelanguagetag($wanted)}\n\nAnd that does the Right Thing.\n\n*   the function alternatelanguagetags($lang1)\n\nThis function, if given a language tag, returns all language tags that are alternate forms\nof this language tag. (I.e., tags which refer to the same language.) This is meant to handle\nlegacy tags caused by the minor changes in language tag standards over the years; and the\nx-/i- alternation is also dealt with.\n\nNote that this function does *not* try to equate new (and never-used, and unusable) ISO639-2\nthree-letter tags to old (and still in use) ISO639-1 two-letter equivalents -- like \"ara\" ->\n\"ar\" -- because \"ara\" has *never* been in use as an Internet language tag, and RFC 3066\nstipulates that it never should be, since a shorter tag (\"ar\") exists.\n\nExamples:\n\nalternatelanguagetags('no-bok')       is ('nb')\nalternatelanguagetags('nb')           is ('no-bok')\nalternatelanguagetags('he')           is ('iw')\nalternatelanguagetags('iw')           is ('he')\nalternatelanguagetags('i-hakka')      is ('zh-hakka', 'x-hakka')\nalternatelanguagetags('zh-hakka')     is ('i-hakka', 'x-hakka')\nalternatelanguagetags('en')           is ()\nalternatelanguagetags('x-mingo-tom')  is ('i-mingo-tom')\nalternatelanguagetags('x-klikitat')   is ('i-klikitat')\nalternatelanguagetags('i-klikitat')   is ('x-klikitat')\n\nThis function returns empty-list if given anything other than a formally valid language tag.\n\n*   the function @langs = paniclanguages(@acceptlanguages)\n\nThis function takes a list of 0 or more language tags that constitute a given user's\nAccept-Language list, and returns a list of tags for *other* (non-super) languages that are\nprobably acceptable to the user, to be used *if all else fails*.\n\nFor example, if a user accepts only 'ca' (Catalan) and 'es' (Spanish), and the\ndocuments/interfaces you have available are just in German, Italian, and Chinese, then the\nuser will most likely want the Italian one (and not the Chinese or German one!), instead of\ngetting nothing. So \"paniclanguages('ca', 'es')\" returns a list containing 'it' (Italian).\n\nEnglish ('en') is *always* in the return list, but whether it's at the very end or not\ndepends on the input languages. This function works by consulting an internal table that\nstipulates what common languages are \"close\" to each other.\n\nA useful construct you might consider using is:\n\n@fallbacks = superlanguages(@acceptlanguages);\npush @fallbacks, paniclanguages(\n@acceptlanguages, @fallbacks,\n);\n\n*   the function implicatesupers( ...languages... )\n\nThis takes a list of strings (which are presumed to be language-tags; strings that aren't,\nare ignored); and after each one, this function inserts super-ordinate forms that don't\nalready appear in the list. The original list, plus these insertions, is returned.\n\nIn other words, it takes this:\n\npt-br de-DE en-US fr pt-br-janeiro\n\nand returns this:\n\npt-br pt de-DE de en-US en fr pt-br-janeiro\n\nThis function is most useful in the idiom\n\nimplicatesupers( I18N::LangTags::Detect::detect() );\n\n(See I18N::LangTags::Detect.)\n\n*   the function implicatesupersstrictly( ...languages... )\n\nThis works like \"implicatesupers\" except that the implicated forms are added to the end of\nthe return list.\n\nIn other words, implicatesupersstrictly takes a list of strings (which are presumed to be\nlanguage-tags; strings that aren't, are ignored) and after the whole given list, it inserts\nthe super-ordinate forms of all given tags, minus any tags that already appear in the input\nlist.\n\nIn other words, it takes this:\n\npt-br de-DE en-US fr pt-br-janeiro\n\nand returns this:\n\npt-br de-DE en-US fr pt-br-janeiro pt de en\n\nThe reason this function has \"strictly\" in its name is that when you're processing an\nAccept-Language list according to the RFCs, if you interpret the RFCs quite strictly, then\nyou would use implicatesupersstrictly, but for normal use (i.e., common-sense use, as far\nas I'm concerned) you'd use implicatesupers.\n",
            "subsections": []
        },
        "ABOUT LOWERCASING": {
            "content": "I've considered making all the above functions that output language tags return all those tags\nstrictly in lowercase. Having all your language tags in lowercase does make some things easier.\nBut you might as well just lowercase as you like, or call \"encodelanguagetag($lang1)\" where\nappropriate.\n",
            "subsections": []
        },
        "ABOUT UNICODE PLAINTEXT LANGUAGE TAGS": {
            "content": "In some future version of I18N::LangTags, I plan to include support for RFC2482-style language\ntags -- which are basically just normal language tags with their ASCII characters shifted into\nPlane 14.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "* I18N::LangTags::List\n\n* RFC 3066, \"<http://www.ietf.org/rfc/rfc3066.txt>\", \"Tags for the Identification of Languages\".\n(Obsoletes RFC 1766)\n\n* RFC 2277, \"<http://www.ietf.org/rfc/rfc2277.txt>\", \"IETF Policy on Character Sets and\nLanguages\".\n\n* RFC 2231, \"<http://www.ietf.org/rfc/rfc2231.txt>\", \"MIME Parameter Value and Encoded Word\nExtensions: Character Sets, Languages, and Continuations\".\n\n* RFC 2482, \"<http://www.ietf.org/rfc/rfc2482.txt>\", \"Language Tagging in Unicode Plain Text\".\n\n* Locale::Codes, in \"<http://www.perl.com/CPAN/modules/by-module/Locale/>\"\n\n* ISO 639-2, \"Codes for the representation of names of languages\", including two-letter and\nthree-letter codes, \"<http://www.loc.gov/standards/iso639-2/php/codelist.php>\"\n\n* The IANA list of registered languages (hopefully up-to-date),\n\"<http://www.iana.org/assignments/language-tags>\"\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (c) 1998+ Sean M. Burke. All rights reserved.\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nThe programs and documentation in this dist are distributed in the hope that they will be\nuseful, but without any warranty; without even the implied warranty of merchantability or\nfitness for a particular purpose.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Sean M. Burke \"sburke@cpan.org\"\n",
            "subsections": []
        }
    },
    "summary": "I18N::LangTags - functions for dealing with RFC3066-style language tags",
    "flags": [],
    "examples": [],
    "see_also": []
}