turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

Language affects my dates #235

Closed rickardliljeberg closed 8 years ago

rickardliljeberg commented 8 years ago

So I have a project that required Arabic.

But when i switch to arabic it seems more than i bargained for is switches as I now get DateTimes with the year 1437 which is the Arabic calendars year.

If you look at this screenshot I tried to work around it https://db.tt/YVPReCBJ

I set us culture, send in a date in year 2015. But still, the datetime created is for year 1437.... any ideas?

I do not want arabic calendar as all as client told me that most arabic countries actually run by gregorian calendar.

turquoiseowl commented 8 years ago
LocalizedApplication.Current.SetPrincipalAppLanguageForRequestHandlers = null;

will prevent i18n from changing Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture.

Or you can hookup your own delegate to customize this.

rickardliljeberg commented 8 years ago

In my case I made all code call a custom Now function that solved it easiest for me

like so

 public static DateTime GregorianUTCNow()
        {
            DateTime returnVar;

        try
        {
            CultureInfo originalCulureInfoUI = CultureInfo.CurrentUICulture;
            CultureInfo originalCulureInfo = CultureInfo.CurrentCulture;
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                returnVar = DateTime.UtcNow;
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = originalCulureInfo;
            }
        }
        finally
        {
        }

        return returnVar;
    }
rickardliljeberg commented 8 years ago

Actually, scratch that.

In the end I had to go with your approach as otherwise I could not get it to recieve date fields´.