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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With