Abp VNext 本地化Location

Abp VNext 本地化Location,第1张

1、新建Common类库

CommonModule.cs

 [DependsOn(
        typeof(AbpLocalizationModule),
        typeof(AbpVirtualFileSystemModule)
        )]
    public class CommonModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure(options =>
            {
                options.FileSets.AddEmbedded(baseNamespace: "Common");
            });

            Configure(options =>
            {
                options.Resources
                    .Add("en") //默认语言
                    .AddBaseTypes(typeof(AbpValidationResource))
                    .AddVirtualJson("/Localization/Resources");
                options.DefaultResourceType = typeof(Langue);
            });
            Configure(options =>
            {
                options.MapCodeNamespace("Langue", typeof(Langue));
            });
        }
    }
}

Langue.cs

[LocalizationResourceName("Langue")]
    public class Langue
    {
    }

en.json

{
  "culture": "en",//语种
  "texts": {
    "HelloWorld": "Hello aaa!"
  }
}

zh.json

{
  "culture": "zh",
  "texts": {
    "HelloWorld": "你好啊!"
  }
}

将文件配置为嵌入式,修改文件Common.csproj

  
    
    
  

二、WebApi项目中配置

[DependsOn(
        typeof(AbpLocalizationModule),
        typeof(CommonModule)
        )]
    public class WebApiModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            ConfigureLocalization();
        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            app.UseAbpRequestLocalization();
        }
        private void ConfigureLocalization()
        {
            Configure(options =>
            {
                options.Languages.Add(new LanguageInfo("en", "en", "English"));
                options.Languages.Add(new LanguageInfo("zh", "zh", "简体中文"));
            });
        }
    }

三、依赖注入方式使用

[Route("User")]
public class UserService : ApplicationService, IUserService
{
        private readonly IStringLocalizer _localizer;
        public UserService(IStringLocalizer localizer)
        {
            _localizer = localizer;
        }
        [AllowAnonymous]
        [HttpGet("Login")]
        public string Login(string userName, string pwd)
        {
            var hello = _localizer["HelloWorld"];
            return hello.Value;
        }
}

四、客户端请求带上header

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/919127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存