ASP.NET Read Resources values - asp.net

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");

Related

How to handle I18n when texts are not in .properties files but in the jsp page itself

I'm building a multi-languages application with Spring MVC.
So far I handled the multi-languages system with the Spring class ReloadableResourceBundleMessageSource and .properties files. It was easy since texts were very short.
Now, I have to translate the body of the page and I can't rely on .properties files.
I have an Italian version of the page and an english version of the page. My doubt is: how should I handle it?
I thought that after the #Controller return the page name, for example "index", I should have a filter that check the application Locale and then add to the page name a suffix. So, the filter must turn "index" into "it/index" or "en/index".
IS it a good way to solve the issue?
Thank you.
Here's a suggestion with one drawback: I didn't test it with .jsp but .vm. The idea might still work.
As not to break the i18n mechanism put a message key, say parseContent in every language.property file. Now, make a view for every locale and name them, say parse_en_US.vm, parse_de_DE.vm and so on. These files must only contain what you wouldn't want to be in the language.property files.
Example of an entry in messages_en_US.porperties might be parseContent = parse_en_US.vm
An now use #springMessage('parseContent') to get the right view name depending on the present locale. This view you parse as a sub-view and problem solved.
For .vm it looks like this:
#set($view = "#springMessage('parseContent')")
#parse($view)
Same number of .vm files, but no need to invent sth new.

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.

Getting extension of the file in FileUpload Control

At the moment i get file extension of the file like :
string fileExt = System.IO.Path.GetExtension(filUpload.FileName);
But if the user change the file extension of the file ( for example user could rename "test.txt" to "test.jpg" ), I can't get the real extension . What's the solution ?
You seem to be asking if you can identify file-type from its content.
Most solutions will indeed attempt the file extension, but there are too many different possible file types to be reliably identifiable.
Most approaches use the first several bytes of the file to determine what they are.
Here is one list, here another.
If you are only worried about text vs binary, see this SO question and answers.
See this SO answer for checking if a file is a JPG - this approach can be extended to use other file headers as in the first two links in this answer.
Whatever the user renames the file extension to, that is the real file extension.
You should never depend on the file extension to tell you what's in the file, since it can be renamed.
See "how can we check file types before uploading them in asp.net?"
There's no way to get the 'real' file extension - the file extension that you get from the filename is the real one. If file content is your concern, you can retrieve the content type using the .ContentType property and verify that it is a content type that you are expecting - eg. image/jpg.

ASP.NET localized files

I've got a web page with a link, and the link is suppose to correspond to a PDF is the given user's language. I'm wondering where I should put these PDF files though. If I put them in App_LocalResources, I can't specify a link to /App_LocalResources/TOS_en-US.pdf can I?
The PDF should definitely not be in the App_LocalResources folder. That folder is only for RESX files.
The PDF files can go anywhere else in your app. For example, a great place to put them would be in a ~/PDF folder. Then your links will have to be dynamically generated (similar to what Greg has shown):
string cultureSpecificFileName = String.Format("TOS_{0}.pdf", CultureInfo.CurrentCulture.Name);
However, there are some other things to consider:
You need a way to ensure that you actually have a PDF for the given language. If someone shows up at your site and has their culture specified as Klingon, it's unlikely that you have such a PDF.
You need to decide exactly what the file format will be. In the example given, the file would have to be named TOS_en-US.pdf. It you want to use the 2-letter ISO culture names, use CurrentCulture.TwoLetterISOLanguageName and then the file name would be TOS_en.pdf.
I would store the filename somewhere with an argument in it (i.e. "TOS_{0}.pdf" ) and then just add the appropriate suffix in code:
string cultureSpecificFileName = string.Format("TOS_{0}.pdf", CultureInfo.CurrentCulture);
Does the PDF have to have the same file name for each of the different languages? If not, put them all into a directory and just store the path in your resources file.

What is the most efficient way to perform the reverse of Server.MapPath in an ASP.Net Application

I am building an MVC application in which I am reading a list of files from the file system and I want to pass the relative URL to that file to the view, preferably prefixed with "~/" so that whatever view is selected cab render the URL appropriately.
To do this, I need to enumerate the files in the file system and convert their physical paths back to relative URLs. There are a few algorithms I've experimented with, but I am concerned about efficiency and minimal string operations. Also, I believe there's nothing in the .Net Framework that can perform this operation, but is there something in the latest MVC release that can?
At the moment I don't know any built-in method to do it, but it's not difficult, I do it like this:
We need to get the Application root, and replace it in our new path with ~
We need to convert the backslashes to slashes
public string ReverseMapPath(string path)
{
string appPath = HttpContext.Current.Server.MapPath("~");
string res = string.Format("~{0}", path.Replace(appPath, "").Replace("\\", "/"));
return res;
}
Isn't this what UrlHelper.Content method does? http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.content.aspx
I did some digging, trying to get the UrlHelper class to work outside of a controller, then I remembered a old trick to do the same thing within an aspx page:
string ResolveUrl(string pathWithTilde)
Hope this helps!
See:
https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl(v=vs.110).aspx

Resources