简体   繁体   中英

Aspx and aspx.cs getting strings from different resx files

I created two resource files, "SOSResources.en.resx" and "SOSResources.pt.resx".

In my aspx page, when I try to use a string from these files as below, I get it from the standard English language resx file.

asp:Literal ID="btnDelete_Text" Text="<%$Resources:SOSResources, String1%>"

However, if I try to get the same string on code-behind as below, I get it from the Portuguese language file (which is the user language).

btnEdit_Text.Text = Resources.SOSResources.String1

The following code is used to handle this process. It is supposed to select the User's language, or English in case this info is not available. However, only the example from code-behind gets the user language. The example from aspx page always gets the strings from the English resx file.

protected override void InitializeCulture()
    {
        string lang;

        if (ActiveUser != null && ActiveUser.Language != null)
        {
            lang = ActiveUser.Language;
        }
        else
        {
            lang = "en";
        }

        Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        base.InitializeCulture();
    }

Can someone help me? How can I assure that in both cases the page will get the strings from the appropriate language? I'm not sure either if this is the correct way to perform globalization on .NET.

After many hours of research I found this example that worked for me:

https://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.85).aspx

In addition to that, there was another question which also helped me when trying to move the code to the Site:Master or to a PageBase.cs:

ASP.NET Web Page Globalization & Localization in Master Page C# 3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM