2

I use below code for change language(for example changelanguge("en") ) in C# win application how can i use sth like this in asp ?

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

1 Answer 1

1

Option 1

In an ASP.NET page (ASPX) you need to override the InitializeCulture method:

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

Option 2

In a page's markup:

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

Option 3

In web.config

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

Option 4

Using an HttpModule (This sample uses ASP.NET profiles to get the language, you can change to get the language from a different source)

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;
                    }
                }
            }
        }
    }
}

Then you just need to configure it on the web.config file:

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

occur error and says : colud not load type=Msts.Topics.Chapter06___Gl... what should i use instead of it ? and how should i change language after these steps?what should i use for culture and UICulture ?
Your type implementing IHttpModule
means i write IHttpModule for type?
Just if you follow the option 4 approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.