elm-bcp47-language-tag This package aims to help you build BCP 47 language tags with some type-safety. It's important to note that just because a language tag is valid and correct doesn't mean that it will be supported by browsers and tools. So building up a correct tag with this package should give you added confidence but be sure to test the results.
LanguageTag.build Language.en { emptySubtags | region = Just Country.gb } only includes the data for the English language and the Great Britain region, but doesn't cause your bundle to include data for French, Spanish, etc.)LanguageTag.custom) where you're on your own making sure you have a valid and meaningful value (much like the elm/html API). If you encounter a use case that isn't supported directly and requires this escape hatch, please open a GitHub issue to describe your use case to help me get context!lang attribute in HTML elements.lang attributes are recommended by Lighthouse for the top-level <html> taglang attributes can also be used for individual sections of a page, like <p lang="en-GB">...</p><p lang="fr-CA">...</p>text-transform, relies on lang tags to determine how to apply capitalize, uppercase, etc. to the content. See https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform.lang attributes to determine how to pronounce the text. See https://dequeuniversity.com/rules/axe/3.3/html-has-lang.The current goals for this project are to help you safely construct BCP 47 language tags, in a way that results in good dead-code elimination.
This package is not intended to help you parse BCP 47 language tags, or extract metadata or turn them into human-readable strings (like country name, language name, etc.).
See supermario/elm-countries for a package that lets you find the country name for a given ISO 3166 country.
Here are some places that BCP 47 tags show up where this package's features are likely to not handle:
Intl API in browsers uses BCP 47 language tags as locales for some functionsnavigator.language gives the current user's browser language in the BCP 47 formatBecause this package is intended to construct, not parse BCP 47 tags, this package is not likely to be useful for working with those values.
Some reasons that these use cases don't fit into the package goals currently:
import LanguageTag exposing (emptySubtags)
import LanguageTag.Language as Language
import LanguageTag.Country as Country
import LanguageTag.Script as Script
import LanguageTag.Variant as Variant
Language.no
|> LanguageTag.build emptySubtags
|> LanguageTag.toString
--> "no"
Language.en
|> LanguageTag.build { emptySubtags | region = Just Country.gb }
|> LanguageTag.toString
--> "en-gb"
Language.zh
|> LanguageTag.build { emptySubtags | region = Just Country.tw }
|> LanguageTag.toString
--> "zh-tw"
Language.hy
|> LanguageTag.build
{ emptySubtags |
region = Just Country.it
, script = Just Script.latn
, variants = [ Variant.arevela ]
}
|> LanguageTag.toString
--> "hy-latn-it-arevela"
-- Chinese, Simplified script, as used in China
Language.zh
|> LanguageTag.build { emptySubtags | region = Just Country.cn, script = Just Script.hans }
|> LanguageTag.toString
--> "zh-hans-cn"The following features and tag types are supported indirectly through the custom constructor that lets you supply your own custom string: