2

我在 C# win 应用程序中使用以下代码更改语言(例如 changelanguge("en") )我如何在 asp 中使用这样的东西?

public void changelanguge(String languge)
        {
            foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
            {
                if (lang.Culture.TwoLetterISOLanguageName == languge)
                {
                    Application.CurrentCulture = lang.Culture;
                    Application.CurrentInputLanguage = lang;
                }
            }
        }
4

1 回答 1

1

选项1

在 ASP.NET 页面 (ASPX) 中,您需要重写该InitializeCulture方法:

    protected override void InitializeCulture()
    {
        this.UICulture = "";
        this.Culture = "";
    }

选项 2

在页面的标记中:

<%@ Page UICulture="" Culture=""

选项 3

在 web.config

<globalization culture="" uiCulture="" enableClientBasedCulture="true" />

选项 4

使用HttpModule(此示例使用 ASP.NET 配置文件来获取语言,您可以更改以从不同来源获取语言)

public class LocalizationModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        var application = sender as HttpApplication;
        var context = application.Context;
        var handler = context.Handler;
        var profile = context.Profile as CustomProfile;

        if (handler != null)
        {
            var page = handler as Page;

            if (page != null)
            {
                if (profile != null)
                {
                    if (!string.IsNullOrEmpty(profile.Language))
                    {
                        page.UICulture = profile.Language;
                        page.Culture = profile.Language;
                    }
                }
            }
        }
    }
}

然后你只需要在 web.config 文件中配置它:

<httpModules>
  <add name="Localization" type="Msts.Topics.Chapter06___Globalization_and_Accessibility.Lesson01___Globalization_and_Localization.LocalizationModule" />
</httpModules>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="Localization" type="Msts.Topics.Chapter06___Globalization_and_Accessibility.Lesson01___Globalization_and_Localization.LocalizationModule" />
    </modules>
  </system.webServer>
于 2012-10-08T22:15:59.230 回答