如何向ABP模板添加新语言?

时间:2018-12-20 16:58:34

标签: asp.net-core aspnetboilerplate

我正在使用此网站https://aspnetboilerplate.com/Templates上的免费样板(ASP.NET Core MVC和jQuery)

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

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

我想知道这是免费模板限制吗?

2 个答案:

答案 0 :(得分: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; }
}

答案 1 :(得分:0)

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

相关问题