Localize a component in another assembly - Updated

Kasrak 26 Reputation points
2021-07-29T19:02:38.2+00:00

Updated all the question, reformed and posted the new code here:

I tried 2 approaches at the same time to find out how I can localize my component which is located in a different assembly.

Folder/ Files architecture:

119428-compolocalization.png

Index (Usage in the Main project)

@page "/"  
  
<Component1 />  

Component1:

@inject IStringLocalizer<Component1> Loc  
@namespace OurComponents  
  
<div class="my-component">  
    <span>@Loc["stLink"]</span>  
</div>  

Startup file:

public void ConfigureServices(IServiceCollection services)  
{  
    services.AddControllers();  
    services.AddLocalization(options => options.ResourcesPath = "Resources");  
    services.Configure<RequestLocalizationOptions>(options =>  
    {  
        var supportedCultures = new List<CultureInfo>()  
        {  new CultureInfo("en-US") };  
        options.DefaultRequestCulture = new RequestCulture("en-US");  
        options.SetDefaultCulture("en-US");  
        options.SupportedCultures = supportedCultures;  
        options.SupportedUICultures = supportedCultures; ;  
    });  
    services.AddRazorPages();  
    services.AddServerSideBlazor();  
}  

Namespaces are imported globally.

Currently @Loc ["stLink"] doesn't get the info from any of the defined resources.

Source Code:

https://1drv.ms/u/s!AlScPmE9PLAKgXdHCEobuWZ_Jzv3?e=diNLoR

Note: Basic localization is working fine, just can't localize the components in the library assembly.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.