views:

33

answers:

2

I have two Resources files under App_GlobalResources

MyApp.resx
MyApp.sv.resx

for those who don't know: All languages will fallback to MyApp.resx except the Swedish UICulture will use the MyApp.sv.resx

and I have a simple page that shows 3 <asp:Label> in witch the Text property is called differently like:

    <i>using Resource.Write:</i><br />
    <asp:Label ID="Label1" runat="server" />
    <hr />

    <i>using HttpContext.GetGlobalResourceObject:</i><br />
    <asp:Label ID="Label2" runat="server" />
    <hr />

    <i>using Text Resources:</i><br />
    <asp:Label ID="Label3" runat="server" 
               Text="<%$ Resources:MyApp, btnRemoveMonitoring %>" />

    <p style="margin-top:50px;">
    <i>Current UI Culture:</i><br />
        <asp:Literal ID="litCulture" runat="server" />
    </p>

Label3 is the only one called on Page, the first 2 are set like:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Label1.Text = Resources.AdwizaPAR.btnRemoveMonitoring;
        Label2.Text = HttpContext.GetGlobalResourceObject("MyApp", "btnRemoveMonitoring").ToString();

        litCulture.Text = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
    }
}

if I use the browser language all works fine, but I want to override that setting and load the correct translation based on other input, so I need to overwrite the UICulture and for that I use:

protected void Page_Init(object sender, EventArgs e)
{
    Page.Culture = "en-US";
    Page.UICulture = "en-US";
}

witch is the same as:

protected void Page_Init(object sender, EventArgs e)
{
    System.Globalization.CultureInfo cinfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
    System.Threading.Thread.CurrentThread.CurrentCulture = cinfo;
    System.Threading.Thread.CurrentThread.CurrentUICulture = cinfo;
}

with all this, what I'm getting is this:

alt text

In other words I'm getting the correct localization only if I use code-behind to set the correct text, all inline localization simply uses the browser language.

What am I missing?

+1  A: 

Nightmare is over ...

Page_Init does not change the access to Global Resources, we need to override the initialization to the culure

protected override void InitializeCulture()
{
    //*** make sure to call base class implementation
    base.InitializeCulture();

    //*** pull language preference from profile
    string LanguagePreference = "en-US"; // get from whatever property you want

    //*** set the cultures
    if (LanguagePreference != null)
    {
        this.UICulture = LanguagePreference;
        this.Culture = LanguagePreference;
    }
}

Now all works correctly

alt text

balexandre
A: 

If don't want to change each page, you can set the culture in Global.asax

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim lang As String = "en-us"
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub
Eduardo Molteni
I inherit my pages from a single class... added there, but thanks for the heads up.
balexandre