4

Website show wrong month for julyin arabic, this is Arabic website developed in asp.net webform and culture is set properly to 'ar-AE' and event date shows correct month on local machine but on productions it show month which belongs to Egyptian culture

Date in database is stored as smalldatetime in this format 2017-07-25 00:00:00

Wrong month for July

Correct month on local-host :25 يوليو 2017

Wrong month on production : 25 يوليه 2017

<asp:Label  ID="lblDate"  runat="server"  Text='<%# FormatDate(Eval("PublishDate")) %>'>
</asp:Label>

protected string FormatDate(object dt) {
  string date = String.Format("{0:MMMM dd, yyyy}", dt);
  date = String.Format("{0:dd MMMM yyyy}", dt);
  // Response.Write(date + "<br/>");
  return date;
}

Culture is set to UAE correctly

protected override void InitializeCulture() {
  String lang = "ar-AE";
  CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
  // Call function to detect Language and assign  to session variable
  Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
  Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

  base.InitializeCulture();
}

What could be the reason, could it be production server itself?

We check almost all possibilities and tried everything, it just gets wrong month for July rest all is ok.

I also cross check production server Language setting, IIS settings, it all seems to be okay, only thing i can point is that server has all latest updates, may be we are missing something. we tested same on different windows 7 machine it just works fine.

Update: I have tried solution suggest also but it keeps showing wrong month for JULY يوليه. This is wrong. We tested it also it show the correct culture on the server.

Fiddle example also show wrong month https://dotnetfiddle.net/ZAOJ7H

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
Learning
  • 19,469
  • 39
  • 180
  • 373
  • The object passed to `FormatDate` is a `DateTime`? _"Date in database is stored in this format"_ A date is never stored in a format in the database. It **should** be stored as a `datetime` or `date` which has no format. If it was stored as a string(varchar) you should fix that first. – Tim Schmelter Oct 11 '17 at 09:03
  • I'd suggest explicitly using the `string.Format` overload that accepts a `CultureInfo` as a `IFormatProvider`, or to set the culture in the `Web.config` file as described here: https://forums.asp.net/t/1318941.aspx?How+to+Set+default+language+in+web+config+file+ – Martin Costello Oct 11 '17 at 09:04
  • @MartinCostello: Should be unncessary. OP is already using a recommended way which should work – Tim Schmelter Oct 11 '17 at 09:06
  • There could be some thread-related subtlety going on that isn't visible from the code that's presented causing the difference, which is why I suggested it. – Martin Costello Oct 11 '17 at 09:19
  • @TimSchmelter, object which i pass to FormaDate is DateTime `2017-07-25 00:00:00` – Learning Oct 11 '17 at 09:20
  • Updated question with more details for clarity – Learning Oct 11 '17 at 09:24

2 Answers2

3

I tried to find localized DateTime month name by using these lines of code:

DateTime dt = DateTime.Parse("2017-07-25 00:00:00");

string arabicMonth = new CultureInfo("ar-AE").DateTimeFormat.GetMonthName(dt.Month);

The resulted code gives يوليه instead of يوليو, indicating something went wrong in Windows localization of Gregorian month names in Arabic language.

By finding out a little bit, it caused by locale.nls file located in %WINDIR%\System32\locale.nls containing incorrect data where the month of July is displayed in other Arabic version than current system culture using.

Since NLS file is formatted in binary, it's hard to read and edit stored locale information even viewing it in hex editor. AFAIK, this problem already fixed by Microsoft in KB981981 update for Windows 7 & Server 2008 R2 systems, you can perform update depending on platform used (requires restart afterwards):

Update for Windows 7 32-bit (KB981981) - 5/24/2010

Update for Windows 7 64-bit (KB981981) - 5/24/2010

NB: Ensure that all affected machines installed with this update for best result. If you can't install the update due to certain restrictions, I suggest to try using jQuery plugin for datepicker which shows يوليو for month of July as workaround.

PS: I also tried zahed's solution in the OP's fiddle -

DateTime.Parse("2017-07-25 00:00:00").ToString("dd dddd , MMMM, yyyy", new CultureInfo("ar-AE"))

But it shows 25 الثلاثاء , يوليه, 2017 (i.e. يوليه) too, hence it's not the string formatting problem, instead the Windows localization problem as explained above.

References:

Description of Software Update Services and Windows Server Update Services changes in content for 2010

How to convert the month name in english text in datetime to arabic text using C#?

Related:

Michael S. Kaplan - Arabic locales for month names

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I am not sure if this is the 100 Solution but i am sure it has to do something with OS or updates. Updates you are pointing to are very old & for windows seven while i am facing issue on windows server 2102 R2 version and also on fiddle what about that. Anyways i will mark your anyways as it is better explained but may not to the actual solution .https://dotnetfiddle.net/ZAOJ7H – Learning Oct 24 '17 at 08:45
0

Design code:

<div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</div>

Code behind C#:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Label1.Text = DateTime.Parse("2017-07-25 00:00:00").ToString("dd dddd , MMMM, yyyy", new CultureInfo("ar-AE"));
}

Updated answer:

I would imagine all languages that the OS supports (considering that is where the information comes from).

Here is a list: default supported languages by OS.

Mr doubt
  • 51
  • 1
  • 10
  • 42
  • Which machine you have tested on OS, version as it works for us also on two local machine with windows 7 but not on windows 2012 R2 version. even fiddles also generates wrong month name https://dotnetfiddle.net/ZAOJ7H – Learning Oct 24 '17 at 08:42
  • can you tell me the windows version in production server ? I will update you the answer. @Learning – Mr doubt Oct 24 '17 at 09:31
  • Please check my updated answer will help to out your issue. @Learning – Mr doubt Oct 24 '17 at 09:39