Support for secondary language in ASP.NET localization - asp.net

ASP.NET has build-in functionality to automatically load the correct language resource file based on the browser's preferred language.
For example, if a visitor has Danish (da) as his/her preferred language, and the website has a *.da.resx file, then that resource is used. When the preferred resource file is not available, ASP.NET falls back to the basic/default resource file (*.resx).
Now, browsers allow for more than one preferred languages to be specified. If the visitor would speak Danish, but added Norwegian as a second language to the browsers preferences (written Norwegian is 95% identical to Danish), then the browser would send "da,no;q=0.9" to the web server. However, it seems this secondary language is not supported by ASP.NET; *.no.resx will not be loaded if *.da.resx is not available.
Does anybody know how to support secondary languages?

Note that ASP.NET only automatically processes the first entry in the accept-languages header field to determine which culture to use if enableClientBasedCulture is enabled in the web.config file.
If you wish to extend this and examine other language entries (if any) in the accept-languages list to see if you have a match, you will have to do it programmatically and set the Thread.CurrentThread.CurrentUICulture(which the ResourceManager uses to find the appropriate localized resources) and Thread.CurrentThread.CurrentCulture (which will control how locale-aware datatypes will be presented and processed). This is explained here.

Related

What is the best IFormatProvider for Logging?

I have an installable ASP.Net application that is logging using Common.Logging.
When writing log messages ie:
_log.InfoFormat(CultureInfo.???, "{0}: Writing to the Log", DateTime.UtcNow);
I am confused about which culture I should be using. The result I want is that the log messages are formatted using the server's regional settings. That way when administrators install the application they have control over how their log messages are formatted.
There are fours options but none seem to be the correct choice:
CultureInfo.CurrentCulture: this seems like it would be the correct choice for a windows app but ASP.Net is changing this to match the user browsing the site - so it's appropriate for formatting content to display to the user but not for writing to the log.
CultureInfo.CurrentUICulture: similar problems to CurrentCulture - will match the current user browsing the site.
CultureInfo.InstalledUICulture: the system installed culture - appears to relate to the copy of windows installed but not alterable by the user. Apparently this one is a bit useless
CultureInfo.InvariantCulture: this is not user configurable. I am wondering if this is the best of the four options here though because at least it is consistent and won't vary by who is browsing the site/the media windows was installed from.
I am left wondering if I need to add explicit configuration in my web.config for the system culture so the installer can choose a culture. The answer I really want is whatever CultureInfo.CurrentCulture was before ASP.Net changed it to match the current user.
Note: I know I can turn off ASP.Net changing the CurrentCulture per user but the UI relies on this behaviour.
Admins like to have control over the logs. Telling them to go configure their server culture so that the logs are in a particular format doesn't seem like the best solution to me.
Adding a configuration option into web.config is a good idea. This will let the admin configure the log format as required.
Rather than ask the user, you could make an intelligent guess while installing using CultureInfo.CurrentCulture. If the user wants it to be something different, he can always change it.
If you do want to take it from CultureInfo.CurrentCulture rather than configuration, you can pick it during the Application_Start event, and keep it in the application context.

Arabic resource files not being picked by ASP.NET web site

I'm working on a project that has a web application module that generates localized emails upon request. The app requires html templates localization for several languages. I decided to go with asp.net resource files since the email layout is not likely to change and the amount of keys needed was relatively small.
I'm using Global Resources and have around 15 different resource files for the required locales. I have my default resource file in English ("EmailTemplates.resx") and then several localized resource files ("EmailTemplates.ru-RU.resx", "EmailTemplates.zh-CN.resx", "EmailTemplates.it-IT.resx", "EmailTemplates.id-ID.resx", etc).
Among the resource files, I have two arabic ones: "EmailTemplates.ar-AE.resx" which is actually in English and "EmailTemplates.ar-SA.resx" which is in Arabic.
All the emails for non Arabic locales work perfectly. The app picks up the required locale and uses the corresponding resource files. But when I try to request the Arabic ones, it always fall back to the default resource file.
The way I'm setting up the culture is based on the data requested, and not the browser settings:
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
While debugging I can see that the CultureInfo is properly set to "ar-SA" or "ar-AE" but when the app retrieves the corresponding resource value, it always picks up the default one.
I have entered Arabic data in the default resource file for testing purposes, and the Arabic text displays just fine, so I'm sure that it is not a font or HTML layout issue.
I have searched all around the Internet, proved different approaches, and no luck. The "ar-SA" and "ar-AE" are valid CultureInfo values and the app compiles without issues, but it seems that whenever I request the Arabic emails, the app can't find the Arabic resource files.
Any ideas? I'd really appreciate any hits and help !
Cheers!
Please use the language without the culture info
Eg. EmailTemplates.ar.resx
because the browser you are using might not have the Arabic in different cultures as i checked with my chrome .or try adding the language with that culture.example ar-AE ar-SA etc,But anyway it has the Arabic language without culture and it works.

