Monday, November 5, 2012

struggling with resources

Share photos on twitter with Twitpic

Trying to follow an example in C# 4.0 in a Nutshell, I made welcome.en.resx as shown in the image. When inspected in notepad, the file ended like so:

   <data name="Greeting" xml:space="preserve">
      <value>hello</value>
   </data>
</root>

 
 

I also made welcome.de.resx for a German greeting.

   <data name="Greeting" xml:space="preserve">
      <value>hallo</value>
   </data>
</root>

 
 

The resources were in an MVC4 project. I tried to roll a test in a test project like so:

using System.Reflection;
using System.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ResourcesExperiment.Tests
{
   [TestClass]
   public class MyTest
   {
      [TestMethod]
      public void TestMethod1()
      {
         Assembly assembly = Assembly.LoadFile(@"c:\Tom\ResourcesExperiment\
               ResourcesExperiment\bin\ResourcesExperiment.dll");
         ResourceManager resourceManager = new ResourceManager("welcome",
               assembly);
         System.Threading.Thread.CurrentThread.CurrentUICulture = new
               System.Globalization.CultureInfo("en");
         Assert.AreEqual(resourceManager.GetString("Greeting"),"hello");
      }
   }
}

 
 

This blew up like this:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "welcome.resources" was correctly embedded or linked into assembly "ResourcesExperiment" at compile time, or that all the satellite assemblies required are loadable and fully signed.

 
 

This and this had some advice, but I am yet to get around the problem. I tried putting the resources in a App_GlobalResources and/or App_LocalResources and I experimented with setting the Build Action to both Embedded Resource and Content. I also tried to just jam my logic into a Controller, setting a breakpoint at return View();

using System.Reflection;
using System.Resources;
using System.Web.Mvc;
namespace ResourcesExperiment.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         ResourceManager resourceManager = new ResourceManager("welcome",
               Assembly.GetExecutingAssembly());
         string setBreakpointHere = resourceManager.GetString("Greeting");
         return View();
      }
   }
}

 
 

...and yet. I'm still screwing something up.

No comments:

Post a Comment