Csharp/CSharp Tutorial/I18N Internationalization/Culture Format — различия между версиями

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

Текущая версия на 12:14, 26 мая 2010

An culture-friendly money matching expression

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
   public static void Main(){
        string[] money = new string[] { "$0.99", "$0,99", "$1000000.00", "$10.25", "$90,000.00", "$90.000,00", "$1,000,000.00", "$1,000000.00" };
        NumberFormatInfo info = CultureInfo.CurrentCulture.NumberFormat;
        Regex moneyint = new Regex(String.Format(@"\{0}(\d{{1,3}}\{0})*\d+\{1}\d{{2}}",
            info.CurrencyGroupSeparator, info.CurrencyDecimalSeparator));
        foreach (string m in money)
        {
            Console.WriteLine("{0}: {1}", m, moneyint.IsMatch(m));
        }
   }
}
$0.99: False
$0,99: False
$1000000.00: False
$10.25: False
$90,000.00: True
$90.000,00: False
$1,000,000.00: True
$1,000000.00: True

Culture formatting: en-GB

using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    DateTime dtNow = DateTime.Now;
    Double curOriginal = 12345.67;
    CultureInfo ci2 = new CultureInfo("en-GB");
    string sLocalizedDate2 = dtNow.ToString("d", ci2);
    string sLocalizedCur2 = curOriginal.ToString("c", ci2);
    Console.WriteLine(sLocalizedDate2);
    Console.WriteLine(sLocalizedCur2);
  }
}
25/03/2007
?12,345.67

Culture formatting: en-US

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);
  }
}
3/25/2007
$12,345.67

Culture information: Currency Format

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));
            }
        }
    }
}
Currency: ($100,299.55)
    Currency: -?100,299.55
    Currency: -$100,299.55
    Currency: -100.299,55 �
    Currency: -?100,300

Culture information: Days

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("    Days: ");
                foreach (string d in ci.DateTimeFormat.DayNames)
                    sw.Write(", {0}", d);
                sw.WriteLine();
            }
        }
    }
}
Days: , Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
    Days: , Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
    Days: , domingo, lunes, martes, mi�rcoles, jueves, viernes, s�bado
    Days: , Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag
    Days: , ???, ???, ???, ???, ???, ???, ???

Culture information: DisplayName, NativeName and LCID

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);
            }
        }
    }
}
en-US: English (United States)
    English Name: English (United States)
    Native Name: English (United States)
    LCID: 1033
en-GB: English (United Kingdom)
    English Name: English (United Kingdom)
    Native Name: English (United Kingdom)
    LCID: 2057
es-MX: Spanish (Mexico)
    English Name: Spanish (Mexico)
    Native Name: Espa?ol (M�xico)
    LCID: 2058
de-DE: German (Germany)
    English Name: German (Germany)
    Native Name: Deutsch (Deutschland)
    LCID: 1031
ja-JP: Japanese (Japan)
    English Name: Japanese (Japan)
    Native Name: ??? (??)
    LCID: 1041

Culture information: Long Date Format and Short Date Format

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));
            }
        }
    }
}
Short Date Example: 9/12/1980
Long Date Example: Friday, September 12, 1980
Short Date Example: 12/09/1980
Long Date Example: 12 September 1980
Short Date Example: 12/09/1980
Long Date Example: viernes, 12 de septiembre de 1980
Short Date Example: 12.09.1980
Long Date Example: Freitag, 12. September 1980
Short Date Example: 1980/09/12
Long Date Example: 1980?9?12?

Culture information: Months

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("    Months: ");
                foreach (string m in ci.DateTimeFormat.MonthNames)
                    sw.Write(", {0}", m);
                sw.WriteLine();
            }
        }
    }
}
Months: , January, February, March, April, May, June, July, August, September, October, November
, December,
    Months: , January, February, March, April, May, June, July, August, September, October, November
, December,
    Months: , enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviemb
re, diciembre,
    Months: , Januar, Februar, M?rz, April, Mai, Juni, Juli, August, September, Oktober, November, D
