Hi, I have three culture added to my supportedcultures and set default culture to fa-IR but when my website open default website culture is en-US, i dont know why default culture not worked??
below is part of my code that relate to Localization:
In ConfigureServices:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fa-IR"),
new CultureInfo("en-US"),
new CultureInfo("ar-SA")
};
opts.DefaultRequestCulture = new RequestCulture("fa-IR");
opts.SupportedCultures = supportedCultures;
opts.SupportedUICultures = supportedCultures;
});
In Configure:
app.UseRequestLocalization(
app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hello @MohammadRezaRahmatzadeh ... You're saying this happens when you make a request from a culture not supported by the app (not fa-IR, en-US, ar-SA)? For example, what happens when you request French?
http://localhost:5000/?culture=fr
... you're saying that it loads en-US?
Also note that your code doesn't match the approach recommended by the topic. Try the pattern shown in :point_right: https://docs.microsoft.com/aspnet/core/fundamentals/localization?view=aspnetcore-2.1#localization-middleware and remove the .Configure<RequestLocalizationOptions>( ... ); from Startup.ConfigureServices (which is shown in the topic as an approach for a custom provider). It might make no difference, of course, but I'd rule it out as a possible cause.
Hi Guardrex, thank you for your attention,
My main solution is : i want to have 3 languages for my website and i want when website opened default language be fa-IR, if user was from another country he/she can choose en-US or ar-SA for changing website language.
can i prepare it with cultures of is there any other ways for this solution???
I'm still a bit confused ...
i want when website opened default language be fa-IR
You want the language to be fa-IR no matter what the client requests? ... even if the client requests en-US or ar-SA? If so, you'll need a custom provider for that.
"Default" (in the context of what the framework does) means that the framework falls back to a "default culture" if a provider can't match what the client is requesting. The framework is built around the concept that if the client makes a request and the culture is supported by the app that the app serves that culture. The framework's default behavior _isn't_ built around the concept that the client requests a culture that the app supports but the app _serves a different culture anyway_. That's something that can be done, of course, but you'll have to code it using a custom provider.
The selection of a culture thereafter is covered in Set the culture programmatically, where you intend to give users a choice of supported cultures after they receive a response and then have the app honor that setting every request thereafter.
In addition to what the topic shows, see if bloggers have written about similar approaches. Search Stack Overflow for similar scenarios. If you want to talk to devs in chat, try Slack and Gitter.
... keeping in mind that the code you posted :point_up: doesn't exactly match the topic in the base case. I suggest getting the base case (en-US, ar-SA, and fa-IR supported with fa-IR as the default) to work first following exactly the approach shown in the topic. Then, expand that using the approach shown in the Set the culture programmatically section to allow the user to choose the culture after fa-IR has loaded for unsupported cultures.
WRT the topic. There are over a dozen issues associated with enhancing the topic. I'm not sure if any of them ask for exactly what you're requesting here. Thus far, it sounds like the pieces of what you need are there ... you'll need to wire them together for your scenario to work.
Thanks guardrex, I got it