CodeChops.Idioma 0.11.1

dotnet add package CodeChops.Idioma --version 0.11.1
                    
NuGet\Install-Package CodeChops.Idioma -Version 0.11.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeChops.Idioma" Version="0.11.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeChops.Idioma" Version="0.11.1" />
                    
Directory.Packages.props
<PackageReference Include="CodeChops.Idioma" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeChops.Idioma --version 0.11.1
                    
#r "nuget: CodeChops.Idioma, 0.11.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeChops.Idioma@0.11.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeChops.Idioma&version=0.11.1
                    
Install as a Cake Addin
#tool nuget:?package=CodeChops.Idioma&version=0.11.1
                    
Install as a Cake Tool

Idioma

Why use Idioma?

Lightweight and fast translations for any .NET app, written in C# and checked at compile time:

  • The build catches the mistakes that resx files hide until a user sees them:
    • A mistyped key;
    • A text missing from a language;
    • A translation that never uses a value the original shows;
    • An enum member that has no translation.
  • There are no resource files, so you get full IDE support:
    • Autocomplete;
    • Go to definition;
    • Every language's wording on hover;
    • Find all references.
  • A wording can use your app's services, where a resx entry is a fixed string. A set is a class, so its constructor can take services from dependency injection (details).
  • Idioma shows most texts in less time than IStringLocalizer, and plain texts allocate nothing (details).
  • An AI coding assistant translates your texts well:
    • It sees the code that uses them so it knows the context.
    • The build names each missing translation, so the assistant knows when it's done (details).
  • The package has no dependencies, and is safe to trim and to publish with Native AOT.

At a glance

Declare your languages once, [assembly: SupportedLanguages(defaultLanguage: "en", "nl")], and write each text in one of three ways:

  • In a set per language, for texts you reuse, or enum members.
  • Inline, where you use a text once.
  • As an attribute, for the label of a type, a property or an enum member.
// A set per language
public class ShopLocalizationEn : ILocalizationSet, IShopLocalization
{
    public string Checkout => "Checkout";
}

public class ShopLocalizationNl : ILocalizationSet, IShopLocalization
{
    public string Checkout => "Afrekenen";
}

// An attribute on a type or its member
[Localized(en: "Address", nl: "Adres")]
public class Address
{
    [Localized(en: "City", nl: "Plaats")]
    public string City { get; set; } = "";
}

From the English set, the source generator writes the contract IShopLocalization, which every set implements, and the class ShopLocalization. Each text then shows in the user's language:

@* A text of a set *@
<button>
  @ShopLocalization.Checkout
</button>

@* A text of a type, written as an attribute *@
<label>
  @(LocalizableText.GetTextOfType<Address>())
</label>
@* A text of its member, written as an attribute *@
<label>
  @(LocalizableText.GetTextByMember<Address>(nameof(Address.City)))
</label>

@* An inline text, written where it shows *@
<button>
  @LocalizableText.Inline(en: "Cancel", nl: "Annuleren")
</button>

Also see the library Formosa. It generates Blazor forms from your C# models, and writes its texts with Idioma too.

Getting started

Each demo app is one file that dotnet run runs without a project. You can also run them in your browser:

Open in GitHub Codespaces

Install

dotnet add package CodeChops.Idioma

The package needs .NET 10 or later and C# 14. C# 14 is the default on .NET 10. An older LangVersion in your project is build error LOC102.

Declare your languages

Add this to any C# file of your project, after its using lines:

using CodeChops.Idioma;

[assembly: SupportedLanguages(defaultLanguage: "en", "nl")]

From then on, the build checks that every text in the project has all of these languages.

If your texts are in a library, declare your languages in the app's project too. Your users get the app's languages (details).

Write your texts

Write a text in one of three ways, and mix them as you like.

Write a set

A set (like a resx file) is a class that holds related texts in one language. Each text is a property, or a method if it takes a value. Sets suit texts you reuse.

Write the set of your default language as a class whose name ends in the language code in PascalCase, such as ShopLocalizationEn. It lists ILocalizationSet, and the contract the source generator writes from it:

