CodeChops.LightResources
1.6.3
See the version list below for details.
dotnet add package CodeChops.LightResources --version 1.6.3
NuGet\Install-Package CodeChops.LightResources -Version 1.6.3
<PackageReference Include="CodeChops.LightResources" Version="1.6.3" />
paket add CodeChops.LightResources --version 1.6.3
#r "nuget: CodeChops.LightResources, 1.6.3"
// Install CodeChops.LightResources as a Cake Addin #addin nuget:?package=CodeChops.LightResources&version=1.6.3 // Install CodeChops.LightResources as a Cake Tool #tool nuget:?package=CodeChops.LightResources&version=1.6.3
Light resources
Lightweight dynamic resources for your Blazor website
LightResources is a Blazor base component for enabling lightweight text resources for a Blazor WebAssembly website. When using the default .NET solution for localization, Blazor has to refresh the website in order to fetch and load the satellite DLL which contains the non-default resource. This is slow and cumbersome. LightResources fixes this problem by loading the resources statically when starting the application. Now the language can be changed dynamically on your page.
This package works on every Blazor rendering mode. Check out www.CodeChops.nl to see this package in action, and to see more projects.
Advantages
- The page does not need to be refreshed when switching the current UI language. So you get a real SPA.
- The resources are lightweight and fast.
- The .NET
CultureInfo
does not have to be used, so your Blazor WebAssembly application will be smaller. - Resources can be searched through.
- Easy implementation.
- Reference a resource the same way as in traditional .resx files:
@HomeResource.Title
.
This package makes use of MagicEnums and Implementation discovery under the hood.
Getting started
- Install the package
CodeChops.LightResources
in your Blazor project. - Add
builder.Services.AddLightResources()
to yourProgram.cs
and provide the language codes you want to support. - Optional: Set the current language code by calling
LanguageCodeCache.SetLanguageCode()
. If this step is omitted, the first provided language code will be set as default. - Create a component and inherit it from
ResourcedComponent
:@inherits ResourcedComponent
- Create a new file (as code behind, for example
HomePage.razor.cs
). - Create a record with a name. Suffix the name with the simple language code in upper case for non-default language resources.
- Implement the base class
Resource
for each language. The type parameters that should be provided to the base class are:- The current resource implementation
TSelf
. - All discovered resource implementations of the project, to be found in
CodeChops.LightResources.ResourceProxyEnum
.
- The current resource implementation
Example
Add the following to your Program.cs:
builder.Services.AddLightResources(new LanguageCode[] { new("en-GB"), new("nl-NL") });
Create a new resource file and add the following:
namespace CodeChops.Website.Client.Pages.Home;
public record HomeResource : Resource<HomeResource, ResourceProxyEnum>
{
public static string Title => CreateMember("Welcome");
public static string Author => CreateMember("Logo, design and website by CodeChops");
}
public record HomeResourceNL : Resource<HomeResourceNL, ResourceProxyEnum>
{
public static string Title { get; } = CreateMember("Welkom");
public static string Author { get; } = CreateMember("Logo, design en website door CodeChops");
}
Note that the default language should have expression bodied properties. This will forward your call to the resource in the current language (when the language is non-default).
The resource can now be called from a razor file:
<Title>@HomeResource.Title</Title>
API
LanguageCode
The default ISO 639-1 language code with a 2-letter country code (ISO 3166-1 alpha-2) where relevant, for example: "en-GB".
CultureInfo
is not used in this library in order to reduce package size.
No check takes place if the language code is an existing language code. This can be done by using
CultureInfo
if needed.
LanguageCodeCache
Member | Description |
---|---|
DefaultLanguageCode |
Retrieves the default language code. |
CurrentLanguageCode |
Retrieves the currently displayed language code. |
CurrentSimpleLanguageCode |
Takes the first 2 letters of CurrentLanguageCode and converts it to upper invariant. |
SetCurrentLanguage() |
Sets the current displayed language code. |
The resource managers make of MagicEnums under the hood, so the base API is the same as the MagicEnum API. This API can be used to search for or add resources at runtime:
Resource
Method | Description |
---|---|
CreateMember |
Creates a new discovered implementation member and returns it. |
GetEnumerator |
Gets an enumerator over the enum members. |
GetMembers |
Gets an enumerable over:<br/>- All enum members, or<br/>- Members of a specific value: Throws when no member has been found. |
GetValues |
Gets an enumerable over the member values. |
TryGetMembers |
Tries to get member(s) by value. |
TryGetSingleMember |
Tries to get a single member by name / value.<br/>Throws when multiple members of the same value have been found. |
GetSingleMember |
Gets a single member by name / value.<br/>Throws when not found or multiple members have been found. |
GetUniqueValueCount |
Gets the unique member value count. |
GetMemberCount |
Gets the member count. |
GetDefaultValue |
Gets the default value of the enum. |
GetOrCreateMember |
Creates a member or gets one if a member already exists. |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Architect.AmbientContexts (>= 2.0.0)
- CodeChops.DomainModeling (>= 2.17.8)
- CodeChops.ImplementationDiscovery (>= 1.8.7)
- Microsoft.AspNetCore.Components.Web (>= 8.0.8)
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 |
---|---|---|
1.6.6 | 72 | 11/19/2024 |
1.6.5 | 34 | 11/19/2024 |
1.6.4 | 372 | 10/1/2024 |
1.6.3 | 79 | 9/30/2024 |
1.6.2 | 86 | 9/29/2024 |
1.6.1 | 132 | 3/10/2024 |
1.6.0 | 120 | 3/9/2024 |
1.5.6 | 105 | 3/9/2024 |
1.5.5 | 126 | 3/9/2024 |
1.5.4 | 114 | 3/9/2024 |
1.5.3 | 124 | 3/9/2024 |
1.5.2 | 121 | 3/4/2024 |
1.5.1 | 116 | 3/4/2024 |
1.5.0 | 122 | 3/4/2024 |
1.4.7 | 136 | 3/4/2024 |
1.4.5 | 115 | 3/3/2024 |
1.4.4 | 121 | 3/3/2024 |
1.4.3 | 125 | 3/3/2024 |
1.4.2 | 125 | 3/3/2024 |
1.4.1 | 114 | 2/28/2024 |
1.4.0 | 117 | 2/28/2024 |
1.3.0 | 248 | 3/20/2023 |
1.2.1 | 210 | 3/16/2023 |
1.1.1 | 298 | 1/21/2023 |
1.1.0 | 281 | 1/21/2023 |
1.0.2 | 306 | 1/12/2023 |
1.0.1 | 503 | 1/8/2023 |
Updated packages.