XLocalizer in a console app without services.AddRazorPages #49
Replies: 3 comments 2 replies
-
`using CultureSwitcher Cs = new("es");
|
Beta Was this translation helpful? Give feedback.
-
Don't worry I'm not bothered, instead I feel glad to see different use cases of Actually I didn't consider console apps support with Re your issue, I see that you need to register some internal services to get it working. Have a look at the XLocalizer/XLocalizer/DependencyInjection.cs Lines 65 to 112 in 07e1a00 Maybe you can avoid adding data annotation and identity errors localization, sine they are not available in a console app. Hopefully this will help, if not let me know please. |
Beta Was this translation helpful? Give feedback.
-
Thank you @LazZiya ! That worked. Here is the code that worked for me, perhaps it can help someone else. `
|
Beta Was this translation helpful? Give feedback.
-
@LazZiya I don't mean to bother you so much, but I have run into an issue using XLocalizer in my console app.
Previously you helped me get it working in my console application which generates receipts in a loop.
I used the CultureSwitcher to switch cultures in the loop depending on the customer's culture who is receiving the looped receipt.
I was generating PDFs using a library that fetches an external webpage and converts the HTML to PDF. I have since switched to generating the view using Microsoft.Playwrite which renders the HTML locally without having to go to the internet for it.
Unfortunately, Playwrite does not seem to work well when services.AddRazorPages() is used, and this means I cannot hook up services.AddRazorPages().AddXLocalizer<>().
With the help pf chatGPT, I was able to wire up IStringLocalizer as follows
`// Configure Request Localization Options
services.Configure(options =>
{
var SupportedCultures = new List()
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
new CultureInfo("es")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = SupportedCultures;
options.SupportedUICultures = SupportedCultures;
});
While this allows the program to run, if I set any culture other than the default culture, I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
at XLocalizer.XStringLocalizer
1.GetLocalizedString(String name, Object[] arguments) at XLocalizer.XStringLocalizer
1.get_Item(String name)Hoping you can help? I am happy to pay if you need to code something new for this :) Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions