Use value from resx file in selected language - asp.net

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.

Related

Possible to use one resource file with multiple languages?

I've got a .net 4.0 website that we have 2 copies of it running. One for US based users and another for AU based users. Code is basically the same, the only differences being some text and wording here or there where it references the US versus Australia. Right now I have two copies of the site which is pretty silly. So I want to maintain just one copy and put all these regional text changes in a resource file.
Is it possible to have just a single resource file contain multiple 'languages' or do i need to create a Resource.US.resx and a Resource.AU.resx file? Also, if i do create two files, how do I tell .net which file to use in each site? I assume in the web.config globalization uiCulture & culture='en-AU' or en-US would tell .net which of the two files to use?
Yes, you are right, you can use Resource.US.resx and a Resource.AU.resx files and tell in web.config globalization uiCulture & culture='en-AU' or en-US
in code:
string culturePref = [Your setting from web-config]; // "en-US" or "en-EU"
try
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(culturePref);
}
catch
{
Thread.CurrentThread.CurrentCulture =
new CultureInfo("en-US");
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

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

Culture resource files

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

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.

Is it possible to create a .ascx virtually? i.e. not on disk, but as a string?

I was wondering if it was possible to load a asp.net control (.ascx) that doesn't reside on the file system?
Like say a string variable?
Not a string varible but you can load it from resources or zip file, but you have to have full trust. Google for VirtualPathProvider.
As of 4.0 you don't have to have full trust.
System.Reflection.Emit
Assembly.Load(byte[])
No designer for you if you do this though.
You could try this:
string controlString = //read or build it
Control control = this.Page.TemplateControl.ParseControl(controlString);
More information is available here:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx

Resources