question

Kasrak-1517 avatar image
0 Votes"
Kasrak-1517 asked ZhiLv-MSFT commented

Localize a component in another assembly - Updated

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.


dotnet-aspnet-core-blazor
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


@JasonPan-MSFT

Hello, searched before my post,
Saw and checked the article you posted before,
It isn't related to a component located in another assembly.
The normal localization procedure of my application works fine when components are located in the main web project,
the problem is with localizing components in another assembly.

0 Votes 0 ·

Uploaded a very basic sample project showing the question here: https://1drv.ms/u/s!AlScPmE9PLAKgXYvC29PyaVfazX3?e=UtIvyj
In this example let me know how can I use Component1 Localization in the main project?
Going out, if I back soon, may edit and add a parameter which we can use/edit the localization for the component right in the main project.
Hope you get what I mean here.

0 Votes 0 ·

Hi @Kasrak-1517,

When we configure the localization, in the ConfigureServices method we need to add the localization services with AddLocalization, it specifies where to get the resources, in your case it is “Resources” (since you will put the resx files in the “Resources” folder). If you add the resx files in the different assembly (Thelib's Resource file), the main page localization services can't find these resx file in the main application's Resources folder, so the library assembly's localization will not work. I suggest you could try to put all of the resx file in the main application Resources folder, in library assembly just add the components.

0 Votes 0 ·

0 Answers