c# - 如何在 "website"模式下使用 ResourceManager?

标签 c# asp.net localization web

我在这里尝试为我正在使用的应用程序进行手动翻译。 (已经有一个工作的 LocalizationModule,但它工作不可靠,所以我不能使用 <asp:Localize /> 标签。

通常对于 ResourceManager,您应该将其用作 Namespace.Folder.Resourcename (在应用程序中)。目前我正在翻译一个现有的 asp.net“网站”(不是 Web 应用程序,所以这里没有命名空间....)。

资源位于名为“Locales/resources”的文件夹中,其中包含“fr-ca.resx”和“en-us.resx”。

所以我使用了类似这样的代码:

public static string T(string search)
 {
  System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null );

  var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture);

  if (text == null)
   return "null";
  else if (text == string.Empty)
   return "empty";
  else
   return text;
 }

在页面里面我有这样的东西<%= Locale.T("T_HOME") %>

当我刷新时,我有这个:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Locales.resources" was correctly embedded or linked into assembly "App_Code.9yopn1f7" at compile time, or that all the satellite assemblies required are loadable and fully signed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Locales.resources" was correctly embedded or linked into assembly "App_Code.9yopn1f7" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Source Error:

Line 14:
System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null ); Line 15: Line 16: var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture); Line 17: Line 18: if (text == null)

Source File: c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs Line: 16

我什至尝试用 Locales.fr-ca 加载资源或者只有 fr-ca这里没有什么能正常工作。

最佳答案

如果您无权访问 HTTPContext,Marvin Smit 的解决方案非常有用

const string ASSEMBLY_NAME = "App_GlobalResources";
const string RESOURCE_NAME = "Resources.MetaTagResource";
const string RESOURCE_MANAGER = "ResourceManager";

Assembly assembly = Assembly.Load(ASSEMBLY_NAME);
Type type = assembly.GetType(RESOURCE_NAME);
PropertyInfo propertyInfo = type.GetProperty(RESOURCE_MANAGER);
ResourceManager resourceManager = propertyInfo.GetValue(null, new object[] { }) as ResourceManager;
resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);

但是如果您有权访问 HTTPContext,只需使用 HttpContext.GetGlobalResourceObject

string title = HttpContext.GetGlobalResourceObject("MetaTagResource", "Title").ToString();
string keywords = HttpContext.GetGlobalResourceObject("MetaTagResource", "keywords").ToString();
string description = HttpContext.GetGlobalResourceObject("MetaTagResource", "Description").ToString();

关于c# - 如何在 "website"模式下使用 ResourceManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2156265/

相关文章:

c# - 从进程中获取当前打开的 Word 文档

C# EF6 条件属性选择?

javascript - javascript 上的预期十六进制

asp.net - 无法加载文件或程序集 "Microsoft.Web.Extensions"

php - Laravel 和本地化国家 + 货币 + 语言 + 时区

c# - Azure 表查询因错误请求错误而失败

c# - 如何将变量插入带有多个引号和转义字符的长字符串中?

javascript - Node.Select() 在 RadTreeView 中触发 Node_Click() 事件?

url - 规范网址和本地化

android - 重构——Android字符串提取问题