Base solution for your next web application
Open Closed

Problem to Get localized string from Core project #2271


User avatar
0
jeromevoxteneo created

Hello,

I'm trying to make my custom attribute that extend DisplayNameAttribute to make it localized. But I cannot get the value from the languageTextManager nor from the localizationManager.

Here is my attribute for LocalizedDisplayName

using System;
using System.ComponentModel;
using System.Globalization;
using Abp.Dependency;
using Abp.Localization;
using Abp.Runtime.Session;

namespace iExpertise.Localization
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Method, AllowMultiple = false)]
    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        private IApplicationLanguageTextManager _languageTextManager;
        private ILocalizationManager _localizationManager;
        //private IAbpSession _abpSession;
        private string _originalName;
        public LocalizedDisplayNameAttribute(string displayName)
            : base()
        {
            _originalName = displayName;
        }

        public override string DisplayName
        {
            get
            {
                if (_languageTextManager == null)
                {
                    _languageTextManager = IocManager.Instance.Resolve<IApplicationLanguageTextManager>();
                    _localizationManager = IocManager.Instance.Resolve<ILocalizationManager>();
                    //_abpSession = IocManager.Instance.Resolve<IAbpSession>();
                }
                var translation = _localizationManager.GetString(iExpertiseConsts.LocalizationSourceName,
                    _originalName);
                var translation2 = _languageTextManager.GetStringOrNull(null, iExpertiseConsts.LocalizationSourceName,
                    CultureInfo.CurrentCulture, _originalName);
                return translation;
            }
        }
    }
}

I put a breakpoint at the return translation and it results that: translation is [Task type id_Label] translation2 is null

I wanted to be sure that my key that was added in the xml file was ok, so I navigated to the languages list and see my entry and put a breakpoint inside of the EditTextModal of LanguagesController, the translation here is ok.

In PreInitialize method of Core module I have:

//Add/remove localization sources
            Configuration.Localization.Sources.Add(
                new DictionaryBasedLocalizationSource(
                    iExpertiseConsts.LocalizationSourceName,
                    new XmlEmbeddedFileLocalizationDictionaryProvider(
                        Assembly.GetExecutingAssembly(),
                        "iExpertise.Localization.iExpertise"
                        )
                    )
                );

I cannot understand why my localization is ok in LanguagesController and not in my custom attribute in Core project.

Thanks for help


4 Answer(s)
  • User Avatar
    0
    jeromevoxteneo created

    any idea ?

  • User Avatar
    0
    hikalkan created
    Support Team

    Your code seems correct. We had actually done something similar:

    public class AbpDisplayNameAttribute : DisplayNameAttribute
        {
            public override string DisplayName => LocalizationHelper.GetString(SourceName, Key);
    
            public string SourceName { get; set; }
            public string Key { get; set; }
    
            public AbpDisplayNameAttribute(string sourceName, string key)
            {
                SourceName = sourceName;
                Key = key;
            }
        }
    

    And this properly works and it's already included in Abp framework. To compare, can you try to use AbpDisplayNameAttribute instead of your own attribute?

  • User Avatar
    0
    jeromevoxteneo created

    Yeah didn't know about this existing attribute

    It's working, I was tired and set a wrong key in the attribute key value :oops:

    Do you have similar attribute for required and string length attributes that can be localized ?

    Thank for the help

  • User Avatar
    0
    hikalkan created
    Support Team

    No, because we had problem with localizing validation attributes. Created an issue for that: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1360">https://github.com/aspnetboilerplate/as ... ssues/1360</a>