Question about Page Output Caching in ASP.NET

I wanted to use this statement
<%# OutputCache Duration="20" Location="Any" VaryByParam="none"%>
for our homepage.
(this works by the way)
But there are multiple domains pointing to the same site, like mydomain.fr and mydomain.ch.
Then, in the basepage i set the culture of the site to fr-FR if they typed mydomain.fr, de-CH when they typed mydomain.ch etc.
I was wondering, as both url's would load the same page /default.aspx, is the page served the same for both users (so when .fr comes first, the .ch visitor sees the (cached) .fr page), or does the framework think and say: hey, mydomain.fr/default.aspx is not the same as mydomain.ch/default.aspx, even if it's the same fysical page, so let's NOT take the cached one and recreate (and cache) a new version?
I've read about varybyheader for the page output caching, but .fr vs .ch is not a header i think?
You could vary it by the HOST header, which would mean that each domain would have its own cache set.
The HOST header contains the hostname/domain name that the browser loaded; So, mydomain.fr or mydomain.ch, etc.
I would highly suggest that you don't automatically set the culture based on the domain they used to get to your site.
Instead, just respect the culture settings of the browser. One reason is that they could very well be going to your French site, but prefer things in English. The various versions of the browsers will have sent you their chosen culture settings.
Also, offer the users a little language icon (usually a flag) at the top of your site. This should allow them to change their language of choice.
As to your actual question: if you are implementing culture resources properly then you don't have to worry about caching. It's taken care of for you.

Asp.net Localization - Should I use a button?

This is a pretty basic question.
Since browsers have a culture setting that the web app uses to decide which locale to use, should I still have a "language" button for users to be able to override the culture?
Why or Why Not?
To me it doesn't make sense to have a button there if the user had already set their language in their system.
Yes, you should.
One of the reasons outlined on the ASP.NET Localisation page is that a user may have a preference for reading certain types of websites in a specific language (i.e. technical websites in English). This is a good reason.
There are other reasons as well (i.e. the translation may not be great, a different user than typical is using the given computer, etc, etc).

What is the .MSPX file extension?

I've noticed a lot of Microsoft sites have the *.MSPX extension. While I'm very familiar with ASP.NET, I've not seen this extension before.
Does anyone know what this identifies?
A few internet searches led me to http://www.microsoft.com/backstage/bkst_column_46.mspx, but it was a dead link. Fortunately, it was archived on the Wayback Machine and you can read it here:
http://web.archive.org/web/20040803120105/http://www.microsoft.com/backstage/bkst_column_46.mspx
The .MSPX extension is part of the "Microsoft Network Project," which according to the article above, is designed to give Microsoft's sites a consistent look-and-feel worldwide, as well as keep the design of the site seperate from the content. Here's the gist of the article:
The presentation framework includes a custom Web handler built in ASP.NET. Pages that use the presentation framework have the .mspx filename extension, which is registered in Microsoft Internet Information Services (IIS) on the Web servers. When one of the Microsoft.com Web servers receives a request for an .mspx page, this custom Web handler intercepts that call and passes it to the framework for processing.
The framework first checks to see whether the result is cached. If it is, the page is rendered immediately. If the page is not cached, the handler looks up the URL for that page in the table of contents provided by the site owner (see below) to determine where the XML content for the page is stored. The framework then checks to see if the XML is cached, and either returns the cached content or retrieves the XML from the data store identified in the table of contents file.
Within the file that holds the content for the page, XML tags identify the content template to be used. The framework retrieves the appropriate template and uses a series of XSLTs to assemble the page, including the masthead, the footer, and the primary navigational column, finally rendering the content within the content pane.
I think it's an XML based template system that outputs HTML. I think it's internal to MS only.
Well, a little googling found this:
The presentation framework includes a
custom Web handler built in ASP.NET.
Pages that use the presentation
framework have the .mspx filename
extension, which is registered in
Microsoft Internet Information
Services (IIS) on the Web servers.
When one of the Microsoft.com Web
servers receives a request for an
.mspx page, this custom Web handler
intercepts that call and passes it to
the framework for processing."
I'd like to find out more info though.
I love you guys, i was asking myself also many times, why MS uses .mspx and what it is at all?! :)
That time i couldn´t find any informations quickly and assumed it would just be something on top of asp.net or maybe not even that, because you should be able to assign the same asp.net cgi dll to .mspx also easy too ;)
But, surely, it can be anything.. also an "special" CGI itself (completely beside ASP.NET), which processes that request with much better / much more cache-use, easier editing and so on..
The end of the story was, that i came accross the view, that maybe it´s not important to know, what .mspx exactly is :)

Resources