Open Closed

Using Host Language Texts for Tenants #2300


User avatar
0
jackmcelhinney created
  • ABP Framework version: v4.4.4
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Hello. We have removed the Languages and Language Texts features from the tenants by changing the MultiTenancySides to Host on these permissions. However, we'd like to use the UI in the host to edit the language texts and have these changes apply to all tenants. Currently, it seems changing language texts in the host only changes them in for users logged into the host. Is there a way to override so all tenants will use language texts defined in the host?

Thanks!


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to always switch to host in DynamicLocalizationResourceContributor

    
    public class DynamicLocalizationResourceContributor : ILocalizationResourceContributor
    {
        protected LocalizationResource Resource;
        protected IDynamicResourceLocalizer DynamicResourceLocalizer;
    
        public void Initialize(LocalizationResourceInitializationContext context)
        {
            Resource = context.Resource;
            DynamicResourceLocalizer = context.ServiceProvider.GetRequiredService<IDynamicResourceLocalizer>();
        }
    
        public LocalizedString GetOrNull(string cultureName, string name)
        {
            using (CurrentTenant.Change(null))
            {
                return DynamicResourceLocalizer.GetOrNull(Resource, cultureName, name);
            }
        }
    
        public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary)
        {
            using (CurrentTenant.Change(null))
            {
                DynamicResourceLocalizer.Fill(Resource, cultureName, dictionary);
            }
        }
    }
    
    
    
  • User Avatar
    0
    jackmcelhinney created

    Hi maliming,

    I've made the following changes but edits to the language texts still only apply in host.

    Added DynamicLocalizationResourceContributor (had to get CurrentTenant because the class doesn't extend something with it already)

    public class DynamicLocalizationResourceContributor : ILocalizationResourceContributor
    {
        protected LocalizationResource Resource;
        protected IDynamicResourceLocalizer DynamicResourceLocalizer;
        protected ICurrentTenant CurrentTenant;
    
        public void Initialize(LocalizationResourceInitializationContext context)
        {
            Resource = context.Resource;
            DynamicResourceLocalizer = context.ServiceProvider.GetRequiredService<IDynamicResourceLocalizer>();
            CurrentTenant = context.ServiceProvider.GetRequiredService<ICurrentTenant>();
        }
    
        public LocalizedString GetOrNull(string cultureName, string name)
        {
            using (CurrentTenant.Change(null))
            {
                return DynamicResourceLocalizer.GetOrNull(Resource, cultureName, name);
            }
        }
    
        public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary)
        {
            using (CurrentTenant.Change(null))
            {
                DynamicResourceLocalizer.Fill(Resource, cultureName, dictionary);
            }
        }
    }
    

    Added the contributor in the HostModule:

    Configure<LocalizationResource>(options =>
    {
        options.Contributors.Clear();
        options.Contributors.Add(new DynamicLocalizationResourceContributor());
    });
    

    Any other suggestions?

    Thanks!

  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    Hi

    How about this?

    https://support.abp.io/QA/Questions/1642/Is-there-a-way-to-bypass-Tenant-for-Translations#answer-eea69656-8bb0-af79-e8fb-39fe16c17820

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38