Csharp/C Sharp by API/System.Globalization/CultureInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

CultureInfo.Calendar

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
       string[] cultures = new string[] { "en-US", "en-GB", "es-MX", "de-DE", "ja-JP" };
        using (TextWriter sw = Console.Out)
        {
            foreach (string c in cultures)
            {
                CultureInfo ci = new CultureInfo(c);
                sw.Write("    Calendars: ");
                sw.Write("      {0} (primary)", ci.Calendar.GetType().Name);
                foreach (Calendar cal in ci.OptionalCalendars)
                    sw.Write(", {0}", cal.GetType().Name);
                sw.WriteLine();
            }
        }
    }
}


CultureInfo.CreateSpecificCulture

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
        decimal money = 1250.75m;
        string[] cultures = { "en-US", "en-GB", "ja", "ar" };
        Console.OutputEncoding = Encoding.UTF8;
        foreach (string culture in cultures)
        {
            CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(culture);
            Console.WriteLine("{0} = {1}", cultureInfo.DisplayName,money.ToString("C", cultureInfo));
        }
    }
}


CultureInfo.CurrentCulture

 
using System;
using System.Globalization;
class MainClass
{
  static void Main(string[] args)
  {  
      int theInt = 0;    
    Console.WriteLine("IConvertible-ing System.Int32 to System.Byte");
    IConvertible itfConvert = (IConvertible)theInt;
    byte theByte = itfConvert.ToByte(CultureInfo.CurrentCulture);
    Console.WriteLine("Type code int converted to byte is: {0}", theByte.GetTypeCode());
    Console.WriteLine("Value of converted int: {0}", theByte);
  }
}


CultureInfo.CurrentUICulture

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
        CultureInfo current = CultureInfo.CurrentCulture;
        CultureInfo currentUI = CultureInfo.CurrentUICulture;
        Console.WriteLine("CurrentCulture = {0}", current);
        Console.WriteLine("CurrentUICulture = {0}", currentUI);
    }
}


CultureInfo.DateTimeFormat

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
       string[] cultures = new string[] { "en-US", "en-GB", "es-MX", "de-DE", "ja-JP" };
        using (TextWriter sw = Console.Out)
        {
            foreach (string c in cultures)
            {
                CultureInfo ci = new CultureInfo(c);
                DateTime dt = new DateTime(1980, 9, 12, 0, 30, 55);
                sw.WriteLine("Short Date Example: {0}", dt.ToString(ci.DateTimeFormat.ShortDatePattern, ci.DateTimeFormat));
                sw.WriteLine("Long Date Example: {0}", dt.ToString(ci.DateTimeFormat.LongDatePattern, ci.DateTimeFormat));
            }
        }
    }
}


CultureInfo.DisplayName

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
       string[] cultures = new string[] { "en-US", "en-GB", "es-MX", "de-DE", "ja-JP" };
        using (TextWriter sw = Console.Out)
        {
            foreach (string c in cultures)
            {
                CultureInfo ci = new CultureInfo(c);
                sw.WriteLine("{0}: {1}", ci.Name, ci.DisplayName);
                sw.WriteLine("    English Name: {0}", ci.EnglishName);
                sw.WriteLine("    Native Name: {0}", ci.NativeName);
                sw.WriteLine("    LCID: {0}", ci.LCID);
            }
        }
    }
}


CultureInfo.EnglishName

 
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    // show some basic information
    Console.WriteLine("Native Name: " + ci.NativeName);
    Console.WriteLine("English Name: " + ci.EnglishName);
  }
}


CultureInfo.GetCultureInfo(String name)

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
        CultureInfo current = CultureInfo.CurrentCulture;
        CultureInfo currentUi = CultureInfo.CurrentUICulture;
        try
        {
            CultureInfo mexicanSpanishCi = CultureInfo.GetCultureInfo("es-MX");
            Thread.CurrentThread.CurrentCulture = mexicanSpanishCi;
            Thread.CurrentThread.CurrentUICulture = mexicanSpanishCi;
            Console.WriteLine("{0}/{1}", Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture);
            Console.WriteLine("{0:C}, {1}", 1250.50m, DateTime.Now.ToLongDateString());
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = current;
            Thread.CurrentThread.CurrentUICulture = currentUi;
        }
    }
}


CultureInfo.GetCultures

 
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
      Console.WriteLine("{0} | {1}", ci.EnglishName, ci.Name);
    }
  }
}


CultureInfo.NativeName

 
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    // show some basic information
    Console.WriteLine("Native Name: " + ci.NativeName);
    Console.WriteLine("English Name: " + ci.EnglishName);
  }
}


CultureInfo.NumberFormat

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
       string[] cultures = new string[] { "en-US", "en-GB", "es-MX", "de-DE", "ja-JP" };
        using (TextWriter sw = Console.Out)
        {
            foreach (string c in cultures)
            {
                CultureInfo ci = new CultureInfo(c);
                double number = -100299.55;
                sw.WriteLine("    Currency: {0}", number.ToString("C", ci.NumberFormat));
            }
        }
    }
}


CultureInfo.ToString()

 
using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    DateTime dtNow = DateTime.Now;
    Double curOriginal = 12345.67;
    CultureInfo ci = new CultureInfo("en-US");
    string sLocalizedDate = dtNow.ToString("d", ci);
    string sLocalizedCur = curOriginal.ToString("c", ci);
    // print them out
    Console.WriteLine(sLocalizedDate);
    Console.WriteLine(sLocalizedCur);
  }
}


new CultureInfo(String cult)

 
using System;
using System.Globalization;
using System.Threading;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("en-US");

  }
}