Red
Red

Reputation: 7324

Get all @Html.GetResource() in a .cshtml file?

We have a resource file called Resouce.NL.resx. This file contains translations for a specific language. However, alot of the @Html.GetResource() arguments dont yet exists inside the resource file.

I now have to loop through every .cshtml file to manually add all those resources.

Is there any tool or extionsion to get all @Html.GetResource() inside .cshtml files that dont yet exists within a resource file?

Upvotes: 0

Views: 1324

Answers (1)

Ashiquzzaman
Ashiquzzaman

Reputation: 5284

You can get all resources as a dictionary using System.Resources.ResourceManager in C#. Example:

 var resorces =new System.Resources.ResourceManager("YourNamespace.YourResourceFileName", Assembly.GetExecutingAssembly())
        .GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true)
            .OfType<DictionaryEntry>(); 

Razor: You can create and use custom helper.example:

Helper:

public static class MyClass{
 public static string ResourceFor<T>(this HtmlHelper html, object key) {
    return new System.Resources.ResourceManager(typeof(T)).GetString(key.ToString());
 }
}

Use:

@Html.ResourceFor<MyProject.Resouce.NL>("Test") //where `MyProject.Resouce.NL` is your resource namespace with class name

Upvotes: 1

Related Questions