Culture resource files - asp.net

I'm working on an ASP.NET app
I have a couple of resource files with the different languages I can support
example: Language.en.resx
Language.pt.resx
Is there any way to get, for example, a list with all the different languages dynamically?

If you are looking for a way which determine how many ( and what ) languages you localize for your application. There is no solution.
You have to write a parser which look in a series of sub directories in your application ( or given ) path. the read and store the name of Resx files into a list.
Finally you have to split the name of Resx file with Dot (split('.')) and seperate the language part of the Resx files like
string[] myString = new string[MyResxList.lenght];
for (int i=0; i<=MyResxList.lenght;i++)
myString[i] = MyResxList[i].toString().split('.')[3];
note that above code is a snippet and I wrote it here so you have to debugit if it's necessary
then you should remove the duplicates and return the List

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

Multiple file upload association in MVC

I have implemented the File Upload (upon reading Scott Hanselman's excellent post)
I have multiple files associated with various questions on the form though, and would like to associate each saved file with an internal ID.
How can I do this? For example, if question # 3 has a file uploaded abc.pdf, how can I associated that file with ID #3?
Any good ideas, or has someone done this before?
I would have an array or vector in one of files with a getter and setter. This way when question #3 has file abc.pdf uploaded you can send the information you want to save to the setter and save it at index 3. When you want to access it use the getter for index 3.
Depending what you want to save you create an array that holds what you want. I haven't used Asp.net but this site tells you how to sort an array, which we don't want, but it also shows how to make an array of structures. So if you want to save the name of the file only then you only need a string array. But if you need to save the name and something else then create the array of structures.
Private Structure FileInfo
Public Name As String
Public OtherInfo As String
End Structure
Then create the array with :
Dim FileInfoArray(NumOfPotentialUploadedFiles - 1) As FileInfo
Since it sounds like each of your input fields upload one file each you would just need to remember the id number of the fields and then you would easily "know which IDs the uploaded files were associated with" as if field 1 has an uploaded file then it would be in the array at the same position. You could create a boolean within the structure that is set to false when you first create the array. Then when you upload a file of index 1 you change the boolean to true. This way you easily know which files you have when you go through the array b/c only the positions with a true value have a file.
Ok, figured out an easy solution. I was struggling since the Request.Files[x] object did not have any reference to the fields, but the Request.Files (HttpFileCollectionWrapper) has an AllKeys property that holds the array of fields. My code now is:
for (int fileIndex = 0; fileIndex < Request.Files.Count; fileIndex++)
{
string fieldName = Request.Files.AllKeys[fileIndex]; <--- Here is where you can gleam an key to persist to the database, I have an ID in the fieldName
string savedFileName = Path.GetFileName(Request.Files[fileIndex].FileName);
var path = Path.Combine(<your server save path>, savedFileName);
Request.Files[fileIndex].SaveAs(path);
}
Easy enough!

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.

Resources