Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .resx file in Azure Function App

Tags:

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?

like image 447
Scott Lewis Avatar asked Apr 26 '17 22:04

Scott Lewis


2 Answers

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
like image 105
des Avatar answered Sep 22 '22 11:09

des


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);
        }
    }
like image 24
Vova Bilyachat Avatar answered Sep 25 '22 11:09

Vova Bilyachat