Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery更改CurrentCulture_Jquery_Asp.net Core_Jquery Plugins_Globalization_Jquery Globalization - Fatal编程技术网

如何使用jquery更改CurrentCulture

如何使用jquery更改CurrentCulture,jquery,asp.net-core,jquery-plugins,globalization,jquery-globalization,Jquery,Asp.net Core,Jquery Plugins,Globalization,Jquery Globalization,我有一个jquery函数,可以检测下拉列表中的更改项。我想使用jquery更改CurrentCulture,以便更改.net core中的LocalizatedValue,这是包含我的试用版的jquery方法 $('#dropdownList li').find("a").click(function () { $('#dropdown-button').html($(this).html()); $("#dropdown-

我有一个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];
        }

以下是基于以下内容的工作演示:

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

public class SharedResource
{
}
2.在
Resources
文件夹中创建
SharedResource.fr.resx
,以添加资源。
Resources
文件夹位于根项目中

3.意见:

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?}");
           
    });
}
public void配置服务(IServiceCollection服务)
{
services.AddLocalization(options=>options.ResourcesPath=“Resources”);
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.后缀)
.AddDataAnnotationsLocalization();
配置(选项=>
{
var supportedCultures=new[]
{
新文化信息(“美国”),
新文化信息(“fr”)
};
options.DefaultRequestCulture=newrequestculture(new-CultureInfo(“en-US”)、new-CultureInfo(“en-US”);
options.SupportedCultures=SupportedCultures;
options.supportedCultures=supportedCultures;
});
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
var-supportedCultures=new[]{“en-US”,“fr”};
var localizationOptions=new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.添加支持教育(支持文化);
app.UseRequestLocalization(localizationOptions);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
}
结果:


Hi@Mahdi Guemi,我的回答有助于您解决问题吗?如果有,请接受我的回答。如果没有,请跟进让我知道。
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);
    }
}
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?}");
           
    });
}