public class ShopLocalizationEn : ILocalizationSet, IShopLocalization
{
    public string Cart => "Cart";
    public string Checkout => "Checkout";
    public string Greeting(string name) => $"Hello, {name}!";
}

That makes three names to tell apart:

  • ShopLocalizationEn is the set you wrote, in your default language.
  • IShopLocalization is the contract, which lists every text. The source generator writes it from your set.
  • ShopLocalization is the class you show the texts with. The source generator writes it as well.

Both generated names are the set's name without its language code, and both land in the set's namespace.

For every other language, write a set that lists the same two interfaces:

public class ShopLocalizationNl : ILocalizationSet, IShopLocalization
{
    public string Cart => "Winkelwagen";
    public string Checkout => "Afrekenen";
    public string Greeting(string name) => $"Hallo, {name}!";
}

The build checks your sets:

  • A missing text or a missing language is a build error.
  • A set without its contract is a build error too, and your IDE adds the contract.
  • A class without ILocalizationSet isn't a set, so the build reports its language as missing.

There's nothing to register, because every set is registered automatically.

If you add German to your languages, the build tells you which set to write:

error LOC201: 'ShopLocalization' has no set for 'de', which [assembly: SupportedLanguages] requires.
Add 'class ShopLocalizationDe : ILocalizationSet, IShopLocalization'. A class without ILocalizationSet doesn't count as a set, and neither does a nested, generic, or file-local one.

An enum gets a set of its own, which lists ILocalizationSet<TEnum> (Enums).

Write an inline text

An inline text is written where you use it, in markup or in C#. It suits a text you use once:

public string Title => LocalizableText.Inline(en: "Orders", nl: "Bestellingen");

Inline takes one argument per language you declared, so leaving out a translation doesn't compile. It returns the wording for the user's language, so write the property with =>. A property with an initializer keeps the wording of the first user.

To include a value, write it into every language: LocalizableText.Inline(en: $"Hello, {name}!", nl: $"Hallo, {name}!"). A Dutch wording that lost its {name} is a build error (LOC402).

Write an attribute

An attribute puts a text on the member it names: a property, a field, an enum member, or a type. It suits a label, such as the name of a property of a model, which a form, a table or an export shows:

[Localized(en: "Address", nl: "Adres")]
public class Address
{
    [Localized(en: "City", nl: "Plaats")]
    public string City { get; set; } = "";

    [Localized(en: "Postal code", nl: "Postcode")]
    public string PostalCode { get; set; } = "";
}

[Localized] takes one argument per language you declared, as Inline does, so leaving out a translation doesn't compile.

Its wording is a constant, so it can't include a value. Write a text with a value in a set or inline.

Show a text

Show a text of a set by its name, wherever you need it:

<button>
  @ShopLocalization.Cart
</button>

In your IDE, hovering over ShopLocalization.Cart shows its wording in every language, and you can jump to each.

An inline text shows where you write it.

To show a text written as an attribute, ask for it by its type and the name of its member. A type's own text needs only the type (details):

<h2>
  @(LocalizableText.GetTextOfType<Address>())
</h2>

<label>
  @(LocalizableText.GetTextByMember<Address>(nameof(Address.City)))
</label>

All three give a string: the wording for the user's language at the moment you read the text. So they work in markup, in string interpolation, and wherever a string is expected, as in string label = ShopLocalization.Checkout;.

The user's language

The user's language is CultureInfo.CurrentUICulture. A desktop or console app starts with the language of the user's system, and a Blazor WebAssembly app with that of the browser.

An ASP.NET Core app needs request localization, as in app.UseRequestLocalization([.. LocalizationRegistry.SupportedLanguages]), which sets the language for each request. Without it, every user gets the language of the server. The ASP.NET Core page has the rest.

Users whose language you don't support see your default language (details).

An app that keeps the user's language somewhere else, such as in a user profile, sets LocalizationRegistry.CurrentUICultureProvider once at startup. Every text then follows the culture it returns (details):

LocalizationRegistry.CurrentUICultureProvider = () => UserProfile.Current?.Culture;
Another language

To get the wording in another language, such as that of an email's recipient, wrap the block that reads the texts in LocalizationRegistry.UseCulture:

using (LocalizationRegistry.UseCulture(CultureInfo.GetCultureInfo("nl")))
{
    // "Hallo, Anna!", whatever the user's language
    string greeting = ShopLocalization.Greeting("Anna");
}

The culture formats dates and numbers too, so the whole block is Dutch. The scope follows your code across an await (details).

Texts with parameters

A text that takes a value, such as Greeting above, is a method. Call it with the value:

<p>
  @ShopLocalization.Greeting(customer.Name)
</p>

Every language implements the method, so each can put the value where it reads best. A set that leaves the method out, or gives it other parameters, doesn't compile. A value can carry a format, as in $"Total: {amount:N2}".

Use the value in every language. A Dutch greeting that lost its {name} is a build error:

error LOC402: 'ShopLocalizationNl.Greeting' doesn't use its parameter 'name', but the default wording of 'IShopLocalization.Greeting' shows it. Use 'name' in every language, or users of 'nl' won't see it.

A placeholder such as {0} gets build warning LOC403, because nothing fills it in. Your IDE turns such a text into a method that takes the value.

The values are formatted for CultureInfo.CurrentCulture, as in any interpolated string.

Plurals

A text with parameters can choose its wording by their values, as for a plural:

public class ShopLocalizationEn : ILocalizationSet, IShopLocalization
{
    public string ItemsInCart(int count, string shop) => count is 1
        ? $"1 item in your cart at {shop}"
        : $"{count} items in your cart at {shop}";
}

public class ShopLocalizationNl : ILocalizationSet, IShopLocalization
{
    public string ItemsInCart(int count, string shop) => count is 1
        ? $"1 artikel in je winkelwagen bij {shop}"
        : $"{count} artikelen in je winkelwagen bij {shop}";
}

Idioma has no plural rules. Each language writes its own conditions in C#: English and Dutch have two forms, and Polish has more (details).

Enums

A set of an enum gives every member a text. It lists ILocalizationSet<TEnum> in place of ILocalizationSet:

public enum OrderStatus { Pending, Shipped }

public class OrderStatusLocalizationEn : ILocalizationSet<OrderStatus>, IOrderStatusLocalization
{
    public string Pending => "Pending";
    public string Shipped => "Shipped";
}

A member you add to the enum later is a build error in every language until it has a text. A set also works for an enum you can't change, such as DayOfWeek. Show a value's text with order.Status.GetText(). To keep the wording next to each member instead, mark each one [Localized] (details).

Where Idioma doesn't fit

  • When you need to add translations without a new build. Every language is compiled into your assembly, so a new language or a changed wording takes a new build. Where a wording must change without one, your app can ask an IStringLocalizer first, such as one that reads a database (details).
  • When your translators work in a translation tool, which reads files such as XLIFF.
  • When your app targets .NET 9 or earlier, or can't move to C# 14.

Documentation

The docs cover the rest, a page per subject:

License

MIT. Code the source generator writes into your project is yours, and carries no obligations under this license.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.11.1 0 11/24/2026
0.11.0 0 11/24/2026
0.10.0 0 11/24/2026
0.9.0 57 11/20/2026
0.8.0 100 11/15/2026
0.7.0 112 11/14/2026
0.5.0 90 11/13/2026
0.4.0 93 11/13/2026
0.3.0 85 11/13/2026
0.2.0 88 11/13/2026
0.1.0 93 11/13/2026

Every text is a string. ShopLocalization.Checkout, ShopLocalization.Greeting(customer.Name), LocalizableText.Inline(...), LocalizableText.GetTextByMember and GetText() each give the wording for the user's language at the moment you read them. Read a text where you show it, because a string kept in a static field stays in the language of the first user. The build warns about a text in the initializer of a static field or property (LOC602). LocalizableText is no longer a type you hold. It only has Inline, GetTextByMember and GetTextOfType. Its conversions, its equality, ToString(culture), Key, FullKey, Contract, Arguments, Keyed, GetWording and its JSON converter are gone. A parameter that took a LocalizableText takes a string. Rebuild every library built with an earlier version, because the generated code calls other methods.

