Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
Hello everyone I am working on the project that I need to use multiple language and then I want to set default culture. Everything is working fine but I couldn't set default culture. Project is starting with default English language, even though I changed it to Russian language as I showed code below. Please help me thank you.


What I have tried:

Quote:
Program.cs

  using AnalysisDashboard.Authentication;
using AnalysisDashboard.Data;   
using AnalysisDashboard.DataAccess;
using AnalysisDashboard.Models;
using AnalysisDashboard.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<DashboardContext>(options =>
{
    options.UseMySQL(builder.Configuration.GetConnectionString("Default"));
});

// Add services to the container.
builder.Services.AddAuthenticationCore();
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<ProtectedSessionStorage>();
builder.Services.AddScoped<AuthenticationStateProvider, 
CustomAuthenticationStateProvider>();
builder.Services.AddSingleton<UserAccountService>();
builder.Services.AddScoped<IRegistrationService, RegistrationService>();
builder.Services.AddScoped<DataInfo>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
  app.UseExceptionHandler("/Error");
  app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

var supportedCultures = new[] { "en-US", "uz-UZ", "uz-Cyrl", "ru-RU" };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture(supportedCultures[3])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);

app.UseRequestLocalization(localizationOptions); 

app.UseRouting();

app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();</blockquote>


Index.razor.cs file

public void ClickLanguage(string lanCode)
    {
        string selectedCulture = Thread.CurrentThread.CurrentCulture.Name;
        switch (lanCode)
        {
            case "uz":      selectedCulture = "uz-UZ";   break;
            case "uzCyrl":  selectedCulture = "uz-Cyrl"; break;
            case "ru":      selectedCulture = "ru-RU";   break;
            case "en":      selectedCulture = "en-US";   break;
        }

        if (string.IsNullOrWhiteSpace(selectedCulture))
        {
            return;
        }

        var uri = new Uri(Navigation.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
        var query = $"?culture={Uri.EscapeDataString(selectedCulture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}";

        Navigation.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); 
    }</blockquote>


CultureController.cs file

using Microsoft.AspNetCore.Http;
   using Microsoft.AspNetCore.Localization;
   using Microsoft.AspNetCore.Mvc;

  namespace AnalysisDashboard.Controllers
  {
      [Route("[controller]/[action]")]
      public class CultureController : Controller
      {
           public IActionResult SetCulture(string culture, string redirectUri)
           {
              if (culture != null)
              {
                  HttpContext.Response.Cookies.Append(
                         CookieRequestCultureProvider.DefaultCookieName,
                         CookieRequestCultureProvider.MakeCookieValue(
                         new RequestCulture(culture)));
              }

               return LocalRedirect(redirectUri);
           }
      }
   }
Posted
Updated 20-Jul-23 21:33pm

1 solution

There are many tutorials and solutions available on the internet, you just need to look. I searched YoutTube, based on you can see the code and how it runs: blazor server change cultre[^]

Here are a couple of clear solutions:
* Localization in Blazor Server - How To Change Languages and Cultures - YouTube[^]
* Localization in Blazor: Carl Franklin's Blazor Train Ep 90 - YouTube[^]

These guys are pros when it comes to Blazor. You will find your answer there.
 
Share this answer
 
v2
Comments
Member 14526095 21-Jul-23 4:06am    
Thank you. I already watched the link which you posted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900