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

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.

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}" />

Related

Use value from resx file in selected language

I have a ASP.NET MVC Website.
I use resources files to translate the website using
#Html.Encode(Resources.MY_STRING)
But in some pages, I would like to display the text in all languages. Is it possible to do it with resx files ?
Here is a example of what I want to do :
#Html.Encode(Resources.MY_STRING, "en-US")
#Html.Encode(Resources.MY_STRING, "fr-FR")
Of course it doesn't like this but is there a way to do it using .resx files ? Or should but these texts in an other configuration file...?
Yes, that is possible. But not as direct as your code.
CultureInfo userCulture = CultureInfo.CreateSpecificCulture("en-US");
string myString = HttpContext.GetGlobalResourceObject("MyResource", "MyString", userCulture).ToString();
But maybe you just wanna store all languages for that particular case in one/all resources.

How to read the Resource File using C#?

I have a web application which should be Localized to 3 languages. All the controls are taking the control text from the Resx file of that language. Now I have scenario like suppose if we have a messages,custom error messages to show for that particular culture. So for this I have created a seperate Foldere as "Resources" and created a resx as "DialogMessages.ar-IQ.resx".
How can I read the "DialogMessages.ar-IQ.resx" in C# ?
I have tried to read the file using ResxResourceReader class. Is this a correct process or any flaw exists ?
You can use ResXResourceReader and specifying the resource file location properly .
ResXResourceReader reader = new ResXResourceReader("Map path with resource file");
IDictionaryEnumerator iterator = reader.GetEnumerator();
while (iterator.MoveNext())
{
// process the collection of key value pair.
}

ASP.NET Read Resources values