A text takes a value as a method of its set. Write Greeting(string name) in each language and call ShopLocalization.Greeting(customer.Name). LocalizableText.Format is gone, and nothing fills in a placeholder such as {0} any more, so a wording that holds one shows its braces (LOC403, with an IDE fix that writes the method). Each language writes its own plurals in C#, because a wording can depend on its values.

A set can hold the texts of an enum. List ILocalizationSet<OrderStatus> in place of ILocalizationSet, and every member of the enum needs a text in every language (CS0535). order.Status.GetText() gives a value its text, and so does OrderStatusLocalization.GetTextByValue(order.Status). A value that two members have gives the text of the first, so OrderStatus.None.GetText() next to Default = 0 is a build warning (LOC600).

A label can sit on the member it names. Mark a property, a field, an enum member or a type [Localized(en: "City", nl: "Plaats")], with a wording per language. LocalizableText.GetTextByMember<Address>(nameof(Address.City)) gives the text, also by a Type or a PropertyInfo your code holds. LocalizableText.GetTextOfType<Address>() gives the text of a type itself, and a value of an enum gives the text of its member with GetText(). The source generator writes each text as code, so nothing reads the attribute at run time. A member of an enum without [Localized] next to members that have it is a build warning (LOC501), and so is GetText() on the later of two marked members with one value (LOC600).

The build holds every language to the values. A language whose method never uses a value the default wording shows doesn't build (LOC402), and neither does a language of LocalizableText.Inline that leaves out a value the default language shows.

The names say localization. A set implements ILocalizationSet and its contract: class ShopLocalizationNl : ILocalizationSet, IShopLocalization. The registry is LocalizationRegistry, and [ExcludeFromLocalizationDiscovery] keeps a set or a member out. A set's name ends in its language in PascalCase, such as ShopLocalizationEnGb for en-GB, and an inline text takes enGb: for it. A class that lists only a contract is no set. A set states its language in LocalizationLanguageCode, which was LanguageCode: string ILocalizationSet.LocalizationLanguageCode => "en-GB".

ResourceText is gone. The key of a text of a set is its name, such as Checkout. Every generated class has a method GetTextByKey, for a key your code only has at run time. ShopLocalization.GetTextByKey("Checkout") gives the wording, and GetTextByKey("Greeting", customer.Name) that of a text with parameters. A key no text has throws. No text can be named GetTextByKey any more (LOC300).

LocalizationRegistry.Create is gone, because nothing public read a text from a registry of your own. Read a set of your app with LocalizationRegistry.Current.Find. [Localized] written through an alias is a build warning (LOC502), and the fix for parameter names (LOC401) no longer moves names over parameters of different types.

LocalizationRegistry.CurrentUICultureProvider gives the user's culture in an app that keeps it somewhere other than CultureInfo.CurrentUICulture. LocalizationRegistry.UseCulture(culture) makes every text in a using block follow another culture, such as that of an email's recipient, also across an await.

An app declares its languages with [assembly: SupportedLanguages], also an app without texts of its own, such as a test project or the server project of a Blazor Web App. An app without the attribute doesn't build (LOC103). The languages of an app it references no longer count.

The codes of the build's messages have new numbers, grouped by subject, from LOC100 to LOC701. Update every code in a NoWarn or a #pragma. The page docs/build-errors.md lists each one, and marks those with an IDE fix.

A language code that no user's culture matches is a build warning (LOC104), such as dk for Danish, which is da, or en-UK, which is en-GB. The set of your default language can list ILocalizationSet through an abstract class it derives from. A set whose constructor reads a text of its own contract throws an InvalidOperationException that names the set, where the app stopped before. LOC600 is reported in Razor markup too.

The details have a page per subject under docs/, which the README links to. New is a page on performance, with measurements next to IStringLocalizer.