本地化模块

ABP的本地化系统与Microsoft.Extensions.Localization无缝集成,并与AspnetCore的本地化文档兼容. 它添加了一些实用功能和增强功能, 使其更易于在实际开发中应用.

Volo.Abp.Localization是本地化系统的核心包.

模块的定义
使用AbpStringLocalizerFactory替换了IStringLocalizerFactory,增加单例ResourceManagerStringLocalizerFactory,虚拟文件定义,本地化配置文件

本地化资源用于将相关的本地化字符串组合在一起,并将它们与应用程序的其他本地化字符串分开.通常一个模块会定义自己的本地化资源. 本地化资源就是一个普通的类. 例如:

  • 添加了一个新的本地化资源, 使用"en"(英语)作为默认的本地化.
  • 用JSON文件存储本地化字符串.
  • 使用虚拟文件系统 将JSON文件嵌入到程序集中.
[DependsOn(
        typeof(AbpVirtualFileSystemModule),
        typeof(AbpSettingsModule),
        typeof(AbpLocalizationAbstractionsModule)
        )]
    public class AbpLocalizationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            AbpStringLocalizerFactory.Replace(context.Services);

            Configure(options =>
            {
                options.FileSets.AddEmbedded("Volo.Abp", "Volo/Abp");
            });

            Configure(options =>
            {
                options
                    .Resources
                    .Add("en");

                options
                    .Resources
                    .Add("en")
                    .AddVirtualJson("/Localization/Resources/AbpValidation");
            });
        }
    }

 配置Options有三个集合,本地化资源LocalizationResource,资源贡献者ILocalizationResourceContributor,语言种类LanguageInfo

 public class AbpLocalizationOptions
    {
        public LocalizationResourceDictionary Resources { get; }

        public ITypeList GlobalContributors { get; }

        public List Languages { get; }

        public AbpLocalizationOptions()
        {
            Resources = new LocalizationResourceDictionary();
            GlobalContributors = new TypeList();
            Languages = new List();
        }
    }

AbpCultureHelper.Use,设置当前的语言,同设置租户功能差不多

public static IDisposable Use([NotNull] CultureInfo culture, CultureInfo uiCulture = null)
        {
            Check.NotNull(culture, nameof(culture));

            var currentCulture = CultureInfo.CurrentCulture;
            var currentUiCulture = CultureInfo.CurrentUICulture;

            CultureInfo.CurrentCulture = culture;
            CultureInfo.CurrentUICulture = uiCulture ?? culture;

            return new DisposeAction(() =>
            {
                CultureInfo.CurrentCulture = currentCulture;
                CultureInfo.CurrentUICulture = currentUiCulture;
            });
        }

2、获取本地化文本,在服务端获取本地化文本的用法是非常标准的(它与AspNetCore提供的获取本地化资源方式无缝集成).

本地化字符串访问器IStringLocalizerIStringLocalizer 是一个实现了 IEnumerable 的简单接口并且拥有索引器来来返回本地化的字符串。

AbpDictionaryBasedStringLocalizer,如_localizer["About Title"] 没有发现 “About Title” 的本地化值,则索引的键值被返回

 

3、中间件

        public static IApplicationBuilder UseAbpRequestLocalization(this IApplicationBuilder app)
        {
            IReadOnlyList languages;
            string defaultLanguage;

            using (var scope = app.ApplicationServices.CreateScope())
            {
                var languageProvider = scope.ServiceProvider.GetRequiredService();
                languages = languageProvider.GetLanguages();

                var settingProvider = scope.ServiceProvider.GetRequiredService();
                defaultLanguage = settingProvider.GetOrNull(LocalizationSettingNames.DefaultLanguage);
            }

            if (!languages.Any())
            {
                return app.UseRequestLocalization();
            }

            var options = new RequestLocalizationOptions
            {
                DefaultRequestCulture = DefaultGetRequestCulture(defaultLanguage, languages),

                SupportedCultures = languages
                    .Select(l => l.CultureName)
                    .Distinct()
                    .Select(c => new CultureInfo(c))
                    .ToArray(),

                SupportedUICultures = languages
                    .Select(l => l.UiCultureName)
                    .Distinct()
                    .Select(c => new CultureInfo(c))
                    .ToArray()
            };
            
            return app.UseRequestLocalization(options);
        }

 FixedLocalizableString

public class FixedLocalizableString : ILocalizableString
    {
        public string Value { get; }

        public FixedLocalizableString(string value)
        {
            Value = value;
        }

        public LocalizedString Localize(IStringLocalizerFactory stringLocalizerFactory)
        {
            return new LocalizedString(Value, Value);
        }
    }

 

客户端:ABP提供了JavaScript服务, 可以在客户端使用相同的本地化文本. 

var testResource = abp.localization.getResource('Test');
var str = testResource('HelloWorld');

 

你可能感兴趣的:(本地化模块)