Professional Documents
Culture Documents
Hall Toc 2
Hall Toc 2
HANDBOOK
FOR THE MICROSOFT
.NET PLATFORM
A GUIDE FOR PROGRAMMERS,
ARCHITECTS AND QA PERSONNEL
PART II
BILL HALL
MLM ASSOCIATES, INC.
Globalization Handbook
for the Microsoft .NET Platform
by Bill Hall
Copyright ©2003-2005 by Bill Hall. All rights reserved. No part of this book may be
reproduced, stored in a retrieval system or transmitted in any form or means without the
prior written permission of the publisher and author, except in the instance of brief
quotations contained in critical articles or reviews.
The author and publisher of this book have made every effort to ensure the accuracy of
the information and the functionality of any code written to run on computing systems.
However, the information presented here is provided and sold without warranty either
expressed or implied. Neither the authors nor the publisher will be held liable for any
damages caused by or alleged to be caused by this book, either directly or indirectly.
MultiLingual Press
MultiLingual Computing, Inc.
319 N. First Avenue
Sandpoint, Idaho 83864
www.multilingual.com
GLOBALIZATION HANDBOOK
FOR THE MICROSOFT .NET PLATFORM
PART II
TABLE OF CONTENTS
The RegionInfo Class:
Constructors, Properties and Methods RI-1-12
Overview 1
Class Summary 1
Constructors 1
Using the Country Part of an RFC 1766 Identifier as Constructor Parameter 2
Public Methods 2
Properties 2
Simple Examples 4
Basic Console Demo Program 4
ISO 3166 List 6
Creating the List 6
Program Output 9
Adding a Localization Structure 9
Chapter Summary 12
Notes 12
Figures
Figure 1: Partial program output from RfcToISO3166.exe 3
Figure 2: Greek RegionInfo methods and properties 5
Figure 3: Partial ISO 3166 list organized by identifier 6
Figure 4: Partial ISO 3166 list organized by display name 6
Figure 5: Unsorted RegionInfo list with duplicates 7
Figure 6: Partial list of regions and ISO 3166 identifiers in (US) sorted order 7
Figure 7: Norwegian output 9
Figure 8: Swedish output with Swedish sort 9
Figure 9: Swedish output with English sort 9
Figure 10: Base resource file for ISO3166List 10
Figure 11: Improved alignment for Norwegian output 10
2–i
Globalization Handbook for the Microsoft .NET Platform
Listings
Listing 1: Getting a RegionInfo instance from CultureInfo 2
Listing 2: Getting the region identifier from an RFC 1766 ID 2
Listing 3: Comparing RFC 1766 country codes with ISO 3166 codes 2
Listing 4: Simple console RegionInfo demo program 4-5
Listing 5: Simple ISO 3166 generator 6
Listing 6: Code producing a sorted list of regions and ISO 3166 identifiers 7
Listing 7: Creating a list of ISO 3166 identifiers and region names 7-8
Listing 8: Loading a ResourceManager for the ISO3166List program 10
Listing 9: Loading the format String from Resources 10
Listing 10: Final form of ISO3166List.cs 10-11
Tables
Table 1: RegionInfo constructors 1
Table 2: Public methods of RegionInfo 3
Table 3: RegionInfo properties 3
Figures
Figure 1: RegionInfo console demo output for Norway 1-2
Figure 2: Resource strings in RegionInfoReflection.cs 5
2–ii
Part II Contents
Listings
Listing 1: Main routine in RegionInfoReflection.cs 2-3
Listing 2: DoProps in RegionInfoReflection.cs 3-4
Listing 3: DoMethods from RegionInfoReflection.cs 4-5
Listing 4: Custom RegionInfo dropdown list control 7-9
Listing 5: Loading the properties ListView control at program startup 11
Listing 6: Filling the center column of the properties ListView 12
Listing 7: Startup code to set UI culture 14
Listing 8: Program to create a .resx file for WinRegionInfoDemo.exe 16-17
Listing 9: Excerpt from AllRegionList.cs 18
Listing 10: Excerpt from the NameString class in RegionInfoList.cs 19
Listing 11: MyRgn class 19-20
Listing 12: Loading a dropdown list of RegionInfo objects 20
Listing 13: LoadProps in Windows CE code 21
Listing 14: GetProps in Windows CE code 21
Listing 15: RegionDisplayMembers enumeration. 24
Listing 16: Loading the user control RegionInfo list 24-25
2–iii
Globalization Handbook for the Microsoft .NET Platform
Formatting Fundamentals:
Simple and Composite Culturally Correct Formatting Methods for Objects FF-1-26
Overview 1
The Need for Culturally Correct Formatting 2
Formattable Objects and Format Providers 2
Simple Formatting of Objects 4
Extending ToString 5
Formatting Example: Complex Numbers 8
Recalling Complex Numbers 8
ComplexNumber Structure 8
Composite Formatting 16
The Need for Careful String Design 17
Composite Formatting Details in .NET 18
Index 18
Alignment 18
Format String 19
Composite Formatting Examples 19
String.Format Overloads 19
Basic Usage 19
The Importance of Using IFormattable 21
Spaceships on Mars 23
Chapter Summary 26
Notes 26
Figures
Figure 1: Output from OverloadedToString.exe 3
Figure 2: Obtaining stubs for IFormattable 12
Figure 3: Testing ComplexNumber 15-16
Figure 4: Resource files for the NameTag project 17
Figure 5: Output from NameTag.exe 17
Figure 6: Output from StringFormatDemo.exe 21
Figure 7: Output from PossessiveCase.exe 22
Figure 8: Java MessageFormat tutorial test string 23
Figure 9: English output from Java MessageFormat example 24
Figure 10: German fixed string from Java MessageFormat example 24
Figure 11: German output from Java MessageFormat example 24
Figure 12: Strings for the .NET version of the Java example 24
Figure 13: Output from Mars.exe 26
2–iv
Part II Contents
Listings
Listing 1: Required IFormattable interface method 2
Listing 2: OverloadedToString.cs 3
Listing 3: SimpleClass.cs 4
Listing 4: Running SimpleClass.exe 4
Listing 5: Object.ToString override in SimpleClass 4
Listing 6: General format example, no precision 5
Listing 7: General format example with precision 5 5
Listing 8: Making money with PI 5
Listing 9: Exploring single letter formatters with DoublesAndInts.cs 5-6
Listing 10: Single-letter formats for DateTime as created by DateTimeFormats.exe 6-7
Listing 11: Constructors and properties for the ComplexNumber structure 9-10
Listing 12: Basic operations in the ComplexNumber structure 12
Listing 13: Creating and trying to display a complex number 11
Listing 14: Overriding default behavior in the ComplexNumber class 12
Listing 15: ToString method using format string 13
Listing 16: Format handler for math format 13
Listing 17: Getting a list separator 13
Listing 18: Partial listing of TestComplexNumber.cs 14-15
Listing 19: Concatenation example (English) 16
Listing 20: Concatenation example (Japanese) 16
Listing 21: Composite formatting (English) 16
Listing 22: Composite formatting (Japanese) 16
Listing 23: NameTag.cs 17
Listing 24: StringFormatDemo.cs 19-21
Listing 25: Making possessive forms (a crude example) 22-23
Listing 26: Driving code for testing PossessiveCase.cs 23
Listing 27: Main part of Mars.cs 24-25
Tables
Table 1: Composite formatting methods in .NET 1
Table 2: Typical overridden ToString methods of DateTime, Double, Decimal,
Int32 and other similar objects 2
Table 3: Acceptable formatters for Double (double) and Int32 (int) 6
Table 4: Single-letter formatters for DateTime 7
Table 5: Methods inherited from the Object base class 10
Table 6: Formats to be implemented for the ComplexNumber class 12
Table 7: Useful .NET numeric formatting strings for doubles 12
Table 8: Numbers in Czech 18
Table 9: Overloaded String.Format methods 19
Table 10: Test strings in PossessiveCase project 22
2–v
Globalization Handbook for the Microsoft .NET Platform
Figures
Figure 1: Output from SimpleExample.exe 2
Figure 2: Output from TimeSpanConstructors.exe 2-3
Figure 3: Fields and properties of TimeSpan 4
Figure 4: Output from MethodExamples.exe 6
Figure 5: Julian Day calculator in two cultures: English (United States) and French (France) 7
Figure 6: Interpreting a two-digit year (Region and Language Settings Dialog) 9
Figure 7: Output from NISTTime project. 9
Listings
Listing 1: SimpleExample.cs 1
Listing 2: Julian Day calculator main code 7-8
Listing 3: Retrieving NIST string using TCP/IP 10
Listing 4: NISTTime.cs main listing 10-11
Tables
Table 1: TimeSpan constructors 2
Table 2: Public fields in TimeSpan 3
Table 3: Public properties of TimeSpan 4
Table 4: Public methods of TimeSpan 5
Table 5: Overloaded operators of TimeSpan 5
Table 6: Parse format for TimeSpan 5
Table 7: ToString output for TimeSpan 5
Table 8: NIST Daytime Format 8
2–vi
Part II Contents
Figures
Figure 1: Output from DateTimeConstructors.exe 2-3
Figure 2: DateTimePicker properties 3
Figure 3: Output from DateTimeProps.exe for Thailand 5-6
Figure 4: Windows DateTime properties demo program 6
Figure 5: WinDateTimeProps.exe with startup cultures in Arabic 7
Figure 6: Standard and custom DateTime formats demo program 11
Figure 7: Experimenting with custom formats 11
Figure 8: DateTime Structure addition and subtraction operations 11
Figure 9: Comparison and equality DateTime operations 12
Figure 10: Demonstrating DateTime conversions 13
Figure 11: Format enumeration in DateTime 15
Figure 12: DateTime default formats 16
Figure 13: Setting the User locale to Arabic (Saudi Arabia) 16
Figure 14: Forcing the use of Indic digits in a locale 16
Figure 15: Demo program reacting to previously set locale 16
Figure 16: DateTime.Parse demo program 18
Listings
Listing 1: Excerpt from WinDateTimeProps.cs 6-7
Listing 2: Annotated fragment from WinDateTimeProps.cs 7
Tables
Table 1: Public constructors for DateTime 2
Table 2: Static fields of DateTime 4
Table 3: Properties of DateTime 4
2–vii
Globalization Handbook for the Microsoft .NET Platform
Figures
Figure 1: Output from SimpleCalendarSelection 2
Figure 2: Output from DTFIConstructor.exe 5
Figure 3: Output from FirstWeekOfYear (FIRSTtFOURDAYWEEK) 8
Figure 4: Testing GetEra with two Gregorian calendars 18
Figure 5: GetEra example, German (Germany) 18
Figure 6: The output for Sweden using the full date formatter 'F' 19
Listings
Listing 1: Static thread of SimpleCalendarSelection.cs 2
Listing 2: ConsoleDateTimeCalendars.cs 4
Listing 3: Static thread from DTFIConstructor.cs 4-5
Listing 4: Main static thread of FirstWeekOfYear.cs 6
Listing 5: Static thread of DTFIProps.cs 11-12
2–viii
Part II Contents
Tables
Table 1: Improving the console display output 3
Table 2: DateTimeFormatInfo properties 7
Table 3: CalendarWeekRule enumeration members 7
Table 4: DateTimeFormatInfo properties for French (France) 9-10
Table 5: One third of the output from DTFIProps.exe for Taiwan 10-11
Table 6: DateTimeFormatInfo public methods 12
Table 7: Output from DaysOfWeek.exe 13
Table 8: Weekday names for the various Japanese calendars 1
Table 9: Two examples of month lists 15
Table 10: Two Hebrew Calendars 16
Table 11: Era names for some calendars 17
Table 12: Testing GetEra with Japanese 18
Figures
Figure 1: DateTimeFormatInfo properties for Slovakia, part 1 1
Figure 2: Properties of DateTimeFormatInfo for Slovakia, part 2 2
Figure 3: Weekday and month names for Czech (left) and Slovak (right) 3
Figure 4: First try at a ‘friendly’ list of Calendar objects 5
Figure 5: Extract of Calendar List control resx file. 6
Figure 6: Friendly names for Calendars 7
Figure 7: DateTimeFormatInfo properties, Czech pseudo-translation, Italian culture 9
Figure 8: DateTimeFormatInfo methods demo 9
2–ix
Globalization Handbook for the Microsoft .NET Platform
Listings
Listing 1: Main source code for WinDateTimeFormatInfoProps.cs 3
Listing 2: Filling in the properties and the values for WinDateTimeFormatInfoProps.cs 4
Listing 3: Supporting class for the Calendar List user control 6
Listing 4: Loading a list of Calendar objects with friendly names 6-7
Listing 5: Changing UI culture in Main 8
Listing 6: Setting up for bidirectional languages 10-11
Listing 7: Fixing a bug in the Japanese Calendar 11
Listing 8: DateTimeFormatFlags definition obtained from WinCV.exe 12
Listing 9: Selected properties of CECultureList.cs 13
Listing 10: Windows CE Calendar List code 13-14
Listing 11: Managing menus with an enumeration 16
Tables
Table 1: Gregorian calendar types in .NET 5
2–x