valid culturename check

  • Thread starter Thread starter alexandervanveelen
  • Start date Start date
A

alexandervanveelen

Hi all!

I'm working on a project where I have to check if a given string . a
valid culture name is.

Is there a way in .NET to do this very easy? The function I wrote for
this solution is not very elegant.

Can somebody help me out?
 
I'm working on a project where I have to check if a given string . a
valid culture name is.

Is there a way in .NET to do this very easy? The function I wrote for
this solution is not very elegant.

CultureInfo.GetCultureInfo(name)
throws an exception if name is a not a supported CultureInfo.
 
   CultureInfo.GetCultureInfo(name)
throws an exception if name is a not a supported CultureInfo.

But that is not very elegant is it?
 
Maybe something like this?

private static string[] m_CultureNames;
public static bool IsCultureNameValid(string cultureNameToValidate)
{
if (m_CultureNames == null)
{
CultureInfo[] allCultures =
CultureInfo.GetCultures(CultureTypes.AllCultures);
m_CultureNames = new string[allCultures.Length];
for (int i = 0; i < allCultures.Length; i++)
{
CultureInfo ci = allCultures;
m_CultureNames = ci.Name;
}
}

int index = Array.IndexOf<string>(m_CultureNames,
cultureNameToValidate);
return (index != m_CultureNames.GetLowerBound(0) - 1);
}


   CultureInfo.GetCultureInfo(name)
throws an exception if name is a not a supported CultureInfo.

        Martin Honnen --- MVP XML
       http://JavaScript.FAQTs.com/

But that is not very elegant is it?[/QUOTE]
 
Hi all!

I'm working on a project where I have to check if a given string . a
valid culture name is.

Is there a way in .NET to do this very easy? The function I wrote for
this solution is not very elegant.

Can somebody help me out?

If you can target .NET 3.5, you could try this:

using System;
using System.Linq;
using System.Globalization;

// [...]
bool IsValidCultureName(string cultureName)
{
return CultureInfo.GetCultures(CultureTypes.AllCultures)
.Any(ci => ci.Name == cultureName);
}

Regards,
Gilles.
 

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

Back
Top