Repository navigation
Home
Enbrea.Bcp47 is a .NET implementation of IETF BCP 47 for parsing, validating, inspecting, normalizing, building, and matching language tags and language ranges.
The repository contains two packages:
-
Enbrea.Bcp47provides registry-independent BCP 47 support based on RFC 5646 and RFC 4647 -
Enbrea.Bcp47.Ianaadds validation and metadata based on a bundled snapshot of the IANA Language Subtag Registry
Both packages support .NET 10, .NET 9 and .NET 8.
Install the core package from NuGet using the .NET CLI:
dotnet add package Enbrea.Bcp47Or using the Package Manager Console in Visual Studio:
Install-Package Enbrea.Bcp47Or add the package reference directly to your project file:
<ItemGroup>
<PackageReference Include="Enbrea.Bcp47" Version="x.y.z" />
</ItemGroup>For registry-aware validation and registry metadata, install the IANA package instead:
dotnet add package Enbrea.Bcp47.IanaEnbrea.Bcp47.Iana depends on Enbrea.Bcp47, so installing it automatically includes the core package.
Types from the core package are located in the Enbrea.Bcp47 namespace:
using Enbrea.Bcp47;Parse a language tag:
var tag = LanguageTag.Parse("zh-Hans-CN");
Console.WriteLine(tag.Language); // zh
Console.WriteLine(tag.Script); // Hans
Console.WriteLine(tag.Region); // CN
Console.WriteLine(tag); // zh-Hans-CNValue preserves the original input, while ToString() returns the normalized representation:
var tag = LanguageTag.Parse("ZH-hans-cn");
Console.WriteLine(tag.Value); // ZH-hans-cn
Console.WriteLine(tag.ToString()); // zh-Hans-CNA language tag can also be constructed using the fluent builder:
var tag = new LanguageTagBuilder()
.Language("de")
.Region("DE")
.Variant("1901")
.Build();
Console.WriteLine(tag); // de-DE-1901Language ranges are parsed separately because RFC 4647 ranges are not the same thing as language tags:
var range = LanguageRangeParser.Parse("de-*-DE");They can be used for filtering and lookup:
var tags = new[]
{
LanguageTag.Parse("de-DE"),
LanguageTag.Parse("de-Latn-DE"),
LanguageTag.Parse("en-US")
};
var matches = range.ExtendedFilter(tags);For IANA-aware validation, add the companion namespace:
using Enbrea.Bcp47.Iana;
var tag = LanguageTag.Parse("de-DE");
if (tag.IsValidAgainstIanaRegistry())
{
// All applicable registry-controlled subtags are registered or reserved.
}- BCP 47 > Tags for Identifying Languages
- RFC 5646 > Tags for Identifying Languages
- RFC 4647 > Matching of Language Tags
- IANA Language Subtag Registry