Impostor
Impostor

Reputation: 2050

System.Globalization.CultureTypes.FrameworkCultures obsolete

When I call

System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.FrameworkCultures).Where(c => c.Name.EndsWith( "CH"))

I get the result:

de-CH
fr-CH
it-CH

but Visual Studio says

[Obsolete("This value has been deprecated.  Please use other values in CultureTypes.")]
FrameworkCultures = 0x40

question: what's the alternative for this to determine the valid cultures of a country?

Upvotes: 2

Views: 141

Answers (4)

Sharan Arumugam
Sharan Arumugam

Reputation: 363

1/2

Use Nager.Country or equivalent for bordering countries

using System.Globalization;
using Nager.Country;

var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

var countryProvider = new CountryProvider();
var countries = countryProvider.GetCountries();

var borderCountries = countries.First(c => c.Alpha2Code == Alpha2Code.CH).BorderCountries
    .Select(language => new CultureInfo($"{language}-CH"))
    .Select(cultureInfo => cultureInfo.Name)
    .ToList();

var filteredCultures = cultures
    .Where(c => c.Name.EndsWith("CH"))
    .Where(c => borderCountries.Contains(c.Name))
    .ToList();

foreach (var culture in filteredCultures)
{
    Console.WriteLine(culture.Name);
}

Result

de-CH
fr-CH
it-CH

Reference

Nager.Country


2/2

Use additional filter like West European Countries or similar

  • e.g. Using GeoId to narrow down further
  • Disadvantage: Checking GeoId periodically
using System.Globalization;

var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

var filteredCultures = cultures
    .Where(c => c.Name.EndsWith("CH"))
    .Where(c =>
    {
        try
        {
            var parent = CultureInfo.GetCultureInfo(c.Parent.Name);
            var region = new RegionInfo(parent.Name);
            return region.GeoId < 119;
        }
        catch
        {
            return false;
        }
    })
    .ToList();

foreach (var culture in filteredCultures)
{
    Console.WriteLine(culture.Name);
}

Result

de-CH
fr-CH
it-CH

Reference

GeoId Table

Upvotes: 0

Iti Tyagi
Iti Tyagi

Reputation: 3661

The FrameworkCultures value is deprecated, and the recommended alternative is to use SpecificCultures or AllCultures. However, these values will return all specific or all cultures respectively, not limited to a specific country.

To determine the valid cultures for a specific country (e.g., ending with "CH" for Switzerland), you can use the SpecificCultures or AllCultures value of CultureTypes, and then filter the results based on the culture name. Here's an example of how you can achieve this:

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

public class Program
{
    public static void Main()
    {
        var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                                  .Where(c => c.Name.EndsWith("-CH"));

        foreach (var culture in cultures)
        {
            Console.WriteLine(culture.Name);
        }
    }
}

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

As one can see from manual https://learn.microsoft.com/en-us/dotnet/api/system.globalization.culturetypes?view=net-8.0

FrameworkCultures 64
This member is deprecated; using this value with GetCultures(CultureTypes) returns neutral and specific cultures shipped with the .NET Framework 2.0.

(bold is mine)

You, probably, want SpecificCultures

Cultures that are specific to a country/region.

using System.Globalization;

...

var result = CultureInfo
  .GetCultures(CultureTypes.SpecificCultures)
  .Where(c => c.Name.EndsWith("CH"));

Edit: note, that some extra cultures are added into .Net since .Net 2.0 Framework:

var result = CultureInfo
    .GetCultures(CultureTypes.SpecificCultures)
    .Where(c => c.Name.EndsWith("CH"))
    .Select(c => $"{c,8} : {c.EnglishName}");

Console.Write(string.Join(Environment.NewLine, result));

Output (.Net 8)

   de-CH : German (Switzerland)
   en-CH : English (Switzerland)
   fr-CH : French (Switzerland)
  gsw-CH : Swiss German (Switzerland)
   it-CH : Italian (Switzerland)
   pt-CH : Portuguese (Switzerland)
   rm-CH : Romansh (Switzerland)
  wae-CH : Walser (Switzerland)

Upvotes: 2

fatih
fatih

Reputation: 229

You can use these alternatives:

    var examp1 = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures).Where(c => c.Name.EndsWith("CH"));
    var examp2 = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures).Where(c => c.Name.EndsWith("CH"));

By the way you can see details in this link: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.culturetypes?view=net-8.0

Upvotes: 2

Related Questions