How to read ".resources" file generated by "resgen.exe"?

654 Views Asked by At

By using resgen.exe, I can generate the "resources" file from a txt file. I can use the "resources" file in WPF, however I am not sure how can I populate the value in "cshtml" file. Any help would be appreciated.

1

There are 1 best solutions below

0
On

Design-time .resx files are compiled into binary .resources blobs within .NET assemblies that are then read with the ResourceManager class. These blobs are essentially just key/value dictionaries.

Like so:

ResourceManager res = new ResourceManager("NameOfEmbedded.resources", GetType().GetExecutingAssembly());
String localizedString = res.GetString("resourceKey");

In Visual Studio will create a strongly-typed wrapper around each (non-localized) .resx file so you don't need to remember each resource's string key and also makes it available from a static context, so all you need to do is:

String localizedString = Resources.ResourceKey;

(Assuming Resources is the name of your .resx-wrapper class)

In your .cshtml files, to render localized text, use the @ syntax to render accordingly:

<p>Hello this next text is localized: @Resources.ResourceKey</p>

This is different to WPF where you would do it like this:

xmlns:r="MyNamespace.Properties"

<TextBlock Content="{x:Static r:Resources.ResourceKey}" />