Migrating Westwind Globalization from .net framework to .net 6

91 Views Asked by At

We are using westwind.globalization (3.0.5) in .net framework projects and wanted to reuse the same localization resources for a new .net 6 api. In existing .net framework projects, after setting up the web.config, then we can use DbRes static class.

 DbRes.T(localeId, resourceSet, lang)

For .net 6, I have tried different approaches as stated in the documentation but I am unable to pull the localizations.

using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Localization;
using Westwind.Globalization;
using Westwind.Globalization.AspnetCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();//getting "Failed to load API definition" without DocInclusionPredicate
builder.Services.AddSwaggerGen(c =>
{
    c.DocInclusionPredicate((docName, apiDesc) =>
    {
        // Filter out 3rd party controllers
        var assemblyName = ((ControllerActionDescriptor)apiDesc.ActionDescriptor).ControllerTypeInfo.Assembly.GetName().Name;
        var currentAssemblyName = typeof(Program).Assembly.GetName().Name;
        return currentAssemblyName == assemblyName;
    });
});


builder.Services.AddSingleton<IStringLocalizerFactory, DbResStringLocalizerFactory>();
builder.Services.AddWestwindGlobalization(
    opt =>
    {
        opt.ResourceAccessMode = ResourceAccessMode.DbResourceManager;
        opt.ConnectionString = "***connectionstring***";
        opt.DataProvider = DbResourceProviderTypes.SqlServer;
        opt.ResourceTableName = "Localizations";
        opt.AddMissingResources = false;
    }
    );

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

then for testing, just calling it like this

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
    var x = DbRes.T("lbl_Ok", "APP_LOCALE", "en-gb");

    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

Ideally, we wanted the configuration in the appsettings.json, but even via AddWestwindGlobalization(), we are not able to make it work. What settings or steps are we missing here? TIA.

0

There are 0 best solutions below