LangTags (lang_tags v0.2.0)

Copy Markdown View Source

The Language Tag according to the BCP47

Language tags are used to help identify languages, whether spoken, written, signed, or otherwise signaled, for the purpose of communication. This includes constructed and artificial languages but excludes languages not intended primarily for human communication, such as programming languages.

For more information, see BCP47

Summary

Functions

Shortcut for LangTags.Tag.valid?/1. Returns true if the tag is valid, false otherwise.

Returns the file date for the underlying data, as a string.

The opposite of subtags/1. Returns a list of codes that are not registered subtags, otherwise returns an empty array.

Convenience method to get a single language type subtag. Returns a subtag map or nil.

Returns a list of subtag maps representing all the language type subtags belonging to the given macrolanguage type subtag.

As language/1, but with region type subtags.

As language/1, but with script type subtags.

Search for tags and subtags by description.

Look up one or more subtags. Returns a list of subtag maps. Returns an empty list if all of the subtags are non-existent.

Get a subtag by type. Returns the subtag matching type as a subtag map otherwise returns nil.

Look up for one or more types for the given string.

Functions

check(tag)

@spec check(map() | String.t()) :: boolean()

Shortcut for LangTags.Tag.valid?/1. Returns true if the tag is valid, false otherwise.

For meaningful error output see errors/1.

date()

@spec date() :: String.t()

Returns the file date for the underlying data, as a string.

Examples

iex> LangTags.date() |> String.match?(~r/\d{4}-\d{2}-\d{2}/)
true

filter(subtag)

@spec filter(String.t() | [String.t()]) :: [String.t()]

The opposite of subtags/1. Returns a list of codes that are not registered subtags, otherwise returns an empty array.

Examples

iex> LangTags.filter(["en", "Aargh"])
["Aargh"]

language(subtag)

@spec language(String.t()) :: map() | nil

Convenience method to get a single language type subtag. Returns a subtag map or nil.

Examples

iex> LangTags.language("en")
%{"Record" => %{"Added" => "2005-10-16", "Description" => ["English"],
    "Subtag" => "en", "Suppress-Script" => "Latn", "Type" => "language"},
  "Subtag" => "en"}
iex> LangTags.language("us")
nil

languages(macrolanguage)

@spec languages(String.t()) :: [map()] | Exception.t()

Returns a list of subtag maps representing all the language type subtags belonging to the given macrolanguage type subtag.

Throws an error if macrolanguage is not a macrolanguage.

Examples

iex> LangTags.languages("zh") |> Enum.count()
38
iex> LangTags.languages("en")
** (ArgumentError) 'en' is not a valid macrolanguage.

region(subtag)

@spec region(String.t()) :: map() | nil

As language/1, but with region type subtags.

Examples

iex> LangTags.region("mt")
%{"Record" => %{"Added" => "2005-10-16", "Description" => ["Malta"],
    "Subtag" => "mt", "Type" => "region"}, "Subtag" => "mt"}
iex> LangTags.region("en")
nil

script(subtag)

@spec script(String.t()) :: map() | nil

As language/1, but with script type subtags.

Examples

iex> LangTags.script("aghb")
%{"Record" => %{"Added" => "2012-11-01",
    "Description" => ["Caucasian Albanian"], "Subtag" => "aghb",
    "Type" => "script"}, "Subtag" => "aghb"}
iex> LangTags.script("en")
nil

search(description, all \\ false)

@spec search(String.t() | Regex.t(), boolean()) :: [map()]

Search for tags and subtags by description.

Supports either a Regex or a string for description. Returns a list of subtag and tag maps, or an empty list if no results were found.

Search is case-insensitive when description is a string, and matches on any part of a description. Entries whose description matches the query exactly are returned first; the rest follow in registry order.

Note that tag maps in the results represent grandfathered or redundant tags. These are excluded by default. Set the all parameter to true to include them.

Examples

iex> LangTags.search("Maltese") |> Enum.map(&LangTags.SubTag.format/1)
["mt", "mdl", "mdl"]

iex> LangTags.search(~r/^Klingon$/) |> Enum.map(&LangTags.SubTag.format/1)
["tlh"]

iex> LangTags.search("Gibberish")
[]

subtags(key)

@spec subtags(String.t() | [String.t()]) :: [map()]

Look up one or more subtags. Returns a list of subtag maps. Returns an empty list if all of the subtags are non-existent.

Examples

Calling LangTags.subtags("mt") will return an array with 2 subtag maps: one for Malta (the 'region' type subtag) and one for Maltese (the 'language' type subtag).

iex> for subtag <- LangTags.subtags("mt"), do: subtag["Record"]["Description"]
[["Maltese"], ["Malta"]]
iex> LangTags.subtags(["mt", "ca"]) |> Enum.count()
4
iex> LangTags.subtags("bumblebee")
[]

To get or check a single subtag by type use language/1, region/1 or type/2.

tags(tag)

@spec tags(String.t()) :: map()

Shortcut for LangTags.Tag.new/1

Examples

iex> LangTags.tags("art-lojban") == LangTags.Tag.new("art-lojban")
true

type(subtag, type)

@spec type(String.t(), String.t()) :: map() | nil

Get a subtag by type. Returns the subtag matching type as a subtag map otherwise returns nil.

A type consists of one of the following strings: language, extlang, script, region or variant. To get a grandfathered or redundant type tag use tags/1.

Examples

iex> LangTags.type("zh", "language") == LangTags.language("zh")
true
iex> LangTags.type("zh", "script")
nil

types(subtag, all \\ false)

Look up for one or more types for the given string.

Returns an empty list if the does not have any type or if the available types are: grandfathered or redundant.

By default, the types grandfathered or redundant are excluded from the result. Set the all parameter to true to include them.

Examples

iex> LangTags.types("en")
["language"]
iex> LangTags.types("xml")
["extlang", "language"]
iex> LangTags.types("art-lojban")
[]
iex> LangTags.types("art-lojban", true)
["grandfathered"]