CultureInfo 클래스 사용

CultureInfo 클래스에는 언어, 국가/지역, 달력 및 문화권 규칙 등의 문화권별 정보가 들어 있습니다. 또한 이 클래스는 대/소문자 구분, 날짜 및 숫자 형식 지정, 문자열 비교 등과 같은 문화권 관련 작업을 수행하는 데 필요한 정보를 제공합니다.

CultureInfo 클래스는 각 문화권의 고유한 이름을 지정합니다. 문화권 이름 목록은 CultureInfo 클래스의 설명을 참조하십시오. 응용 프로그램에서는 GetCultures 메서드를 사용하여 모든 문화권의 전체 목록을 검색할 수 있습니다. 다음 예제에서는 모든 문화권 목록을 표시합니다.

Imports System
Imports System.Globalization

public class printClass
   Public Shared Sub Main()  
      Dim ci As CultureInfo
      For Each ci in _
      CultureInfo.GetCultures(CultureTypes.AllCultures)
         Console.WriteLine(ci)
      Next ci
   End Sub
End Class
using System;
using System.Globalization;

public class printClass
{
   public static void Main()
   {
      foreach (CultureInfo ci in
      CultureInfo.GetCultures(CultureTypes.AllCultures))
      {
         Console.WriteLine(ci);
      }
   }
}

비관리 코드와 CultureInfo 사용

참고참고

.NET Framework 응용 프로그램은 플랫폼 호출 서비스를 사용하여 동적 연결 라이브러리의 관리되지 않는 함수에 액세스할 수 있습니다.

국가/지역 정보를 초기화하려면 응용 프로그램에서 CultureInfo 개체에 국가/지역에 해당하는 RegionInfo 개체를 전달합니다. 비관리 코드의 경우에는 응용 프로그램에서 Win32 함수 GetLocaleInfo에 CultureInfo 개체를 전달할 수도 있습니다.

CultureInfo 개체를 사용하여 RegionInfo 개체를 초기화하려면 응용 프로그램에서 "ar-DZ"로 지정되는 아라비아어(알제리) 문화권 등의 특정 문화권을 나타내는 CultureInfo 개체를 지정해야 합니다. 아라비아어("ar")와 같은 중립 문화권을 나타내는 CultureInfo 개체로 RegionInfo 개체를 초기화하려고 하면 예외가 throw됩니다. 중립 문화권은 국가/지역에 매핑하는 데 필요한 국가 또는 지역 정보를 지정하지 않습니다.

GetLocaleInfo 메서드는 특정 문화권이나 중립 문화권을 나타내는 CultureInfo 개체의 국가/지역을 반환한다는 점에서 RegionInfo 생성자와 다릅니다. 예를 들어 응용 프로그램에서 아라비아어 중립 문화권을 나타내는 CultureInfo 개체를 GetLocaleInfo에 전달하면 메서드에서 중립 문화권을 관련된 기본 국가/지역에 매핑합니다. 이 경우 GetLocaleInfo는 사우디아라비아를 검색합니다. GetLocaleInfo 메서드가 제공하는 기본 국가/지역 매핑이 해당 응용 프로그램에서 문화적으로 부적합할 수 있으므로 이 메서드를 주의하여 사용해야 합니다. 이러한 불일치 상태를 없애려면 응용 프로그램에서 API 함수와 상호 운용될 때 특정 문화권만 사용하도록 해야 합니다.

다음 예제에서는 동일한 CultureInfo 개체에 대해 RegionInfo 클래스 생성자와 GetLocaleInfo 메서드가 서로 다른 값을 검색함을 보여 줍니다. CultureInfo 개체가 특정 문화권인 아라비아어(알제리)를 나타낼 때는 두 메서드 모두 알제리를 국가/지역으로 반환합니다. 그러나 CultureInfo 개체가 아라비아어 중립 문화권을 나타낼 때는 결과가 서로 다릅니다. GetLocaleInfo는 알제리를 검색하지만 RegionInfo 생성자는 국가/지역을 검색하지 못합니다.

