setting language/culture set the start of a page, rather than foreach date

K

Kevin Blount

I'm trying to create a multi-lingual ASP.NET (C#) page where text, etc
comes from a database, so that I don't have to maintain 6 copies of the
page. This is mostly working just fine, but I have a question about
displaying dates.

In ASP (original) I was able to SetLocale("fr-FR") or SetLocale("nl-NL")
at the start of the page based on an 'if' statement that determined
which language should be shown.

I understand that C# doesn't have "SetLocale", and that I need to
explore cultures. I found some code that might work, but had some questions:

Code:
CultureInfo myCulture = new CultureInfo("sl-SI", true);
myCulture.DateTimeFormat.DateSeparator = "/";
myCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
DateTime date = DateTime.Parse(datestr, myCulture);

- what namespace do I need for this to work?

- what other methods (? right term? kinda new to C#) can I add long side
DateSeparator and ShortDatePattern?

- can I set, via a 'switch' or 'if' different CultureInfo parameters? or
perhaps I can set a string to be "nl-NL" and use that as a parameter?

- is there a way to do with without using .Parse? Nothing against it,
just mostly curious

thanks

Kevin
 
G

Guest

Kevin,

CultureInfo is part of the System.Globalization Namespace.

When you create a CultureInfo object like you have in your example,
"CultureInfo myCulture = new CultureInfo("sl-SI", true);" you already have
the correct date formats for Slovenian - Slovenia. You do not need to
specify your own.

To have the page use the culutre you would want to add a line similar to
System.Threading.Thread.CurrentThread.CurrentCulture = myCulture;

You can use a switch statement to create the correct culture object and then
assing it to the current thread.

I'm not sure where the datestr variable comes from but
DateTime.Now.ToShortDateString might be what you are looking for since it
uses the CurrentCulture object for its output.

Hope this helps.

Michael
 
K

Kevin Blount

Thanks Michael, that did help :)
Kevin,

CultureInfo is part of the System.Globalization Namespace.

When you create a CultureInfo object like you have in your example,
"CultureInfo myCulture = new CultureInfo("sl-SI", true);" you already have
the correct date formats for Slovenian - Slovenia. You do not need to
specify your own.

To have the page use the culutre you would want to add a line similar to
System.Threading.Thread.CurrentThread.CurrentCulture = myCulture;

You can use a switch statement to create the correct culture object and then
assing it to the current thread.

I'm not sure where the datestr variable comes from but
DateTime.Now.ToShortDateString might be what you are looking for since it
uses the CurrentCulture object for its output.

Hope this helps.

Michael

Kevin Blount said:
I'm trying to create a multi-lingual ASP.NET (C#) page where text, etc
comes from a database, so that I don't have to maintain 6 copies of the
page. This is mostly working just fine, but I have a question about
displaying dates.

In ASP (original) I was able to SetLocale("fr-FR") or SetLocale("nl-NL")
at the start of the page based on an 'if' statement that determined
which language should be shown.

I understand that C# doesn't have "SetLocale", and that I need to
explore cultures. I found some code that might work, but had some questions:

Code:
CultureInfo myCulture = new CultureInfo("sl-SI", true);
myCulture.DateTimeFormat.DateSeparator = "/";
myCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
DateTime date = DateTime.Parse(datestr, myCulture);

- what namespace do I need for this to work?

- what other methods (? right term? kinda new to C#) can I add long side
DateSeparator and ShortDatePattern?

- can I set, via a 'switch' or 'if' different CultureInfo parameters? or
perhaps I can set a string to be "nl-NL" and use that as a parameter?

- is there a way to do with without using .Parse? Nothing against it,
just mostly curious

thanks

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top