question

davidbuckley-4911 avatar image
0 Votes"
davidbuckley-4911 asked YihuiSun-MSFT answered

Setting Currency independent of culture

I am having a futher issue here. In asp.net 5 and mvc

I want to have a dropdown with currency the part of selection and identifying which works

 [HttpPost]
 public IActionResult SetCurrency(IFormCollection forms,string returnUrl)
 {
     string storeLocale = forms["customerCurrency"].ToString();
    
     var record = _context.Currencies.Where(w => w.DisplayLocale == storeLocale).FirstOrDefault();
    
     var currentLanguageId = _context.Appsettings.Where(w => w.Key == Constants.FrontEndDefaultLanguageId).FirstOrDefault();
     if (record != null)
     {
         _config[Constants.FrontEndDefaultLanguageId] = record.Id.ToString();
         currentLanguageId.Value = record.Id.ToString();
         _context.SaveChanges();
         _toast.AddAlertToastMessage("Currecy changed to " + record.Name); 
     }           
            
     return LocalRedirect(returnUrl);
 }

For my culture I am doing this which is control by the language dropdown

 [HttpPost]
 public IActionResult SetCulture(string culture, string returnUrl)
 {
     var record = _context.Appsettings.Where(w => w.Key == Constants.FrontEndDefaultLanguageId).FirstOrDefault();
         if (culture == "en")
             record.Value = "1";
    
         if (culture == "fr")
             record.Value = "2";
         _context.SaveChangesAsync();
         _toast.AddSuccessToastMessage("Language changed to :" + record.Key);
    
         Response.Cookies.Append(
             CookieRequestCultureProvider.DefaultCookieName,
             CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
             new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
         );
         return LocalRedirect(returnUrl);
  }

I don't want SetCurrency to override what has been set in the cookie but i want it to set if it would use the euro pound symbol etc so I think I need Number Info but am not sure how I would set it in this case.

Buy this i mean the

£ or the Euro symbol at present another square symbol is showing up even though my culture is set to fr-fr for france

dotnet-csharpdotnet-aspnet-core-mvcdotnet-aspnet-mvc
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @davidbuckley-4911,
Do you want to implement a strategy to select the language/culture for each request?If so, I suggest you click this link to see the solution.

  • Suppose you want to let your customers store their language and culture in your databases. You could write a provider to look up these values for the user.

0 Votes 0 ·

I would rather this not be something that the user has to configure I want it to act like how shopify and most other sites would act without any user intervention in the currency bar the dropdown.

And the example u show shows nothing about currency only the langauge

0 Votes 0 ·

1 Answer

YihuiSun-MSFT avatar image
0 Votes"
YihuiSun-MSFT answered

Hi @davidbuckley-4911,

I would rather this not be something that the user has to configure I want it to act like how shopify and most other sites would act without any user intervention in the currency bar the dropdown.

Can I understand it as follows:

  • Do you want to change the Currency automatically in a different culture?

If so, you can follow the link I provided earlier. When you change the culture, the Currency changes. You can also refer to the code I tested below.

Startup

 public void ConfigureServices(IServiceCollection services)
             {
                ... ...
                 services.AddLocalization(options => options.ResourcesPath = "Resources");
                 services.Configure<RequestLocalizationOptions>(options =>
                 {
                     var supportedCultures = new[]
                     {
                         new CultureInfo("en-US"),
                         new CultureInfo("fr")
                      };
                     options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
                     options.SupportedCultures = supportedCultures;
                     options.SupportedUICultures = supportedCultures;
                     options.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(async context =>
                     {
                         // My custom request culture logic
                         return new ProviderCultureResult("en");
                     }));
                 });
                 services.AddMvc()
                     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                     .AddDataAnnotationsLocalization();
             }
             // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
             public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
             {
                 ... ...
                 var supportedCultures = new[] { "en-US", "fr" };
                 var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
                     .AddSupportedCultures(supportedCultures)
                     .AddSupportedUICultures(supportedCultures);
        
                 app.UseRequestLocalization(localizationOptions);
        
                  ... ...
             }

Controller

 public class CurrencyController : Controller
         {
             public IActionResult Index()
             {
                 return View();
             }
             [HttpPost]
             public IActionResult SetLanguage(string culture, string returnUrl)
             {
                 Response.Cookies.Append(
                     CookieRequestCultureProvider.DefaultCookieName,
                     CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                     new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
                 );
        
                 return LocalRedirect(returnUrl);
             }
         }

View

105802-77.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
YihuiSun


view.txt (1.4 KiB)
77.gif (100.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.