Set multilanguage validation error message asp.net mvc - asp.net-mvc

I'm trying to set message for attribute this way in view model
[Required(ErrorMessageResourceName = nameof(Form.Required), ErrorMessageResourceType = typeof(Form))]
So i have my Form.resx file with Required key. I use PublicResXFileCodeGenerator tool and embedded resource type for resource file. I also have other files named form.culture.resx.
I'm also use this to set culture (current culture is "en" too now)
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(currentCulture);
at custom filter attribute.
The problem is that when I try to sumbit my form with empty field, i'm receiving the same message (in Russian).

Related

ASP.NET Core + resx - works only default culture

I am trying to port a WebForm app to MVC 6 and am having an issue with getting resources from a resx file. The code in the controller class returns only default resx values.
I tried both setting Thread.CurrentThread.CurrentCulture & Thread.CurrentThread.CurrentUICulture as well as setting Resources.MyResouces.Culture to specific culture, but Resources.MyResouces.Key still returns only the default. What could be the problem?
public IActionResult MyAction(){
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture =
Resources.test.Culture = CultureInfo.GetCultureInfo("fr-FR");
// The line below still returns the default English resource value
var localizedValue = Resources.test.test_key;
}
WebForms to Asp.Net Core is a pretty huge leap to take. I would recommend starting with the Asp.Net Core localization sample on GitHub to get a clearer picture of how it works.
In particular, you may need to do some more configuration in your Startup.cs file.

Default validation strings MVC

How to get all of default (English) validation messages, so I could translate them. I know I can specify resource name in validation attribute, but I just want to know all the messages.
To localize the default error messages in ASP.NET MVC you need to set the following properties in Global.asax Application_Start method,
ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
DefaultModelBinder.ResourceClassKey = "MyResources";
Next, you need to create a MyResources.resx resource file and your culture specific resource file MyResources.xx.resx inside App_GlobalResources folder where you can override the following messages:
PropertyValueInvalid
FieldMustBeDate
FieldMustBeNumeric
PropertyValueRequired

how to store culture name in database

I try to develop a multilingual website, the problem that I have just to store the culture name in the database (for all the other data I'm using resource file)and I have to call it (culture name) from the view using dropdown list. Does anybody has an idea or examples?
For your point make Culture table with Id, culture code and Culture Name columns. Store culture code i.e. nl-NL and culture Name i.e. Dutch.
Then fetch them from db and create a list of it and bind them to dropdown. Bind culture code as value and culture name as Text of select dropdown. Use SelectListItem to do so like below:
this.ViewBag.Cultures = new SelectListItem(context.Cultures.Select(x=> new{ Id = x.CultureCode, Name = x.CultureName}), "Id", "Name").ToList();
Then on front end use dropdownlist:
#Html.DropdownList("Cultures", (IEnumerable<SelectListItem>)this.ViewBag.Cultures, "Select Culture")
Note: Store only those languages which you want to show on front side i.e. on UI.
You can ask me apart from this if you have any trouble.

Globalization with MVC

I am trying to localize my web page, and have done the following :
Added resource files with some example strings
Added a call to a method to set culture and ui culture on the click event of flag icons :
public void SetCulture(string culture)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
}
And I have made reference to my resource file strings in my web page :
#Resources.General.String1
I have stepped through my code and the culture is successfully changed in my SetCulture method, but the string does not change on the web page. Can anybody advise why?
I don't understand what you mean with the click event in a ASP.NET MVC application. I would use routing to set the new language. A sample you can find at Localization with ASP.NET MVC using Routing.
Instead of using a Javascript event, you have to reload the whole page.

Setting CurrentUICulture is not being remembered by my application

I have an asp.net mvc application where i want the user to be able to change language. I have provided a series of links with small flags on to let the user choose language. The target of all these links is my "dashboard" page, in which controller i have this code:
[HttpGet]
[Authorize]
public ViewResult Dashboard(string id)
{
if (!string.IsNullOrEmpty(id))
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(id);
}
}
The "Dashboard" page is displayed in the chosen language, as it should be. But when i navigate on through my website, the culture is changed back to english (default)... am i missing something? Shouldnt changing the CurrentUICulture change the entire application to the other language?
In System.Threading.Thread.CurrentThread.CurrentUICulture, in this way the culture is set for only current request of the user, and it will be reset to default language for subsequent user requests.
You need to set the ui culture on each view where you want to show language based data (Localized data).
Tip: save the user's selected culture id in session and use it on each view to specify the uiculture
like this,
// save users selected culture id in session
Session["SelectedCultureId"] = id;
on each view set current thread ui culture id
if (Session["SelectedCultureId"] != null)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["SelectedCultureId"]);
}

Resources