Possible to use one resource file with multiple languages? - asp.net

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;

Related

ASP.NET MVC - How to use custom Culture in EvoHtmlToPdf

my program (ASP.NET MVC with VB.NET) is to convert local HTML(with dynamic conrtoller action) to PDF file using third-party tools (EvoHtmlToPdf)
However, my local HTML contains a lot of CSS and I can only use EvoHtmlToPdf's API to convert PDF (pass the link to the function)
outPdfBuffer = htmlToPdfConverter.ConvertUrl(Me.Url.Action("TestDownloadPDFText", "ReportA", New With {.Culture = Session("Culture")}, Me.Request.Url.Scheme))
Whatever I set the culture in different place, the PDF will be showed in English only. If I just try http://localhost/ReportA/TestDownloadPDFText and it can show Chinese when I hard code Chinese culture in code behind.
So, I guess the problem is coming from the third-party tools (EvoHtmlToPdf). Any suggestion or solution for this case? Thanks a lot.
I have finally solved the problem. The solution is to add culture configuration in that controller.
Dim ci As CultureInfo
ci = New CultureInfo(Culture)
System.Threading.Thread.CurrentThread.CurrentUICulture = ci
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
Thanks all.

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.
}

storing the files in a web application in asp.net

I have set of components that i wish to let the users download from my web application.
Now the question is where should i place the files in app_data or create a separate folder in asp.net web application as shown here or is there any other optimal solution for this ?
What i mean by components is you can take a look at this ! So what is the best way to do store the components ?
Right now what i'm doing is: i'm storing the files in a external folder outside the application more specifically in documents folder of my c drive, and i'm storing the path to a component as a data element of the table, when ever user clicks on a particular row's button (in the grid view) i'm getting the title of that particular clicked row and querying the database table for the filepath of that component title using these lines of code:
String filePath = dr1[0].ToString(); //GETS THE FILEPATH FROM DATABASE
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String disHeader = "Attachment; Filename=\"" + filePath + "\"";
HttpContext.Current.Response.AppendHeader("Content-Disposition", disHeader);
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(filePath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(fileToDownload.FullName);
Am i doing it properly ? Is there a better/optimal way to do it ?
A user simply needs read access to download a file, so you can simply create a directory claled "Downloads" and place them in there.
You can ensure that people can't "browse" that directory by disabling Directory Browsing and not placing any default docs in there (index.html, default.aspx for example)
What you are currently doing looks like a fairly standard way for providing downloads off your site.
I can't think of something more "optimal".

Multi-lingual web application - how do I detect the user's language in ASP.NET?

I'm building an ASP.NET web application, and all of my strings are stored in a resource file. I'd like to add a second language to my application, and ideally, I'd like to auto-detect the user's browser language (or windows language) and default to that, instead of making them choose something besides English. Currently, I'm handling all the resource population manually, so adding a second resource file and language is trivial from my point of view, if I had an easy way to automatically figure out what language to display.
Has anybody done this, or do you have any thoughts about how I might retrieve that value? Since ASP.NET is server-based, I don't seem to have any access to specific browser settings.
RESOLUTION: Here's what I ended up doing. I used a "For Each" to go through "HttpContext.Current.Request.UserLanguages" and search for one I support. I'm actually just checking the left two characters, since we don't support any dialects yet - just English and Spanish. Thanks for all the help!
Try this in the web.config:
<globalization culture="auto" uiCulture="auto" />
This will cause ASP.NET to auto-detect the client's culture from the request header. You can also set this on a per-page basis via the Page attribute.
This article (linked to archive.org as original link is now dead) might be helpful with auto detecting the browser's language setting.
[EDIT] Yes. The quoted article does not use ASP.NET. This article does.
Request.UserLanguages in ASP.NET 4 parses this as a string array.
Good info: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
This is a great question, as localization in ASP.NET is overlooked by many developers.
ASP.NET should automatically pick up on the user's browser settings and force the CultureInfo.CurrentCulture to the user's browser language. You can force the issue with a line in Page_OnInit() like:
Thread.CurrentThread.CurrentCulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);
How can you test this? Enter the languages panel on our browser and change settings.
The client generally sets Accept-Language in the HTTP request header with a quantitatively scored list of preferred language, conventionally (but not necessarily) in order of most favored to least favored. You can parse that, but as Maxam has noted, ASP.NET does have a mechanism for doing that on your behalf.
/// <summary>
/// Sets a user's Locale based on the browser's Locale setting. If no setting
/// is provided the default Locale is used.
/// </summary>
public static void SetUserLocale(string CurrencySymbol, bool SetUiCulture)
{
HttpRequest Request = HttpContext.Current.Request;
if (Request.UserLanguages == null)
return;
string Lang = Request.UserLanguages[0];
if (Lang != null)
{
// *** Problems with Turkish Locale and upper/lower case
// *** DataRow/DataTable indexes
if (Lang.StartsWith("tr"))
return;
if (Lang.Length < 3)
Lang = Lang + "-" + Lang.ToUpper();
try
{
System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo(Lang);
if (CurrencySymbol != null && CurrencySymbol != "")
Culture.NumberFormat.CurrencySymbol = CurrencySymbol;
System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
if (SetUiCulture)
System.Threading.Thread.CurrentThread.CurrentUICulture = Culture;
}
catch
{ ;}
}
}
The source of this article is here:
How to detect browser language

Resources