Open Closed

Tenant-independent localization with Volo.LanguageManagement #6636


User avatar
0
lba created
  • ABP Framework version: v7.2.2
  • UI Type: Blazor Server
  • Database System: EF Core
  • Steps to reproduce the issue:
    • Using Volo.LanguageManagement, change value for the key of a text
    • Login with tenant

When using Volo.LanguageManagement to update texts, the changes are not reflected when switching between tenants.

Example We want to change the text from "New Project" in the header to "hello"

Upon saving, the text was changed without any issues.

However, when logging in with a tenant, the header still displays "New Project", as the value appears unchanged in the language management module.

How can we use the language management module for tenant-independent localization?


4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    This is because the LanguageText is a multi-tenant entity.

    You can try this:

    [ExposeServices(typeof(DynamicResourceLocalizer), typeof(IDynamicResourceLocalizer))]
    public class MyDynamicResourceLocalizer : DynamicResourceLocalizer
    {
        private readonly ICurrentTenant _currentTenant;
        
        public MyDynamicResourceLocalizer(IServiceScopeFactory serviceScopeFactory, IDistributedCache<LanguageTextCacheItem> cache, ICurrentTenant currentTenant) : base(serviceScopeFactory, cache)
        {
            _currentTenant = currentTenant;
        }
    
        public override LocalizedString GetOrNull(LocalizationResourceBase resource, string cultureName, string name)
        {
            using (_currentTenant.Change(null))
            {
                return base.GetOrNull(resource, cultureName, name);
            }
        }
    
        protected override LanguageTextCacheItem GetCacheItem(LocalizationResourceBase resource, string cultureName)
        {
            using (_currentTenant.Change(null))
            {
                return base.GetCacheItem(resource, cultureName);
            }
        }
    
        protected override Task<LanguageTextCacheItem> GetCacheItemAsync(LocalizationResourceBase resource, string cultureName)
        {
            using (_currentTenant.Change(null))
            {
                return base.GetCacheItemAsync(resource, cultureName);
            }
        }
    }
    
  • User Avatar
    0
    lba created

    Hi liangshiwei, thank you for your suggestion! I will check if this solves our problem and report back sometime around the middle of next week.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    ok

  • User Avatar
    0
    lba created

    Using the code you provided and configuring it in the module as follows works perfectly.

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddSingleton<IDynamicResourceLocalizer, MyDynamicResourceLocalizer>();
    }
    

    We can now login without a tenant to update and localize our text which will then display as expected for all tenants.

    Thank you!

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11