Hello,
I have followed tutorial of localization in Asp.Net core 6 web API. I cloned the project from "https://github.com/aram87/LocalizationInAspNetCoreWebApi. It is working properly but I created the same project with the following steps but that is not working. I am not getting the expected results from that. I have added dependencies and package, all other necessary code and files but that is not working. Actually I have created a duplicate project but that is not working the same as Aram's project. Why?
Please help me with this. What I am getting in output while testing Postman is: I am getting the key string which I am passing of resource.
[HttpGet]
[Route("PostsControllerResource")]
public IActionResult GetUsingPostsControllerResource()
{
var article = stringLocalizer["My Article"];
var postName = stringLocalizer.GetString("Welcome").Value ?? "";
Thanks for a great and simple to understand guide Aram.
It is hard to find on the web what happens after deployment.
The resx file(s) get compiled in the ASP.Net 7.0 dll and then we can't update it.
I am trying to find where can I place updated resx file to override the compiled ones.
Hello.... this is perfect for a mulilingual site, however, I have developed a site that is to be used only by Spanish speaking users. How can the default ASP.NET core messages be shown in Spanish? For example, if user is changing password and he uses one that not meets the complexity requirements, these errors are shown: "Passwords must have at least one non alphanumeric character, Passwords must have at least one lowercase ('a'-'z'), Passwords must have at least one uppercase ('A'-'Z')."
When I developed in .NET Framework, those kind of messages appear correctly in Spanish. How can I achieve the same in .NET Core 6?
Thanks Aram, your explanation helped me ramp up this subject quickly.
Regarding the SharedResources, I managed to get the same result without the need of creating an empty class since I'm using the resx generator and I basically used the name of the auto-generated class instead of dummy\empty one.
I have a question about localization across multiple projects (for instance I have the WebApi project which I managed to add localization to it, but it's referencing different projects which I'd like to have them localized but with defining the resx in those project) Any idea of how to make this work ?
Regarding multiple projects usually I define resources per project, so each project will have its own resx. This should be easy by adding a new folder under your project with name like 'Resources' and then add a resource item like 'strings.resx', define your localization strings inside it, and after that add another file with the targeted locale like 'strings.fr.resx', and then add the translation for your strings.
and I make sure that whatever locale is requested from the initial client app it would be propagated all the way to the API and then to the DAL, or Services or infrastructure project whichever structure you have.
You can alternatively gather all your resources inside the API Project, and then you make sure that your API will map and translate all responses with the correct translation before they are sent back to the client.
Thank you for the nice and so useful article. Is there any quick and easy way to automatically localize an existing app that has been developed without localization with strings embedded in the code?
Thanks for your message. I don't there is an easy or quick way to do this, if your app is in Android, you have to move all the hard-coded strings to resource files and then add the needed locale-based resource file to with the intended translation for each string. You can use some third party tools to help you localize your resources.xml file.
Thanks for your message. To be honest, I haven't been following up much on the cross-platform track recently, since I am more focusing on the native apps development and web apps as well as building APIs with the ASP.NET Core technology. Nevertheless, MAUI seems to be a superior replacement for Xamarin, so it might be worth looking into in the near future, so I will keep your request in mind and do my best to write a MAUI localization tutorial soon.
I'm not an ASP.Net expert (although I use DotNet for Desktop app with similar technique to localize the UI and other strings),
Regarding the UI localization
What if we need to share the url to a specific page and it must be in the user preferred language, or when the user want to change the page language by clicking on the Arabic/English link?
unless I'm missing something it looks it's not possible here as we're relying on the request header to specify the response language,
maybe including the language in the url can solve this issue (when needed), of course we need to handle that language var so it loads the correct localization resource file,
Thank you for your message and apologies for the late reply. The Accept-Language Header is not the only way you can use to set the API culture or locale. In fact, there are 2 other ways that can be used for this purpose, one of them is through query strings. If you add the culture and/or culture-ui parameters to the URL, the QueryStringRequestCultureProvider will interpret these parameters and set the culture of your API with the provided values.
So in our tutorial, if we pass the culture as 'ar' in the query strings, like this: https://localhost:7153/api/posts/SharedResource?culture=ar and keep sending the Accept-Language as 'en', then you will get the result in Arabic, because the priority will go to the query string parameters.