Imports System
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Namespace CountryRegionName
   Class CountryRegionName
      ' The name of a country or region in English.
      Private LOCALE_SENGCOUNTRY As Integer = &H1002

      ' Use COM interop to call the Win32 API GetLocalInfo.
      Declare Unicode Function GetLocaleInfoW Lib "Kernel32.dll" _
         (Locale As Integer, LCType As Integer,<[In](), _
          MarshalAs(UnmanagedType.LPWStr)> lpLCData As String, _
          cchData As Integer) As Integer
      
      ' A method to retrieve the .NET Framework Country/Region
      ' that maps to the specified CultureInfo.
      Public Function GetNetCountryRegionName(ci As CultureInfo) As String
         ' If the specified CultureInfo represents a specific culture,
         ' the attempt to create a RegionInfo succeeds.
         Try
            Dim ri As New RegionInfo(ci.LCID)
            Return ri.EnglishName
         ' Otherwise, the specified CultureInfo represents a neutral
         'culture, and the attempt to create a RegionInfo fails.
         Catch
            Return String.Empty
         End Try
      End Function
      
      ' A method to retrieve the Win32 API Country/Region
      ' that maps to the specified CultureInfo.
      Public Function GetWinCountryRegionName(ci As CultureInfo) As String
         Dim size As Integer = GetLocaleInfoW(ci.LCID, _
            LOCALE_SENGCOUNTRY, Nothing, 0)
         Dim str As New String(" "c, size)
         Dim err As Integer = GetLocaleInfoW(ci.LCID, _
            LOCALE_SENGCOUNTRY, str, size)
         ' If the string is not empty, GetLocaleInfoW succeeded.
         ' It will succeed regardless of whether ci represents
         ' a neutral or specific culture.
         If err <> 0 Then
            Return str
         Else
            Return String.Empty
         End If
      End Function

      <STAThread()> _
      Public Shared Sub Main(args() As String)
         Dim crn As New CountryRegionName()
         
         ' Create a CultureInfo initialized to the neutral Arabic culture.
         Dim ci1 As New CultureInfo(&H1)
         Console.WriteLine(ControlChars.NewLine + _
            "The .NET Region name: {0}", _
            crn.GetNetCountryRegionName(ci1))
         Console.WriteLine("The Win32 Region name: {0}", _
            crn.GetWinCountryRegionName(ci1))
         
         ' Create a CultureInfo initialized to the specific 
         ' culture Arabic in Algeria.
         Dim ci2 As New CultureInfo(&H1401)
         Console.WriteLine(ControlChars.NewLine + _
            "The .NET Region name: {0}", _
            crn.GetNetCountryRegionName(ci2))
         Console.WriteLine("The Win32 Region name: {0}", _
            crn.GetWinCountryRegionName(ci2))
      End Sub
   End Class
End Namespace
using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace CountryRegionName
{
  class CountryRegionName
  {
    // The name of a country or region in English
    int LOCALE_SENGCOUNTRY     = 0x1002;

    // Use COM interop to call the Win32 API GetLocalInfo.
    [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
    public static extern int GetLocaleInfo(
       // The locale identifier.
       int Locale,
       // The information type.
       int LCType,
       // The buffer size.
       [In, MarshalAs(UnmanagedType.LPWStr)] string lpLCData,int cchData
     );

    // A method to retrieve the .NET Framework Country/Region
    // that maps to the specified CultureInfo.
    public String GetNetCountryRegionName(CultureInfo ci)
    {
      // If the specified CultureInfo represents a specific culture,
      // the attempt to create a RegionInfo succeeds.
      try
      {
        RegionInfo ri = new RegionInfo(ci.LCID);
        return ri.EnglishName;
      }
      // Otherwise, the specified CultureInfo represents a neutral
      // culture, and the attempt to create a RegionInfo fails.
      catch
      {
        return String.Empty;
      }
    }

    // A method to retrieve the Win32 API Country/Region
    // that maps to the specified CultureInfo.
    public String GetWinCountryRegionName(CultureInfo ci)
    {
      int size = GetLocaleInfo(ci.LCID, LOCALE_SENGCOUNTRY, null, 0);
      String str = new String(' ', size);
      int err  = GetLocaleInfo(ci.LCID, LOCALE_SENGCOUNTRY, str, size);
      // If the string is not empty, GetLocaleInfo succeeded.
      // It will succeed regardless of whether ci represents
      // a neutral or specific culture.
      if(err != 0)  
        return str;
      else
        return String.Empty;
    }

    [STAThread]
    static void Main(string[] args)
    {
      CountryRegionName crn = new CountryRegionName();

      // Create a CultureInfo initialized to the neutral Arabic culture.
      CultureInfo ci1 = new CultureInfo(0x1);  
      Console.WriteLine("\nThe .NET Region name: {0}", 
         crn.GetNetCountryRegionName(ci1));
      Console.WriteLine("The Win32 Region name: {0}",
         crn.GetWinCountryRegionName(ci1));

      // Create a CultureInfo initialized to the specific 
      // culture Arabic in Algeria.
      CultureInfo ci2 = new CultureInfo(0x1401);  
      Console.WriteLine("\nThe .NET Region name: {0}", 
         crn.GetNetCountryRegionName(ci2));
      Console.WriteLine("The Win32 Region name: 
         {0}",crn.GetWinCountryRegionName(ci2));
    }
   }
}

이 예제는 다음과 같이 출력됩니다.

The .NET Region name:
The Win32 Region name: Saudi Arabia

The .NET Region name: Algeria
The Win32 Region name: Algeria

참고 항목

참조

CultureInfo

개념

CurrentUICulture 속성 사용

CurrentCulture 속성 사용

InvariantCulture 속성 사용

CultureInfo 개체에 연결된 이름

기타 리소스

인코딩 및 지역화