简体   繁体   中英

Using .resx file in Azure Function App

I am creating a new webhook C# function in Azure that I wish to return fixed content in different translations depending on either an incoming lang query parameter or the Accept-Language header.

For storing the different translations I naturally think of .resx files. Is there a way to utilize .resx files in Azure Function Apps?

It doesn't look like resource files are supported properly yet .

I worked around by reading the embedded resource file(s) into a resource set.

var culture = CultureInfo.CurrentUICulture;
var resourceName = $"FunctionApp.Properties.Resources.{culture.TwoLetterISOLanguageName}.resources";
var cultureResourceSet = new ResourceSet(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName));
var localizedString = cultureResourceSet.GetString(resourceKey);
// fallback to default language if not found

Provided answer did not help me so I've done small wrapper

  public static class ResourceWrapper
    {
        private static Dictionary<string, ResourceSet> _resourceSets = new Dictionary<string, ResourceSet>();
        static ResourceWrapper()
        {
            _resourceSets.Add("uk", Load("uk"));
            _resourceSets.Add("ru", Load("ru"));
            _resourceSets.Add("en", Emails.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, false, false));
        }

        private static ResourceSet Load(string lang)
        {
            var asm = System.Reflection.Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, "bin", lang, "Function.App.resources.dll"));
             var resourceName = $"Function.App.Resources.Emails.{lang}.resources";
             var tt = asm.GetManifestResourceNames();
            return new ResourceSet(asm.GetManifestResourceStream(resourceName));
        }

        public static string GetString(string key)
        {
            return _resourceSets[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName].GetString(key);
        }
    }

this was my solution:

First i do this:

    public void SetLanguage(FunctionRequestDTO data)
    {

        if (string.IsNullOrWhiteSpace(data.LanguageSetting))
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(data.LanguageSetting);
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(data.LanguageSetting);
        }

        ResourceWrapper.Load(Thread.CurrentThread.CurrentCulture.Name.ToLower());
    }

Then:

   public static class ResourceWrapper
    {
        private static Dictionary<string, ResourceSet> ResourceSets = new Dictionary<string, ResourceSet>();

        private const string DEFAULT_LANGUAGE_VALUE = "default";

        static ResourceWrapper()
        {
            try
            {
                ResourceSets.Add(DEFAULT_LANGUAGE_VALUE, new ResourceSet(Assembly.GetExecutingAssembly().GetManifestResourceStream("Function.Logic.Resources.Resource.resources")));
            }
            catch { }
        }

        public static void Load(string lang)
        {
            if (string.IsNullOrEmpty(lang) || ResourceSets.ContainsKey(lang))
            {
                return;
            }

            lock (new object())
            {
                if (ResourceSets.ContainsKey(lang))
                {
                    return;
                }

                try
                {
                    string rootPath = Environment.CurrentDirectory;

                    if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
                    {
                        rootPath = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot\\";
                    }

                    var asm = Assembly.LoadFrom(Path.Combine(rootPath, "bin", lang, "Function.Logic.resources.dll"));
                    var resourceName = $"Function.Logic.Resources.Resource.{lang}.resources";
                    ResourceSets.Add(lang, new ResourceSet(asm.GetManifestResourceStream(resourceName)));
                }
                catch { }
            }
        }

        public static string GetString(string key)
        {
            string value = "";

            try
            {
                string language = System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower();

                if (string.IsNullOrEmpty(language))
                {
                    language = DEFAULT_LANGUAGE_VALUE;
                }

                if (ResourceSets.ContainsKey(language))
                {
                    value = ResourceSets[language].GetString(key);
                }

            }
            catch { }

            return value ?? "";
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM