Csharp/C Sharp/Development Class/CultureInfo I18N

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

Create Culture info for fr-FR

using System;
using System.Globalization;
using System.Threading;
class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo("fr-FR");
      DateTime MyDate = System.DateTime.Now;
      Thread.CurrentThread.CurrentCulture = MyCulture;
      Console.WriteLine(MyDate.ToLongDateString());
      Console.WriteLine(MyDate.ToLongTimeString());
      Console.WriteLine(MyDate.ToShortDateString());
      Console.WriteLine(MyDate.ToShortTimeString());
      
   }
}


Create culture info from CurrentUICulture

using System;
using System.Globalization;
using System.Threading;

class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo(Thread.CurrentThread.CurrentUICulture.LCID);
      NumberFormatInfo Vnf = new NumberFormatInfo();
      Console.WriteLine(123456.ToString("c", MyCulture));
      Vnf = MyCulture.NumberFormat;
      Vnf.CurrencyDecimalSeparator = ",";
      Vnf.CurrencyGroupSeparator = ".";
      Vnf.NumberDecimalSeparator = ",";
      Vnf.NumberGroupSeparator = ".";
      MyCulture.NumberFormat = Vnf;
      Console.WriteLine(123456.ToString("C", MyCulture));
   }
}


Create Culture info in es-US

using System;
using System.Globalization;
class Class1 {
  static void Main(string[] args) {
         CultureInfo MyCulture = new CultureInfo("es-ES");
         DateTimeFormatInfo Myfmt;
         DateTimeFormatInfo fmt1;
         Myfmt = MyCulture.DateTimeFormat;
         fmt1  = DateTimeFormatInfo.CurrentInfo;
         Console.WriteLine(Myfmt.GetMonthName(12));
         Console.WriteLine(fmt1.GetMonthName(12));
      }
}


Derive Number Format info from culture info

using System;
using System.Globalization;
using System.Threading;

class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo(Thread.CurrentThread.CurrentUICulture.LCID);
      NumberFormatInfo Vnf = new NumberFormatInfo();
      Vnf = MyCulture.NumberFormat;
      Vnf.CurrencyDecimalSeparator = ",";
      Vnf.CurrencyGroupSeparator = ".";
      Vnf.NumberDecimalSeparator = ",";
      Vnf.NumberGroupSeparator = ".";
      MyCulture.NumberFormat = Vnf;
      CultureInfo FRCulture = new CultureInfo("fr-FR");

      MyCulture.NumberFormat = FRCulture.NumberFormat;
      Console.WriteLine(123456.ToString("c", MyCulture));
   }
}


enumerates all available cultures

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example21_9.cs enumerates all available cultures
*/
using System;
using System.Globalization;
public class Example21_9 
{
  public static void Main() 
  {
    foreach (CultureInfo ci in
      CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
      Console.WriteLine("{0} | {1}", ci.EnglishName, ci.Name);
    }
  }
}


illustrates the CultureInfo class

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example21_6.cs illustrates the CultureInfo class
*/
using System;
using System.Globalization;
public class Example21_6 
{
  public static void Main() 
  {
    // create a CultureInfo object for the nl-BE culture
    CultureInfo ci = new CultureInfo("nl-BE");
    // show some basic information
    Console.WriteLine("Native Name: " + ci.NativeName);
    Console.WriteLine("English Name: " + ci.EnglishName);
    // get datetime formatting info
    DateTimeFormatInfo dtfi = ci.DateTimeFormat;
    Console.WriteLine("Long date pattern: " + dtfi.LongDatePattern);
    // get numeric formatting info
    NumberFormatInfo nfi = ci.NumberFormat;
    Console.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    Console.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
  }
}


illustrates the CultureInfo class 2

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example21_7.cs illustrates the CultureInfo class
*/
using System;
using System.Globalization;
using System.IO;
public class Example21_7 
{
  public static void Main() 
  {
    // create a CultureInfo object for the nl-BE culture
    CultureInfo ci = new CultureInfo("nl-BE");
    // create a file to hold the results
    FileStream outStream = File.Create("CultureInfo.txt");
    // use a StreamWriter to write data to the file
    StreamWriter sw = new StreamWriter(outStream);
    // show some basic information
    sw.WriteLine("Native Name: " + ci.NativeName);
    sw.WriteLine("English Name: " + ci.EnglishName);
    // get datetime formatting info
    DateTimeFormatInfo dtfi = ci.DateTimeFormat;
    sw.WriteLine("Long date pattern: " + dtfi.LongDatePattern);
    // get numeric formatting info
    NumberFormatInfo nfi = ci.NumberFormat;
    sw.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    sw.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
    // flush and close the file
    sw.Flush();
    sw.Close();
  }
}


