从资源文件动态生成可用的本地化

本文关键字:本地化 资源 源文件 动态 | 更新日期: 2023-09-27 18:06:28

我有一个使用.resx文件进行本地化的MVC网站。但是,当前用于确定支持哪些本地化的行为需要在物理.resx文件上进行迭代,并且一旦站点被编译为发布,它们就不存在了。

当前文件夹结构为:

  • 语言资源
    • Resource.en-US.resx
    • Resource.fr-CA.resx
    • Resource.hi.resx
    • 资源。resx

尝试通过GetManifestResourceNames()获取所有资源文件的列表,根据这个答案,只产生单个LanguageResources.Resource.resources文件,表示主的,未本地化的列表。它可能有也可能没有嵌入本地化,但我没办法看到。

我如何在运行时告诉我支持三种语言?

<子>最终目标是基于这三个值构建一个下拉菜单。如果我应该用另一种方式来解决这个问题,那么回答哪个地址也是可以接受的

从资源文件动态生成可用的本地化

根据我的问题的边栏链接,我最终找到了这个问题,以及Hans Holzbart的出色答案,我将其复制在下面。这个问题本身是从另一个方向出发的,但答案同样适用于我的情况。

// Pass the class name of your resources as a parameter e.g. MyResources for MyResources.resx
ResourceManager rm = new ResourceManager(typeof(MyResources));
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo culture in cultures)
{
    try
    {
        ResourceSet rs = rm.GetResourceSet(culture, true, false);
        // or ResourceSet rs = rm.GetResourceSet(new CultureInfo(culture.TwoLetterISOLanguageName), true, false);
        string isSupported = (rs == null) ? " is not supported" : " is supported";
        Console.WriteLine(culture + isSupported);
    }
    catch (CultureNotFoundException exc)
    {
        Console.WriteLine(culture + " is not available on the machine or is an invalid culture identifier.");
    }
}

出于我的目的,我必须用.Where(x => x.Name != "")过滤掉不变区域性,但除此之外,这工作得很好。