我正在Azure中创建一个新的webhook C#函数,我希望根据传入的lang查询参数或Accept-Language标头返回不同翻译中的固定内容。
为了存储不同的翻译,我自然会想到.resx文件。有没有办法在Azure功能应用程序中使用.resx文件?
答案 0 :(得分:3)
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
答案 1 :(得分:0)
提供的答案对我没有帮助,所以我做了小包装
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);
}
}
答案 2 :(得分:0)
这是我的解决方案:
首先,我这样做:
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 ?? "";
}