language
Typed BCP47 language tags with names, plural metadata, and conversion helpers to common libraries.
Introduction
Language tags are defined in BCP47. A friendly overview is available in the W3C article “Language tags in HTML and XML.” These tags are commonly used in HTML and in the Content-Language and Accept-Language HTTP headers.
Feature Highlights
- Typed coverage of BCP47 language tags through a single
Languageenum. - Conversion helpers:
tag,name, andlocal_namegive tags, English names, and native names, withFromStr/TryFromfor parsing. Language::all()provides a compile-time array for iterating over every language without allocation.- Optional
serdefeature for serializing and deserializing language values. - Code is generated directly from the translation.io “languages with plural cases” page;
cargo buildenforces validity and thelanguagebinary downloads fresh data when regenerating. - Optional ICU4X interop (
icu_locale_corefeature) for converting to/fromLocaleandLanguageIdentifier. - Optional whatlang interop (
whatlangfeature) for converting to/fromwhatlang::Langwith clear error reporting. - Optional SQLx interop (
sqlx-postgres/sqlx-mysql/sqlx-sqlite) forType/Encode/Decodesupport using textual tags.
Usage
// crates.io
use Language;
let en = En;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
More examples
Parse user input safely (any invalid tag becomes a typed error):
// crates.io
use Language;
let parsed: = "zh-Hant".parse;
assert!;
let bad = "zz-INVALID".;
assert!;
Iterate over every language without allocation:
// crates.io
use Language;
let tags: = all.iter.map.collect;
assert!;
serde (enable the serde feature):
// crates.io
use Language;
use ;
let json = r#"{"lang":"es-MX"}"#;
let payload: Payload = from_str?;
assert_eq!;
ICU4X interop (enable icu_locale_core):
// crates.io
use Locale;
use Language;
let locale: Locale = "pt-BR".parse?;
let language = try_from?;
assert_eq!;
whatlang interop (enable whatlang):
// crates.io
use Language;
use Lang;
let lang = try_from?;
assert_eq!;
SQLx (enable one of the sqlx-* features):
// crates.io
use Language;
use Json;
// Language stores as a text tag; works with Postgres/MySQL/SQLite feature flags.
let stored = Ja;
let row = query!
.fetch_one
.await?;
assert_eq!;
Support Me
If you find this project helpful and would like to support its development, you can buy me a coffee!
Your support is greatly appreciated and motivates me to keep improving this project.
- Fiat
- Crypto
- Bitcoin
bc1pedlrf67ss52md29qqkzr2avma6ghyrt4jx9ecp9457qsl75x247sqcp43c
- Ethereum
0x3e25247CfF03F99a7D83b28F207112234feE73a6
- Polkadot
156HGo9setPcU2qhFMVWLkcmtCEGySLwNqa3DaEiYSWtte4Y
- Bitcoin
Thank you for your support!
Appreciation
We would like to extend our heartfelt gratitude to the following projects and contributors:
- The Rust community for their continuous support and development of the Rust ecosystem.
- translation.io for publishing the languages-with-plural-cases reference we build from.
Additional Acknowledgements
- Autonym data is downloaded from https://translation.io/docs/languages_with_plural_cases whenever you regenerate code with
cargo run --features codegen --bin language.
License
Licensed under GPL-3.0.