abp icon indicating copy to clipboard operation
abp copied to clipboard

Runtime manipulation of localization strings

Open duard-sf opened this issue 6 years ago • 11 comments

Is it possible to create, update, or delete localization strings in runtime?

duard-sf avatar Jan 07 '20 05:01 duard-sf

see https://aspnetboilerplate.com/Pages/Documents/Zero/Language-Management

maliming avatar Jan 07 '20 06:01 maliming

How can DB localization be enabled in the new ABP? The snippet:

Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();

from your link would not work, would it?

duard-sf avatar Jan 07 '20 06:01 duard-sf

Sorry, i was wrong.

In vNext, You can implement the ILocalizationResourceContributor interface and add it to the GlobalContributors of AbpLocalizationOptions.

Dynamically manipulate localization strings in YourLocalizationResourceContributor at runtime

maliming avatar Jan 07 '20 06:01 maliming

Ah, thank you very much, that sounds promising. I will reply to this comment once I have implemented and tested that.

duard-sf avatar Jan 07 '20 06:01 duard-sf

I have completed the implementation and most of the testing, and have found that, since I have been using the newly-created DatabaseLocalizationResourceContributor, fetching the application configuration from the api/abp/application-configuration endpoint takes about 15 seconds.

Troubleshooting this, I found that the Fill method on my DatabaseLocalizationResourceContributor is called 58 times on each request to the application-configuration endpoint.

Should I be caching the values from the database or how would this be resolved? ABP's RemoteLocalizationContributor does not implement any caching.

Code

MyApp.Domain.Localization

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using Volo.Abp.Localization;
using Volo.Abp.Threading;

namespace MyApp.Localization
{
    public class DatabaseLocalizationResourceContributor : ILocalizationResourceContributor
    {
        private ILocalizedMessageRepository _localizedMessageRepository;

        public void Initialize(LocalizationResourceInitializationContext context)
        {
            _localizedMessageRepository = context.ServiceProvider.GetRequiredService<ILocalizedMessageRepository>();
        }

        public LocalizedString GetOrNull(string cultureName, string name)
        {
            LocalizedMessage message = AsyncHelper.RunSync(() => _localizedMessageRepository.GetAsync(cultureName, name));

            if (message == default)
            {
                return null;
            }

            return new LocalizedString(message.Key, message.Value);
        }

        public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary)
        {
            List<LocalizedMessage> messages = AsyncHelper.RunSync(() => _localizedMessageRepository.GetForCultureAsync(cultureName));

            foreach (LocalizedMessage message in messages)
            {
                dictionary[message.Key] = new LocalizedString(message.Key, message.Value);
            }
        }
    }
}

MyApp.HttpApi.Host (omitted for brevity)

public class OfficeHttpApiHostModule : AbpModule
{
    private void ConfigureLocalization()
    {
        Configure<AbpLocalizationOptions>(options =>
        {
            options.Languages.Add(new LanguageInfo("en", "en", "English"));
            options.GlobalContributors.Add<DatabaseLocalizationResourceContributor>();
        });
    }
}

duard-sf avatar Jan 08 '20 13:01 duard-sf

hi You should cache the messages, and remember to change the cache when LocalizedMessage changes.

List<LocalizedMessage> messages = AsyncHelper.RunSync(() => _localizedMessageRepository.GetForCultureAsync(cultureName));

RemoteLocalizationContributor uses ICachedApplicationConfigurationClient(_applicationConfigurationClient) to get configuration information. As you can see from the name, it uses cache. ; )

https://github.com/abpframework/abp/blob/0860f54e5ad4b4bdb7d10f87a57f3b33f880d5d8/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/RemoteLocalizationContributor.cs#L14

maliming avatar Jan 08 '20 13:01 maliming

Oh I missed that it uses ICachedApplicationConfigurationClient! Thanks for the help, I will reply with cache implementation once it has been finished and tested.

duard-sf avatar Jan 09 '20 04:01 duard-sf

Sorry for the long delay in getting back to this issue.

Unfortunately, I had to put this on hold in my project, because I had to spend too much time in creating a solution for synchronizing localized strings between environments and between developers.

Regardless, I completed the caching implementation, and simply used a distributed memory cache.

duard-sf avatar Jan 16 '20 08:01 duard-sf

Serenity has a feature ,it can translate in runtime.No db used,gen some json in local.

But i think,ABP implementation of better.

mentianyi avatar Mar 29 '20 08:03 mentianyi

_localizedMessageRepository

Hello @duard-sf , Can you please share the repository(ILocalizedMessageRepository) code how you are fetching the data from the database. I need the same like you in my project.

jacmiller12 avatar Jul 22 '20 04:07 jacmiller12

Hi @jacmiller12 , it's just an implementation of EfCoreRepository like any other kind of repository in ABP. The implementation will differ depending on how you want to design your entities.

duard-sf avatar Jul 27 '20 05:07 duard-sf