illustrates the current cultures

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example21_8.cs illustrates the current cultures
*/
using System;
using System.Globalization;
using System.Threading;
public class Example21_8 
{
  public static void Main() 
  {
    // set the current culture for the executing thread
    Thread.CurrentThread.CurrentCulture=new CultureInfo("de-DE");
    // and display it
    Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
    // create another new culture
    CultureInfo ci = new CultureInfo("en-US");
    // prove that this doesn"t change the current culture
    Console.WriteLine(CultureInfo.CurrentCulture.Name);
    // set the current UI culture for the executing thread
    Thread.CurrentThread.CurrentUICulture=new CultureInfo("fr-FR");
    // and display it
    Console.WriteLine(Thread.CurrentThread.CurrentUICulture.Name);
    // create another new culture
    CultureInfo ci2 = new CultureInfo("en-US");
    // prove that this doesn"t change the current culture
    Console.WriteLine(CultureInfo.CurrentUICulture.Name);
  }
}


new CultureInfo("da-DK")

 
using System;
using System.Globalization;
public class NumParsingApp {
    public static void Main(string[] args) {
        CultureInfo dk = new CultureInfo("da-DK");
        int k = 12345;
        string w = k.ToString("c", dk);
        Console.WriteLine(w);
    }
}


new CultureInfo( "de-DE" )

using System;
using System.Globalization;
public class EntryPoint
{
    static void Main() {
        CultureInfo current  = CultureInfo.CurrentCulture;
        CultureInfo germany  = new CultureInfo( "de-DE" );
        CultureInfo russian  = new CultureInfo( "ru-RU" );
        double money = 123.45;
        string localMoney = money.ToString( "C", current );
        Console.WriteLine( localMoney+ " Local Money" );
        localMoney = money.ToString( "C", germany );
        Console.WriteLine( localMoney+ " German Money" );
        
        localMoney = money.ToString( "C", russian );
        Console.WriteLine( localMoney+ " Russian Money" );
    }
}


new CultureInfo( "ru-RU" )

using System;
using System.Globalization;
public class EntryPoint
{
    static void Main() {
        CultureInfo current  = CultureInfo.CurrentCulture;
        CultureInfo germany  = new CultureInfo( "de-DE" );
        CultureInfo russian  = new CultureInfo( "ru-RU" );
        double money = 123.45;
        string localMoney = money.ToString( "C", current );
        Console.WriteLine( localMoney+ " Local Money" );
        localMoney = money.ToString( "C", germany );
        Console.WriteLine( localMoney+ " German Money" );
        
        localMoney = money.ToString( "C", russian );
        Console.WriteLine( localMoney+ " Russian Money" );
    }
}


Print all Culture info

using System;
using System.Globalization;
using System.Threading;

class Class1 {
  static void Main(string[] args) {
      CultureInfo[] AllCultures;
      RegionInfo Rg;
      AllCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
      foreach (CultureInfo ACulture in AllCultures) {
         Rg = new RegionInfo(ACulture.LCID);
         Console.WriteLine(Rg.TwoLetterISORegionName.ToString().PadRight(5, " ") + 
               "," + Rg.EnglishName.PadRight(40, " ") + 
               "," + ACulture.LCID.ToString());
      }
   }
}


Set Culture to current thread

using System;
using System.Globalization;
using System.Threading;
class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo("fr-FR");
      DateTime MyDate = System.DateTime.Now;
      Thread.CurrentThread.CurrentCulture = MyCulture;
      DateTimeFormatInfo dtf = MyCulture.DateTimeFormat;
      //Change date and time separator
      dtf.DateSeparator = "\\";
      dtf.TimeSeparator = "&";
      Console.WriteLine();
      Console.WriteLine(MyDate.ToLongDateString());
      Console.WriteLine(MyDate.ToLongTimeString());
      Console.WriteLine(MyDate.ToShortDateString());
      Console.WriteLine(MyDate.ToShortTimeString());
   }
}