Asp.Net中的全球化

问题描述

我用Asp.Net Web-PageSelectBox一起写了语言。这些语言存储在{​​{1}}(1个文件/语言/页)中。一切正常,唯一不起作用的是当我更改页面时必须重新选择语言。设置Cookie后,我在哪里保存Cookie以及如何设置语言?

Startup.cs(剪切)

Ressource Files

我的ViewComponent

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

            services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new[]
                 {
                    new CultureInfo("de"),new CultureInfo("it"),new CultureInfo("en")
                 };
                options.DefaultRequestCulture = new RequestCulture("de");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });

            services.AddLocalization(options => options.ResourcesPath = "Resources");

            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();
        }

  var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
            app.UseRequestLocalization(localizationOptions);

我的模型用于SelectBox填充

  public class CultureSwitcherViewComponent : ViewComponent
    {
        private readonly IOptions<RequestLocalizationOptions> localizationOptions;

        public CultureSwitcherViewComponent(IOptions<RequestLocalizationOptions> localizationOptions) =>
            this.localizationOptions = localizationOptions;

        public IViewComponentResult Invoke()
        {
            var cultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
            var model = new CultureSwitcherModel
            {
                SupportedCultures = localizationOptions.Value.SupportedUICultures.ToList(),CurrentUICulture = cultureFeature.RequestCulture.UICulture
            };

            return View(model);
        }
    }

我的SelectBox充满了值(.cshtml)

    public class CultureSwitcherModel
    {
        public CultureInfo CurrentUICulture { get; set; }
        public List<CultureInfo> SupportedCultures { get; set; }
    }

_Layout.cshtml的导航栏

@model CultureSwitcherModel

<div>
    <form id="culture-switcher">
        <select class="form-control bg-dark text-white" name="culture" id="culture-options">
            <option class="text-black">DE/IT/EN</option>
            @foreach (var culture in Model.SupportedCultures)
            {
                <option class="text-black border-none" value="@culture.Name" selected="@(Model.CurrentUICulture.Name == culture.Name)">@culture.DisplayName</option>
            }
        </select>
    </form>
</div>

<script>
    document.getElementById("culture-options").addEventListener("change",() => {
        document.getElementById("culture-switcher").submit();
    });
</script>

我是从本教程ASP.Net Globalization in Razor Pages

中摘录的

我如何才能获得所选语言的保存时间,以便用户不必一直选择所需的语言?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...