Asp.Net
This article describes how to localize a asp application. There is a sample library on github.
To run Avalanche.Localization on Asp.Net, make sure the Application's .csproj is targeting platform .NET6 or higher.
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
Add package references.
<PropertyGroup>
<RestoreAdditionalProjectSources>https://avalanche.fi/Avalanche.Core/nupkg/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalanche.Localization"/>
<PackageReference Include="Avalanche.Localization.Cldr"/>
<PackageReference Include="Avalanche.Localization.Extensions"/>
<PackageReference Include="Avalanche.Localization.Asp"/>
</ItemGroup>
Go to appsettings.json and add "Debug" level for "Avalanche" to get notified on localization issues.
"Logging": {
"LogLevel": {
"Default": "Warning",
"Avalanche": "Debug"
}
},
Get IServiceCollection reference from WebApplicationBuilder or run this in Startup.cs.
IServiceCollection services = builder.Services;
Remove possible previous .AddLocalization() service, ...
services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
Add .AddAvalancheLocalizationService().
using Avalanche.Localization;
services
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationEmbeddedResourceProviderDefault()
.AddAvalancheLocalizationResourceManagerProvider()
.AddAvalancheLocalizationAspSupport()
.AddAvalancheStringLocalizer()
.AddSingleton(typeof(ILocalizationFilePatterns), new LocalizationFilePatterns("Pages/{Key}", "Pages/{Culture}/{Key}"))
.AddAvalancheLocalizationEmbeddedResourceProvider("*/*.Pages.{Key}", "*/*.Pages.{Culture}.{Key}")
.Configure<Microsoft.Extensions.Localization.LocalizationOptions>(options =>
{
options.ResourcesPath = null!;
});
Choose localization file source. The .AddAvalancheLocalizationService() above adds application root (calls AddAvalancheLocalizationFileSystemApplicationRoot()). This can be replaced with a file provider.
services.Remove(LocalizationServiceDescriptors.Instance.LocalizationFileSystemApplicationRoot);
services.AddAvalancheLocalizationToUseFileProvider();
Possibly add IFileProvider.
services
.AddSingleton(typeof(IFileProvider), new PhysicalFileProvider(AppDomain.CurrentDomain.BaseDirectory));
You may or may not want to add IViewLocalizer and IHtmlLocalizer services from Microsoft.AspNetCore.Mvc.Localization.
using Microsoft.Extensions.DependencyInjection;
services.AddRazorPages().AddViewLocalization();
After 'app' construction, add workaround to a known issue: Flushes cache if Assembly is loaded later. Late loaded assembly may supply localization lines that have already been queried and cached. This issue will be fixed properly later.
AppDomain.CurrentDomain.AssemblyLoad += (object? sender, AssemblyLoadEventArgs args) =>
(app.Services.GetRequiredService<ILocalization>() as ICached)?.InvalidateCache(true);
More information in Microsoft.Extensions.DependencyInjection.
Note
AspNetCore support is experimental.