I have two .resx files: en.resx and he.resx, in the folder App_LocalResources.
I already have two buttons in my web page, clicking each one is supposed to "switch" to the other language's resource file.
I want to simply get a string value located in one of the .resx files.
I tried some of the examples I have found on google, and I asked myself, why do I need to provide an Assembly type and a namespace, when i just want to ask for a string value in my own project?
Why isn't there something like: string val = Resources["en.resx"]["SomeProperty"].Value?
Maybe my whole approach is wrong, and I would like to read your opinions.
Thanks, Guy
using System.Resources;
ResXResourceSet Resource = new ResXResourceSet(HttpContext.Current.Server.MapPath(#"~/Properties/Resource.resx")
String value=Resource.GetStrin("key");

Get full name of binary from Razor Template, including extention

I'm using a technique I found on SO (here) to publish binaries from a Razor template. It works great except for one minor shortcoming.
I want to push an item with the file name that was uploaded to Tridion. I can use the title for most of that, but how can I determine the file extension? In this case I need to know the name of the file in the template because I am going to do some javascript manipulation with it.
Again, everything works fine, but I'd like to do something other than just concatenating a ".jpg". It looks like I can parse the WebDavUrl property. Is that my only option?
#{
var item1 = TridionHelper.Package.CreateMultimediaItem(#Fields.closed.ID);
TridionHelper.Package.PushItem(#Fields.closed.Title + ".jpg", item1);
string closed = #Publication.MultimediaUrl + "/"+(#Fields.closed.Title) + ".jpg";
}
You have full access to the TOM.NET API from Razor Mediator, so you can also access the filename from the Component's BinaryContent field. Assuming #Fields.closed is a Multimedia Component Link you can do:
#Fields.closed.TridionObject.BinaryContent.Filename
Note that this will be the original path/filename that was uploaded. You also have access to System.IO.Path, so you co:
#System.IO.Path.GetFileName(Fields.closed.TridionObject.BinaryContent.Filename)
#System.IO.Path.GetExtension(Fields.closed.TridionObject.BinaryContent.Filename)
#System.IO.Path.GetFileNameWithoutExtensions(Fields.closed.TridionObject.BinaryContent.Filename)
You'll have to be careful if you have a custom TBB that publishes binaries, as this TBB could actually change the filename that is published, and the Filename property of BinaryContent will only contain the original uploaded path and filename.

ASP.NET HttpContext.GetLocalResourceObject() throws InvalidOperationException

Let's say we have such site structure:
App_LocalResources
|- A.aspx.resx
|- B.aspx.resx
A.aspx
B.aspx
Now I use HttpContext.GetLocalResourceObject("~/A.aspx", "Key1") in A.aspx.cs, and it works fine.
But if I use HttpContext.GetLocalResourceObject("~/A.aspx", "Key1") in B.aspx.cs, it throws an exception:
The resource class for this page was not found. Please check if the resource file exists and try again.
Exception Details: System.InvalidOperationException: The resource class for this page was not found. Please check if the resource file exists and try again.
How can I resolve this problem? I want to read the local resources from an external page, and I don't want to read the .resx file myself. Thanks :-)
UPDATE: In my case, there're some "data.xml" files(they are in different directories, and have elements like <key name='Key1' value='value1' />), and the contents of them will be rendered as html.
But the key names in the data.xml should be localized before rendering (different data.xml contain different keys).
For example, the data.xml has such an element:
<key name='CategoryId' value='3' />
In the result html page, I want to display "Category Id = 3" for en-US culture, and "类别=3" for zh-CN culture, etc.
So I think I can create some files following the pattern "data.xml.??-??.resx" in the App_LocalResources folder, then use the HttpContext.GetLocalResource() for each data.xml to retrieve the localized key names. That way I don't need to read the xml myself. Is it possible?
That's not the way that local resources are supposed to be used. Local resources are only valid for a page or control. You should use global resources in your case.
From MSDN
Global Resource Files
You create a global resource file by putting it in the reserved folder App_GlobalResources at the root of the application. Any .resx file that is in the App_GlobalResources folder has global scope. Additionally, ASP.NET generates a strongly typed object that gives you a simple way to programmatically access global resources.
Local Resource Files
A local resources file is one that applies to only one ASP.NET page or user control (an ASP.NET file that has a file-name extension of .aspx, .ascx, or .master). You put local resource files in folders that have the reserved name App_LocalResources. Unlike the root App_GlobalResources folder, App_LocalResources folders can be in any folder in the application. You associate a set of resources files with a specific Web page by using the name of the resource file.
And could be also useful for you to check how access resources programatically
Button1.Text =
GetLocalResourceObject("Button1.Text").ToString();
Image1.ImageUrl =
(String)GetGlobalResourceObject(
"WebResourcesGlobal", "LogoUrl");
Image1.Visible = true;
As Claudio Redi recommended, use Global Resource files.
I would create one per xml file in the format of "filename.resx", so in your example, you'd name it Data.resx.
Set the "Name" in the resource to your "name" attribute and the Value equal to the translated "name".
For example, in Data.resx, you'd have Name=CategoryId, Value=Category Id. In Data.zh-CN.resx, you'd have Name=CategoryId, Value=类别.
One you have the data in the resource files, you'd probably want to create a class wraps the functionality of the XML lookup and localization for your in your application. Something like this should work:
public class Data
{
private const string fileLocation = "TODO";
public string Name{ get; set; }
public string Value{ get; set; }
private Data()
{
}
public Data( string Name )
{
// TODO: Look up the single key from XML
}
public string GetLocalizedName( CultureInfo cultureInfo )
{
return Resources.Data.ResourceManager.GetString(Name, cultureInfo);
}
public static List<Data> LoadData()
{
List<Data> dataList = new List<Data>();
// TODO: Load XML and create a list of Data objects.
return dataList;
}
}
Try the following steps:
Step 1 - Delete the temp files of your web site from
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
Step 2 - Clean and Rebuild your Solution.
Step 3 - Make sure that your Resource file in the App_LocalResources folder has the same name as the page that has this issue (and both App_LocalResources and the page are in the same folder).
Your App should be OK then.
http://iymbas.blogspot.com

Resources