Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dillonkearns/elm-bcp47-language-tag
BCP47 language tags, ISO 639-1 language codes, and ISO 3166-1 country codes.
https://github.com/dillonkearns/elm-bcp47-language-tag
bcp47 elm
Last synced: about 2 months ago
JSON representation
BCP47 language tags, ISO 639-1 language codes, and ISO 3166-1 country codes.
- Host: GitHub
- URL: https://github.com/dillonkearns/elm-bcp47-language-tag
- Owner: dillonkearns
- License: bsd-3-clause
- Created: 2021-01-18T02:49:03.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-02T19:31:33.000Z (about 1 year ago)
- Last Synced: 2024-04-15T02:08:01.183Z (9 months ago)
- Topics: bcp47, elm
- Language: Elm
- Homepage: https://package.elm-lang.org/packages/dillonkearns/elm-bcp47-language-tag/latest/
- Size: 587 KB
- Stars: 0
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `elm-bcp47-language-tag` [![Build Status](https://github.com/dillonkearns/elm-bcp47-language-tag/workflows/CI/badge.svg)](https://github.com/dillonkearns/elm-bcp47-language-tag/actions?query=branch%3Amain)
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.## Project goals
- Provide a way to create valid BCP 47 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 { emptySubtags | region = Just Region.gb } Language.en` 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
- BCP 47 tags can be used in [the `lang` attribute in HTML elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang).
- `lang` attributes are [recommended by Lighthouse for the top-level `` tag](https://web.dev/html-has-lang/)
- `lang` attributes can also be used for individual sections of a page, like `...
...
`
- Some CSS, like `text-transform`, relies on `lang` tags to determine how to apply capitalize, uppercase, etc. to the content. See .
- Screen readers rely on `lang` attributes to determine how to pronounce the text. See .## Out of scope for this package
The current goals for this project are to help you safely _construct_ or _parse_ BCP 47 language tags, in a way that results in good dead-code elimination.
This package is not intended to turn BCP 47 language into human-readable strings (like country or region name, language name, etc.).
See [`supermario/elm-countries`](https://github.com/supermario/elm-countries/) for a package that lets you find the country name for a given ISO 3166 country.Some reasons that this use case doesn't fit into the package goals currently:
- Metadata, like country or region name, language name, etc. is more likely to change frequently. Hopefully the
narrowly focused goal of this package will allow it to have less churn since it only changes the codes (like country or region code, language code, etc.) change.
The package will not change and require an update when names or other metadata change.
- Turning a code, like a language code or country code, into a name, like a language name or a country name, requires including the data for all possible values in your
bundle.## Usage
```elm
import LanguageTag exposing (emptySubtags)
import LanguageTag.Language as Language
import LanguageTag.Region as Region
import LanguageTag.Script as Script
import LanguageTag.Variant as VariantLanguage.no
|> LanguageTag.build emptySubtags
|> LanguageTag.toString
--> "no"Language.en
|> LanguageTag.build { emptySubtags | region = Just Region.gb }
|> LanguageTag.toString
--> "en-gb"Language.zh
|> LanguageTag.build { emptySubtags | region = Just Region.tw }
|> LanguageTag.toString
--> "zh-tw"Language.hy
|> LanguageTag.build
{ emptySubtags |
region = Just Region.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 Region.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:
- [Schema extensions](https://github.com/wooorm/bcp-47#schemaextensions)
- [Private use](https://github.com/wooorm/bcp-47#schemaprivateuse)
- [Irregular tags](https://github.com/wooorm/bcp-47#schemairregular)## Reference
- Learn more about the BCP 47 spec at .