【问题标题】:Using .resx file in Azure Function App在 Azure Function App 中使用 .resx 文件
【发布时间】:2017-04-26 22:06:30
【问题描述】:

我正在 Azure 中创建一个新的 webhook C# 函数,我希望根据传入的语言查询参数或 Accept-Language 标头以不同的翻译返回固定内容。

为了存储不同的翻译,我自然会想到 .resx 文件。有没有办法在 Azure Function Apps 中使用 .resx 文件?

【问题讨论】:

    标签: c# azure azure-functions resx


    【解决方案1】:

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

    我通过将嵌入式资源文件读入资源集中来解决问题。

    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
    

    【讨论】:

      【解决方案2】:

      提供的答案对我没有帮助,所以我做了小包装

        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);
              }
          }
      

      【讨论】:

        【解决方案3】:

        这是我的解决方案:

        首先我这样做:

            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());
            }
        

        然后:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-19
          • 2020-04-25
          • 2020-12-13
          • 2021-11-15
          • 1970-01-01
          • 2022-11-02
          • 2021-01-03
          • 1970-01-01
          相关资源
          最近更新 更多