简体   繁体   English

在Azure Function App中使用.resx文件

[英]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. 我正在Azure中创建一个新的webhook C#函数,我希望根据传入的lang查询参数或Accept-Language标头返回不同翻译中的固定内容。

For storing the different translations I naturally think of .resx files. 为了存储不同的翻译,我自然会想到.resx文件。 Is there a way to utilize .resx files in Azure Function Apps? 有没有办法在Azure功能应用程序中使用.resx文件?

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 ?? "";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用功能app将文件从Sftp移到Azure容器 - Move the file from Sftp to the Azure container using function app 如何使用特定区域性从App_LocalResources文件夹中的resx文件读取? - How to read from a resx file in the App_LocalResources folder using a specific culture? 根据区域性信息使用正确的“ .resx”文件 - Using the correct “.resx” file according to the culture info 在Azure功能应用程序中使用MEF - Using MEF in an Azure function App WPF:使用 resx 文件进行白标(非本地化) - WPF: Using resx file for white labelling (Not localization) 使用 Azure 功能下载文件并将其存储到 Azure - Download and store file to Azure using Azure function 将文件从 azure 函数应用上传到 azure 文件共享 - Uploading file to azure file share from azure function app 将 Serilog 添加到 Azure Function 应用程序并记录到文件? - Adding Serilog to Azure Function App and logging to a file? 如何在Windows 8.1(WinRT)应用程序中引用.resx文件? - How to reference a .resx file in a Windows 8.1 (WinRT) app? 在应用程序 xamarin.android 应用程序中的 xaml 中使用 resx - using resx in xaml in the application xamarin.android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM