Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

change System.Threading.Thread.CurrentThread in startup #534


User avatar
0
omital created

Hi, I try to change System.Threading.Thread.CurrentThread in project startup

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Enable the application to use a cookie to store information for the signed in user
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });

            // Use a cookie to temporarily store information about a user logging in with a third party login provider
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
        }
    }

but it does not work. also I set this in AbpModule's Preinitialize class.

public override void PreInitialize()
        {
            //Add/remove languages for your application

            Configuration.Localization.Languages.Add(new LanguageInfo("fa-IR", "فارسی", "famfamfam-flag-ir", true));
            Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", false));
            

            Configuration.Localization.Sources.Extensions.Add(
     new LocalizationSourceExtensionInfo("AbpWeb",
         new XmlFileLocalizationDictionaryProvider(
             HttpContext.Current.Server.MapPath("~/Localization/AbpWebExtensions")
             )
         )
     );

but it did not work and lang attribute in html tag is en


8 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Setting CurrentThread.CurrentCulture on startup can not work because it's a thread-specific property as you see. Every request uses a different thread. So, you should set it for every request.

    Add this code to your Global.asax file:

    protected override void Application_BeginRequest(object sender, EventArgs e)
    {
        base.Application_BeginRequest(sender, e);
    
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
    }
    

    But you say HTML's lang attribute.. maybe it's hard-coded to "en", have you checked it?

  • User Avatar
    0
    omital created

    Hi, Thank you for your response. Yes I Checked it and it was

    lang=@System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName>
    

    in layout.cshtml page. I want to when user visit site for the first time ,[lang] attribute be "fa-ir" and then user can change it from language menu.

  • User Avatar
    0
    ddnils created

    have you tried changing

    <html lang="en">
    

    to

    <html lang="fa-ir">
    

    (in _Layout.cshtml)

    • This should set your primary language.
  • User Avatar
    0
    omital created

    Yes this should, but it is hard code and Language menu (user select language from drop down menu) does not work anymore. Dose any other suggestion?

  • User Avatar
    0
    hikalkan created
    Support Team

    Have you added

    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");

    to Application_BeginRequest as I write before?

    This always sets current thread to the given culture. So, in layout, @System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName should get the true language. If not, what is System.Threading.Thread.CurrentThread.CurrentCulture in your controller?

  • User Avatar
    0
    omital created

    Hi. I use webApi+AngularJS and can not find "Application_BeginRequest ". if use your solution does menu language work correctly?

  • User Avatar
    0
    hikalkan created
    Support Team

    How to initialize AbpBootstrapper? Your Web project should contain a global.asax. Or you are using just Owin middleware. Where and how to host your layout.cshtml file (is it cshtml or plain html). If you have a cshtml file, you should have an MVC application.

  • User Avatar
    0
    omital created

    Thank you. I Added

    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
    

    to Application_Start and work.