Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

LocalizationHelper.GetString not working with provided cultureInfo in azure function #6074


User avatar
0
PhilWynn created

Hi,

I have a requirement to get localized strings on specific languages. In my MVC application, this is achieved successfully by using the following:

var cultureInfo = new CultureInfo(languageName, true);
  var str = LocalizationHelper.GetString(IppexCloudConsts.LocalizationSourceName, "AboutUs", cultureInfo);

I now have the same requirment within an Azure Function. I have set this up to run ABP. However, the code above now simply returns the label rather than the localized string. Without the culture info parameter, I am still getting localized values (using the default culture within which the function is running).

ABP is bootstrapped into the function using the following code:

public static class Bootstrapper
    {
        private static bool IsStarted = false;
        private static readonly object _syncLock = new object();

        public static AbpBootstrapper AbpBootstrapper { get; private set; }

        public static void Startup()
        {
            if (!IsStarted)
            {
                lock (_syncLock)
                {
                    if (!IsStarted)
                    {
                        var bootstrapper = AbpBootstrapper.Create<IppexCloudAzureFunctionsModule>();
                        bootstrapper.Initialize();
                        AbpBootstrapper = bootstrapper;

                        IsStarted = true;
                    }
                }
            }
        }
    }

My module code is as follows:

  [DependsOn(typeof(IppexCloudDataModule), typeof(IppexCloudUploadApplicationModule), typeof(IppexCloudAzureFileStorageModule))]
    public class IppexCloudAzureFunctionsModule : AbpModule
    {
        public override void PreInitialize()
        {
            Database.SetInitializer<IppexCloudDbContext>(null);
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }

8 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @philwynn

    Normally localization source is *LocalizationConfigurer.Configure method in the PreInitialize of your app's CoreModule.

    Could you check if your Core module's PreInitialize method is executed or not ?

  • User Avatar
    0
    PhilWynn created

    Hi,

    Yes, it is being executed.

    I have since found, that if I use an instance of LocalizationManager rather than the static LocalizationHelper method, I do get the correct localized string back.

    I would, however, appreciate a solution for LocalizationHelper, as using an instance will cause me much refactoring.

    Many thanks

  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, you can easily get the instance of localization manager under abp service/controller when developing MVC application.

    However, if you really need to accees it via static methods, you can try using IocManager.Instance() to resolve the localization manager instance (though it is not recommended)

  • User Avatar
    0
    PhilWynn created

    I am running this code within the context of an Azure Function, so do not have access to anything MVC related.

    Do you have any idea as to why LocalizationHelper is not working as described?

    You say that it is not recommended to use IocManager.Instance(). Could you explain why this is please?

    Many thanks.

  • User Avatar
    0
    PhilWynn created

    I am wondering if the language used by LocalizationHelper gets set when I initialize the AbpBootstrapper.

    If this is the case, how can I programmatically change this language setting?

    If I can do this, then I won't need to pass in the culture info to get the correct string (this would be ok, as each invocation of an Azure Funtion runs its own thread)

  • User Avatar
    0
    ryancyq created
    Support Team

    You say that it is not recommended to use IocManager.Instance(). Could you explain why this is please?

    It was under the assumption that you are using IocManager.Instance() in your application code.

    However, it seems that you want to access localization source from azure function. Currently there is no such built-in functionality.

    You need to share the code for LocalizationHelper.cs.

  • User Avatar
    0
    PhilWynn created

    LocalizationHelper is an Abp class:

    Abp.Localization.LocalizationHelper

  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, you can try debug the dictionaries in the LocalizationManager

    e.g refering to the method in Abp.Localization.LocalizationHelper

    LocalizationManager.Value.GetSource(sourceName).DictionaryProvider.Dictionaries.Values