Skip to main content

is_valid_bcp47

Function is_valid_bcp47 

Source
pub fn is_valid_bcp47(s: &str) -> bool
Expand description

Return true when s is a structurally well-formed BCP 47 (RFC 5646) language tag.

This is a grammar-level check, not a registry lookup. It accepts hyphen-separated ASCII-alphanumeric subtags (each 1-8 characters) whose primary subtag is a 2-3 letter language code, or the x/i privateuse/grandfathered lead. It rejects the common mistakes: the POSIX-locale underscore form (zh_CN), empty or misplaced hyphens, non-ASCII, and out-of-range primaries such as chinese. It does not check that subtags are registered with IANA; a tool needing that guarantee validates further.