Globalization Localization Nightmare

I have two resource files under App_GlobalResources

MyApp.resx
MyApp.sv.resx

for those who do not know: all languages ​​will recede to MyApp.resx , except . UICulture from Sweden will useMyApp.sv.resx

and I have a simple page that shows 3 <asp:Label>in that the property Textis called differently:

    <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 the page, the first 2 are set as:

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 , everything works fine, but I want to override this parameter and load the correct translation based on another input, so I need to overwrite UICulture, and for this I

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 I get the following:

alt text

, code-behind , inline .

?

+3
2

...

Page_Init , override 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;
    }
}

alt text

+3

, 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
0

Source: https://habr.com/ru/post/1766961/


All Articles