dillonkearns/elm-bcp47-language-tag/1.0.0

NOTE: the latest version is 2.0.0

elm-bcp47-language-tag Build Status

This package aims to help you build BCP47 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.

Project goals

  • Provide a way to create valid BCP47 tags with confidence that the result is well-formed and valid.
  • The resulting Elm bundle will be able to include only the data that is explicitly referenced (for example, 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.)
  • For unusual cases, there's an escape hatch (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!
  • This package is generated from a script with some data sources, so it can be relatively kept up-to-date through automation.

Why it matters

Usage

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"

Custom Tags

The following features and tag types are supported indirectly through the custom constructor that lets you supply your own custom string:

Reference