如何使用 jquery 更改 CurrentCulture

Posted

技术标签:

【中文标题】如何使用 jquery 更改 CurrentCulture【英文标题】:How to change the CurrentCulture with jquery 【发布时间】:2021-02-18 14:05:28 【问题描述】:

我有一个 jquery 函数,可以检测下拉列表中更改的项目我想使用 jquery 更改 CurrentCulture 以更改 .net core 中的 LocalizatedValue ,这是包含我的试用版的 jquery 方法

    $('#dropdownList li').find("a").click(function () 

        $('#dropdown-button').html($(this).html());
        $("#dropdown-button").val($(this).text());

        
        
        
        if (document.getElementById('dropdown-button').innerText === "Arab") 
            Globalization.locale("ar")
         else if (document.getElementById('dropdown-button').innerText === "French") 
            Globalization.locale("fr")

         else if (document.getElementById('dropdown-button').innerText === "English") 
            Globalization.locale("en")
        
    );
); 

那里我需要cs代码中的当前文化信息

 private string GetString(string name)
        
            var query = localization.Where(l => l.LocalizedValue.Keys.Any(lv => lv == CultureInfo.CurrentCulture.Name));
            var value = query.FirstOrDefault(l => l.Key == name);
            return value.LocalizedValue[CultureInfo.CurrentCulture.Name];
        

【问题讨论】:

嗨@Mahdi Guesmi,我的回答是否帮助您解决了您的问题?如果可以,请您接受作为答案吗?如果没有,请跟进让我知道。 【参考方案1】:

这是一个基于official docs的工作演示:

1.在根项目中创建SharedResource.cs

public class SharedResource


2.在Resources文件夹中创建SharedResource.fr.resx添加资源。Resources文件夹在根项目中。

3.查看:

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Localization
@using Microsoft.Extensions.Options
@inject IStringLocalizer<SharedResource> SharedLocalizer
@inject IOptions<RequestLocalizationOptions> LocOptions

@
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem  Value = c.Name, Text = c.DisplayName )
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~Context.Request.Path.Value";


<div title="@SharedLocalizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@SharedLocalizer["Language:"]</label>
        <select name="culture" onchange="this.form.submit();"
                asp-for="@requestCulture.RequestCulture.UICulture.Name"
                asp-items="cultureItems">
        </select>
    </form>
</div>
<h1>@SharedLocalizer["Home Page"]</h1>

4.HomeController:

public class HomeController : Controller

    [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);
    

5.Startup.cs:

public void ConfigureServices(IServiceCollection services)

    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.AddControllersWithViews()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();          
    services.Configure<RequestLocalizationOptions>(options =>
    
        var supportedCultures = new[]
        
            new CultureInfo("en-US"),
            new CultureInfo("fr")
        ;

            options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"),new CultureInfo("en-US"));
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
    );



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);
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "controller=Home/action=Index/id?");
           
    );

结果:

【讨论】:

以上是关于如何使用 jquery 更改 CurrentCulture的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 jQuery 更改超链接的 href 属性

如何使用 jQuery 更改超链接的 href? [复制]

如何更改 jQuery 的命名空间?

如何使用jQuery触发类更改事件?

如何使用 jquery 更改模式内的文本?

Jquery地址插件:如何避免历史更改?