Explicit Localization Syntax - One Works, Other Does Not

C

Chris Walls

We have created two (2) global resource files in App_GlobalResouces:
Global.resx
Global.es-MX.resx

In an ASP.NET page, we use two different syntaxes to set text on the page,
depending upon the context.
Syntax 1: <% =Resources.Global.QuickSearch%>
Syntax 2: <%$Resources:Global, QuickSearch%>

When we set our Thread.CurrentThread.CurrentUICulture and
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX"), those
controls using syntax 1 display Spanish, but those displaying syntax 2 still
display English.

To create the Global.es-MX.resx file, we copied the Global.resx and modified
the text. All of the keys are present. Also because of this, we thought if
it were a key issue, then how is the English text being found?

Any ideas on what we're doing wrong?
 
S

Steven Cheng[MSFT]

Hi Chris,

From your description, when you use the following two ways to display
localized Text in ASP.NET 2.0 web page(load from global resource), only the
<%= ...%> appraoch will work, correct?

Syntax 1: <% =Resources.Global.QuickSearch%>
Syntax 2: <%$Resources:Global, QuickSearch%>

As for this problem,I've performed some research and found the cause of the
problem. For the two kind of statements below:

Syntax 1: <% =Resources.Global.QuickSearch%>
====================
This is simple embeded inline code statement which is executeing at the end
of the page's lifecycle(rendering stage). So this code is the same as you
put some code in codebehind page event.

Syntax 2: <%$Resources:Global, QuickSearch%>
====================
This is a built-in ASP.NET 2.0 expression and this expression is determined
at earlier time, when the page is dynamically compiled and initialized.
Therefore, if you set the Thread.CurrentUICulture in page's codebehind
event, that's too late for this localization expression. If you want to
programmatically set the Thread's CultureInfo and want to make the
<%$Resources %> expression be wared of it, you need to put the code in an
earlier time during the page request's server-side processing. One good
place is the "BeginRequest" event of ASP.NET server-side pipeline, you can
use Global.asax to register this event's handler. e.g.

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
................
}

void Application_BeginRequest(object sender, EventArgs e)
{
CultureInfo ci = new CultureInfo("zh-CN");

Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

Context.Response.Write("<br/>Application_BeginRequest.......");
}
...................

<<<<<<<<<<<<<<

Thus, the <%$ REsources %> expression in Page can get awared of the
CultureInfo changing.

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Walls

Thanks for the information. I never ran across that in my searches and in
it seems that the far majority of the examples I found on this topic
reference Syntax #2, but don't mention the limitations below.

Thanks,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top