【问题标题】:How to add new language to ABP template?如何向 ABP 模板添加新语言?
【发布时间】:2018-12-20 16:58:33
【问题描述】:

我正在使用来自该站点https://aspnetboilerplate.com/Templates的免费样板文件(ASP.NET Core MVC 和 jQuery)

是否可以添加新的语言支持?

我已经添加了本地化的 .xml 文件,更新了数据库中的“abplanguages”表,但它不起作用。我正在更改语言,但文本仍然是英文。与“espanol-mexico”等样板已附带的预定义语言的情况相同,但当我选择“法语”时,页面被翻译。 这很奇怪,因为在文档中说可以做到。 https://aspnetboilerplate.com/Pages/Documents/Localization#extending-localization-sources

不知道是免费模板限制吗?

【问题讨论】:

    标签: asp.net-core aspnetboilerplate


    【解决方案1】:

    注入IApplicationLanguageManager接口并使用AddAsync()方法添加新语言。

    private readonly IApplicationLanguageManager _applicationLanguageManager;
    
    
    public LanguageAppService(
        IApplicationLanguageManager applicationLanguageManager,
        IApplicationLanguageTextManager applicationLanguageTextManager,
        IRepository<ApplicationLanguage> languageRepository)
    {
        _applicationLanguageManager = applicationLanguageManager;
        _languageRepository = languageRepository;
        _applicationLanguageTextManager = applicationLanguageTextManager;
    }
    
    
    protected virtual async Task CreateLanguageAsync(ApplicationLanguageEditDto input)
    {
        if (AbpSession.MultiTenancySide != MultiTenancySides.Host)
        {
            throw new UserFriendlyException(L("TenantsCannotCreateLanguage"));
        }
    
        var culture = CultureHelper.GetCultureInfoByChecking(input.Name);
    
        await _applicationLanguageManager.AddAsync(
            new ApplicationLanguage(
                AbpSession.TenantId,
                culture.Name,
                culture.DisplayName,
                input.Icon
            )
            {
                IsDisabled = !input.IsEnabled
            }
        );
    }
    
    public static class CultureHelper
    {
        public static CultureInfo[] AllCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
    
        public static bool IsRtl => CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
    
        public static bool UsingLunarCalendar = CultureInfo.CurrentUICulture.DateTimeFormat.Calendar.AlgorithmType == CalendarAlgorithmType.LunarCalendar;
    
        public static CultureInfo GetCultureInfoByChecking(string name)
        {
            try
            {
                return CultureInfo.GetCultureInfo(name);
            }
            catch (CultureNotFoundException)
            {
                return CultureInfo.CurrentCulture;
            }
        }
    }
    
    
    public class ApplicationLanguageEditDto
    {
        public virtual int? Id { get; set; }
    
        [Required]
        [StringLength(ApplicationLanguage.MaxNameLength)]
        public virtual string Name { get; set; }
    
        [StringLength(ApplicationLanguage.MaxIconLength)]
        public virtual string Icon { get; set; }
    
        /// <summary>
        /// Mapped from Language.IsDisabled with using manual mapping in CustomDtoMapper.cs
        /// </summary>
        public bool IsEnabled { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      我想通了。就我而言,这是不正确的构建操作属性。在 VS 中右键单击本地化源文件:*.xml file -&gt; Advanced -&gt; Build action: Embedded resource

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2019-10-16
        • 1970-01-01
        • 2019-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多