ASP.NET表单的核心多语言验证消息 [英] ASP.Net core multilingual validation messages for forms

查看:55
本文介绍了ASP.NET表单的核心多语言验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个WebForm人员,我想转到ASP.NET Core&;Blazor。我在MVC方面没有任何背景,以防有人根据MVC给出他们的反馈。

我只对ASP.NET Core Razor页感兴趣。

在Web表单中,我为多语言网站使用两个文件,例如,如果需要,我可以将相关的验证消息保存在.aspx页面或相关的js文件中,而在Core中它的做法不同。在WebForm中,我可以将验证消息保存在.aspx文件本身中,而在ASP.NET核心中,我一直使用单个Model类,验证消息在那里定义。

WebForm结构

-en
--index
--aboutus
..
..
-ar
--index
--aboutus

ASP.NET核心文件夹结构

Pages
-en
--index
--aboutus
..
..
-ar
--index
--aboutus
假设在Pages文件夹下,我创建了两个文件夹,一个用于英语,另一个用于阿拉伯语,在Core中,我们假设我已经在Model file中定义了我的验证。由于我有两种语言的一个模型文件,如何显示特定于语言的验证消息

以下代码仅为示例

using System;
using System.ComponentModel.DataAnnotations;

public class Starship
{
    [Required]
    [StringLength(16,
        ErrorMessage = "Identifier too long (16 character limit).")]
    public string Identifier { get; set; }

    public string Description { get; set; }

    [Required]
    public string Classification { get; set; }

    [Range(1, 100000,
        ErrorMessage = "Accommodation invalid (1-100000).")]
    public int MaximumAccommodation { get; set; }

    [Required]
    [Range(typeof(bool), "true", "true",
        ErrorMessage = "This form disallows unapproved ships.")]
    public bool IsValidatedDesign { get; set; }

    [Required]
    public DateTime ProductionDate { get; set; }
}

我面临的问题是,因为我有一个英文模型文件,所以如何在ASP.NET核心中以最简单和最简单的方式显示阿拉伯语验证

假设我的URL类似

www.example.com/en/ 
www.example.com/en/aboutus/ 
www.example.com/en/contact/

www.example.com/ar/ 
www.example.com/ar/aboutus/ 
www.example.com/ar/contact/
仅根据上述URL显示基于语言的验证消息,而不对具有任何形式的网站页面使用任何全球化功能。

推荐答案

是否可以仅基于上述URL显示基于语言的验证消息,而无需对具有任何表单等的网站页面使用任何全球化功能。
是,可以参照路由中的区域性参数显示验证消息。但为了使本地化正常工作,您需要进行一些设置。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddDataAnnotationsLocalization(options => {
            options.DataAnnotationLocalizerProvider = (type, factory) =>
                factory.Create(typeof(SharedResource));
        });
}

www.Example.com/ar/Contact/

查看URL,您需要路由数据请求区域性提供程序。默认情况下,ASP.NET Core使用QueryString、Cookie和AccpetLanguageHeader区域性提供程序(有关详细信息,请参阅docs),因此如果您希望使用路由值进行本地化,则需要设置路由数据区域性提供程序:

services.Configure<RequestLocalizationOptions>(ops =>
{
    ops.DefaultRequestCulture = new RequestCulture("en");
    ops.SupportedCultures = mySupportedCultures;
    ops.SupportedUICultures = mySupportedUICultures;

    // add RouteDataRequestCultureProvider to the beginning of the providers list. 
    ops.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider(cultures));
});

但这还不是全部!要正确本地化ASP.NET核心Web应用程序,还需要考虑许多其他主题:

  • 查看本地化
  • DataAnnotation本地化
  • 模型绑定错误本地化
  • 标识描述符错误本地化
  • 自定义后端消息本地化
  • 客户端验证消息本地化

您可以阅读Asp.Net-Core globalization and localization的官方文档。

另外,我写了一些文章详细描述本地化,请参阅Developing Multicultural Web Application


如何在ASP.NET核心中以最简单的方式用阿拉伯语显示验证

如果您正在寻找一种可以使用LazZiya.ExpressLocalizationNuget包的快捷方式,它为所有本地化设置提供了非常简单的方式:

//add reference to :
using LazZiya.ExpressLocalization;

//setup express localization under ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
    //other configuration settings....

    var cultures = new CultureInfo[]
    {
        new CultureInfo("en"),
        new CultureInfo("tr"),
        new CultureInfo("ar")
    };

    services.AddRazorPages()
        //ExpressLocalizationResource and ViewLocalizationResource are available in :
        // https://github.com/LazZiya/ExpressLocalizationSample
        .AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource>(
            exOps =>
            {
                exOps.ResourcesPath = "LocalizationResources";
                exOps.RequestLocalizationOptions = ops =>
                {
                    ops.SupportedCultures = cultures;
                    ops.SupportedUICultures = cultures;
                    ops.DefaultRequestCulture = new RequestCulture("en");
                };
            });
}

这几乎就是设置"快速"本地化所需的全部内容。您可以在此处找到step by step tutorial for using ExpressLocalizationsample github repository

这篇关于ASP.NET表单的核心多语言验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