2

I have code like this:

@string.Format(Resources.JoinDate, myDate)

The resx entry for Resources.JoinDate is

 "This person joined on {0}"

I can localise the string, but how can I localise the date? I'd need this to be reusable across other parameter types, such as dates for other strings. For example a US date would be mm/dd/yy whilst a UK date would be dd/mm/yyy

I'd rather not pass a formatter to every date being sent into a resource. Is it possible to do this globally for any resx resource?

User101
  • 748
  • 2
  • 10
  • 29
  • Won't string.format should take care of this already? `myDate.ToString()` should be the localized date, no? – CodingIntrigue Feb 12 '14 at 12:47
  • Checkout http://stackoverflow.com/questions/5798908/how-to-produce-localized-date-string-with-cultureinfo – Tommy Grovnes Feb 12 '14 at 12:49
  • Hmm does .ToString() without any format provider use the loczalised date? So if my resx switches to German, say Resources.de.resx, will the date automatically pick up German date format too using .ToString()? – User101 Feb 12 '14 at 16:20

2 Answers2

2

The Format function have a overload that takes the culture. Try using that one.

string.Format(CultureInfo.CurrentCulture, Resources.JoinDate, myDate)

or

string.Format(Thread.CurrentThread.CurrentCulture, Resources.JoinDate, myDate)

Else, check this out: http://msdn.microsoft.com/en-us/library/ms228208(v=vs.100).ASPX

thomas
  • 1,399
  • 1
  • 17
  • 32
1

just turn on with a simple string formatter example:

myDate.ToString("d")
Paulo Lima
  • 1,238
  • 8
  • 8