0

I have some html code that I want to replace with text from resources.

My class looks like:

 public static class ResourceParser
    {
        public static string GetTextFromResource(string keyValue)
        {
            ResourceManager rm = new ResourceManager("pl", Assembly.GetExecutingAssembly());

            return rm.GetString(keyValue);
        }
    }

When I access resources from my view this way:

@Resources.pl.accept;

it works and displays the value I want.

When I do it like this:

@ResourceParser.GetTextFromResource("accept");

there is an exception

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

Alice
  • 173
  • 1
  • 15
  • What is the name of the resource file and what is the file path? – haldo Nov 28 '18 at 14:20
  • 1
    See if [this link](https://developerslogblog.wordpress.com/2017/11/12/how-to-fix-could-not-find-any-resources-appropriate-for-the-specified-culture/) helps. Cannot post as answer yet as that would be just guessing – Andrei Nov 28 '18 at 14:26
  • @Andrei I've changed to embedded resources and still the same problem occurs. Now The part that was working before is not working :P – Alice Nov 28 '18 at 14:46
  • @Haldo the resource file name is pl.resx. The resource file is in different project actually – Alice Nov 28 '18 at 14:48
  • What is the namespace of the project? I think it should be loaded using full namespace: `new ResourceManager('Full.Namespace.WithoutExtension')`. – haldo Nov 28 '18 at 14:55
  • Does [this SO question](https://stackoverflow.com/questions/4873623/accessing-resx-file-from-another-project-assembly) help? – haldo Nov 28 '18 at 14:57
  • @Haldo so if I have Solution "Name1.Name2" and then the Project "Name3.Name4" and inside the project a folder "Folder1" and the file pl.resx the path would be like new ResourceManager("Name1.Name2.Name3.Name4.Folder1.pl") ? – Alice Nov 28 '18 at 15:00
  • @Alice Not sure tbh. I'd try `ResourceManager("Name3.Name4.Folder1.pl")`. – haldo Nov 28 '18 at 15:05
  • I've tried different variations and it seems that it still doesn't work – Alice Nov 28 '18 at 15:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184411/discussion-between-haldo-and-alice). – haldo Nov 28 '18 at 15:11

1 Answers1

0

It started working somehow.

In the meantime I've added this method:

 protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (Request.UserLanguages != null)
            {
                string culture = Request.UserLanguages[0];

                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
            }
        }

to my Global.asax.cs file

and I've change the code a bit:

public static string GetTextFromResource(string keyValue)
        {
            var path = "ProjectName.Folder.pl";

            var res_manager = new ResourceManager(path, typeof(pl).Assembly);

            return res_manager.GetString(keyValue);
        }
Alice
  • 173
  • 1
  • 15