ezember,
    Months: , 1?, 2?, 3?, 4?, 5?, 6?, 7?, 8?, 9?, 10?, 11?, 12?,

Culture information: Number Format

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("    Number Format: {0}", number.ToString(ci));
            }
        }
    }
}
Number Format: -100299.55
    Number Format: -100299.55
    Number Format: -100299.55
    Number Format: -100299,55
    Number Format: -100299.55

Culture information: Number Group Separator

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(ci.NumberFormat.NumberGroupSeparator);
            }
        }
    }
}
,
,
,
.
,

Culture information: primary Calendar and optional 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();
            }
        }
    }
}
Calendars:       GregorianCalendar (primary), GregorianCalendar, GregorianCalendar
    Calendars:       GregorianCalendar (primary), GregorianCalendar, GregorianCalendar
    Calendars:       GregorianCalendar (primary), GregorianCalendar
    Calendars:       GregorianCalendar (primary), GregorianCalendar
    Calendars:       GregorianCalendar (primary), GregorianCalendar, JapaneseCalendar, GregorianCale
ndar

Culture-specific formatting: en-US, en-GB, ja, ar

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));
        }
    }
}

Get datetime formatting info from a CultureInfo

using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    
    DateTimeFormatInfo dtfi = ci.DateTimeFormat;
    Console.WriteLine("Long date pattern: " + dtfi.LongDatePattern);
  }
}
Long date pattern: dddd d MMMM yyyy

Get numeric formatting info from a CultureInfo

using System;
using System.Globalization;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    NumberFormatInfo nfi = ci.NumberFormat;
    Console.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    Console.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
  }
}
Currency symbol: 
Decimal seperator: ,

Implement Format for different Culture Information

using System;
using System.Globalization;
public sealed class ComplexNumber : IFormattable
{
    public ComplexNumber( double real, double imaginary ) {
        this.real = real;
        this.imaginary = imaginary;
    }
    public override string ToString() {
        return ToString( "G", null );
    }
    public string ToString( string format, IFormatProvider formatProvider ) {
        string result = "(" + real.ToString(format, formatProvider) + " " + real.ToString(format, formatProvider) + ")";
        return result;
    }
   
    private readonly double real;
    private readonly double imaginary;
}
public sealed class MainClass
{
    static void Main() {
        ComplexNumber num1 = new ComplexNumber( 1.12345678, 2.12345678 );
        
        Console.WriteLine( "US format: {0}", num1.ToString( "F5", new CultureInfo("en-US") ) );
        Console.WriteLine( "DE format: {0}", num1.ToString( "F5", new CultureInfo("de-DE") ) );
        Console.WriteLine( "Object.ToString(): {0}",num1.ToString() );
    }
}

Save CultureInfo, its DateTimeFormatInfo and NumberFormatInfo to a text file

using System;
using System.Globalization;
using System.IO;
class MainClass
{
  public static void Main() 
  {
    CultureInfo ci = new CultureInfo("nl-BE");
    FileStream outStream = File.Create("CultureInfo.txt");
    StreamWriter sw = new StreamWriter(outStream);
    sw.WriteLine("Native Name: " + ci.NativeName);
    sw.WriteLine("English Name: " + ci.EnglishName);
    DateTimeFormatInfo dtfi = ci.DateTimeFormat;
    sw.WriteLine("Long date pattern: " + dtfi.LongDatePattern);
    NumberFormatInfo nfi = ci.NumberFormat;
    sw.WriteLine("Currency symbol: " + nfi.CurrencySymbol);
    sw.WriteLine("Decimal seperator: " + nfi.NumberDecimalSeparator);
    sw.Flush();
    sw.Close();
  }
}

Use current culture to format currency

using System;
using System.Globalization;
using System.Windows.Forms;
public class MainClass
{
    static void Main() {
        CultureInfo current  = CultureInfo.CurrentCulture;
        double money = 123.45;
        string localMoney = money.ToString( "C", current );
        MessageBox.Show( localMoney, "Local Money" );
    }
}