.NET6 Blazor Server Application Localization - localization

I would like to add language resources to my application based on the browser language preferences.
Blazor server application, .net6, c#
I will be using the syntax such as this on a razor page. #localizer["helloworld"]
I expecting the localizer to know the language set in the browser and then use the appropriate resource file.
I am using ISO language codes for the resource files. The app needs to support english and french, so I have resource.resx and resource.fr.resx
I have spent lots of time trying to get this to work. The documentation seems so easy, yet I have had zero success.
The application will compile and run. The default language of English is always coming up, regardless of the browser language setting. My theory is either the browser language is not being detected, or the naming of the resource file is incorrect preventing the localizer from using it.
The first lesson is that where builder.Services.AddLocalization(); is declared in program.cs is important. It will not function at all if in the wrong location (line)
Does anyone have any experience with this issue using .net6 and Blazor server app?
Thanks,
Marc.

For a full example about localization.
https://github.com/iso8859/AspNetCoreAuthMultiLang
#region Localization
builder.Services.AddLocalization(option => option.ResourcesPath = "Resources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>()
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
#endregion
#edit
Because server side blazor app are Single Page App you can't change the cookie. The only way to change a cookie it is to do a page refresh. This is done in the AuthController.cs
For WASM Project you can set cookie using Javascript
the value is for en-US
Set-Cookie: .AspNetCore.Culture=c%3Den-US%7Cuic%3Den-US; path=/

Both the above posts provided the information I needed for my implementation. Some of the MS documentation was a bit cryptic in assuming you knew all the correct parameter settings, but after a bit of tweaking I was successfully able to build the solution I required for my application. Thanks to all for their help.

Related

How can I protect an old site that has no validation?

Scenario:
I have a website (CATALOG.COM), without any validation request, and without source code.
Now I need to set a validation to access.
Possibility Solution
If I create another site with validation (START_WEB.Com), and after I redirect on CATALOG.COM, can be a good solution ?
For example:
[Authorize]
public ActionResult Run_Old_WebSite()
{
return Redirect("www.Catalog.com");
}
Any suggestions?
p.s. I work with Asp.net, Mvc and Bootstrap
without the source code availability , You cannot validate the page.
Below is the alternate solution for this:
When any one hit the CATALOG.COM apply the 301 redirection rule to it and return it to the new site url like: START_WEB.Com?returnUrl=CATALOG.COM
Then in the new site add some validation login page, authenticate it and then redirect it to the old url: CATALOG.COM?validated=true
In this way you can achieve this, Here you can track the users also who are visiting to the old url site.
Assuming you have access to the published files, I would copy them to my local machine, decompile the site specific assemblies using something like Jetbrains dotPeek (it's free) and re-create a new solution in Visual Studio with the decompiled sources. Depending on site size/complexity this may take a bit of time but you would at least be able to make improvements to the code,build and deploy.

Localising ASP.NET MVC JQuery Validation on Azure

I am developing a website to support both Arabic and English. Now, when on the development I only need to use DisplayAttribute to localize field names and set the thread to the appropriate culture, and then ASP.NET generates a localized error message using my DispalyAttribute names.
However, when deploying on Azure, and when I switch to Arabic, everything is localized correctly but error messages. In which the names are localized but the error itself is in English.
I could use ErrorMessageResourceType explicitly, but would like to know where is the discrepancy.
Note: I set thread culture at Application_BeginRequest of Global.asax.
I made a little hack to know where the issue is. I created my own ValidationAttribute which basically reports current culture name instead of the error message. It turned out that the culture is properly set, and that it seems Azure version of DataAnnotationResources might not include Arabic.

Servicestack razor page is getting added to content

I am trying to use Servicestack with F#. So, far I am successful. But while trying to pull thing up with asp.net hosting using razor engine. I come across weird issue.
If for default.cshtml I choose property that do not copy with compilation with content than page is not getting populated and it is going to metadata page. But if I set to copy if newer it will work.
But as far as I know this should be do not copy only as compilation is content. If we are not doing that then for every change in cshtml page there is need to re-run the project.
Now, this is also unique to F# project, in C# it is working. So, I exactly don't know where to look. BTW I am using 3.9.71 version of servciestack.
Please let me know if any further details is needed. My project repo
Update:
Your application is setup incorrectly.
Your F# MVC application isn't setup properly. You are using ASP.NET which should use IIS as its host. Which means the requests from IIS get passed into the AppHost. However in your setup you are using an AppHostHttpListenerBase this is actually creating it's own HTTP Listener, essentially you have mixed the Standalone Self Hosting with ASP.NET hosted setup.
As a result you are getting the odd situation of having to copy the content to the output directory. That's because in a standalone ServiceStack app, that is the requirement, it's looking for the content in the wrong place.
Try using this:
type AppHost =
inherit AppHostBase
new() = { inherit AppHostBase("Hello F# Services", typeof<HelloService>.Assembly) }
override this.Configure container =
...
You should read this article to ensure you have setup the MVC ASP.NET application correctly.
You will still want to use DebugMode = true to enable ServiceStack to automatically pick up on changes, so I have left that part of the answer in.
It's a little hard to follow what you are saying, but if I have interpreted it correctly, you are saying you have an F# ASP.NET ServiceStack Razor project and you are finding that you are having to re-run the build process every time you make changes to your Views.
I haven't used F# yet so I will have to give the example in C# but you need to set your AppHost config to DebugMode = true in order to have ServiceStack automatically pick up on the changes, and thus you won't have to re-run the build process each time.
SetConfig(new EndpointHostConfig {
DebugMode = true,
});
I notice in your code that this is not set:
type AppHost() =
inherit AppHostHttpListenerBase("Hello F# Service", typeof<HelloService>.Assembly)
override this.Configure container =
this.Plugins.Add(new RazorFormat())
ignore()
static member start() =
let apphost = new AppHost()
apphost.Init()
See the section "Automatic reload of modified views, layout templates and partials (in Debug mode)" in this documentation for more information.
So in Debug Mode we'll also do this where a background file system watcher monitors all pages, partials and Layout templates for modifications and recompiles and auto-reloads them on the fly ...

Custom Resource Provider

I am using custom resource provider in Asp.Net to get the data from the external resource files, After changing the browser language it does not select the proper translation. i.e. It always returns English version. On the other side, if i use Asp.Net tag
It returns the correct translation. I configured it in web.config
Am i missing something?
Actually i figured out by myself. In my custom resouce provider, i was passing the cultureinfo by myself. I change it to null and it started working. Its wiered but worked for me.

Silverlight Cannot find XML data source

I am very new to Silverlight development. I understand that this is client side technology therefore the paradyme is differant from that of conventional ASP.NET development. Having said that, I don't understand where my server side code is deployed.
I have a silver light \ MVC application. I am trying to read an XML document from within my 'Models' folder. The following peice of code is executed from within a class that is in the same location as the XML document, 'Models'. The load() results in a SystemIOFileNotFound exception. I noticed that when building the application the XML document is not laid down in the same location as the web project's assembly. I assume this is specific to the fact that this is a Silverlight project. Can someone tell me what I'm missing?
_xdoc = new XDocument();
_xdoc = XDocument.Load(new Uri("videos.xml",UriKind.Relative).ToString());
Edit..
The behavior I am after is the start page (silverlight) populates controls via a server side controller. ie localhost/video
Silverlight can't access your filesystem (thankfully), which is why you can't access the file. Try embedding it as a resource, or storing it in the local storage API provided by silverlight.
Assuming that your Models folder is in the Web project (i.e. not the Silverlight project), I think that your problem is unrelated to Silverlight.
The code loading the XML file assumes that the file is in the current directory, so you need to ensure this through your deployment technique.
If you are doing this in the Silverlight part, you should put the XML file in an embedded resource and access it as a stream (get it with Assembly.GetManifestResourceStream) or as a resource (a la WPF, not an embedded resource) and access it with the package part syntax.
The problem was that I was attempting to access this static resource as you would in typical ASP.net. However I found it necessary to map the path to the file using the current HTTPContext:
HttpContext.Current.Server.MapPath("~/App_Data/videos.xml");
So the above worked for me. Since this code is in the web project and not in the silverlight project I am still unclear as to why I cannot just access this resource using a relative path. This code will be executed in the context of the web server.
i.e.
XDocument.load(../App_Data/videos.xml);

Resources