Anda di halaman 1dari 12

GLOBALIZATION

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

General Goals: This book is intended to be dynamic and extensible.


From time to time the material found here will be augmented, updated
and corrected. New material will be added, and incorrect or obsolete
material removed.
Source Code: Code downloads are provided for all programming examples and
demonstration programs. As of this writing, all code has been built to operate under
Microsoft .NET Version 1.1.

Trademark Acknowledgements: The publisher has endeavored to provide trademark


information about all the companies and products mentioned here.

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.

Technical Reviewers: John O’Conner (O’Conner Associates, www.javai18n.com)


Tex Texin (XenCraft, www.xencraft.com)
David Crossman (BrassRing, www.brassring.com)
Achim Ruopp (Microsoft Corp., www.microsoft.com)
Aldo Donetti (Microsoft Corp., www.microsoft.com)

Managing Editor: Laurel Wagers


Proofreader: James Healey
Production: Sandy Compton

Published by MultiLingual Press, 2005.

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

RegionInfo Class Demonstration Programs:


Console, Forms, Web and Compact Framework Examples RID1-30
Overview 1
RegionInfo Console Demonstration 1
Code Segments 2
Localization Structure 5
Windows Form RegionInfo Demonstration Program 6
Custom RegionInfo Dropdown List 7
Filling the ListViews With Properties and Methods 9
Adding a Localization Structure 13
Creating the Method and Property Descriptions Resource Files 15
Compact Framework RegionInfo Demo 17
Creating a List of Regions in Windows CE 18
Excerpts from Windows CE Form Code 21
Localization 22
Web RegionInfo Demo Program 23
RegionInfo User Control 24
The Main Page 25
InnerHtml and InnerText 25
Setting Culture and UI Culture 26
Style Sheet 27
Localization 28
Notes 29

Figures
Figure 1: RegionInfo console demo output for Norway 1-2
Figure 2: Resource strings in RegionInfoReflection.cs 5
2–ii
Part II Contents

Figure 3: ‘Translated’ resources for RegionInfoReflection.cs 5


Figure 4: Output of RegionInfoReflection.exe with NO no on the command line 6
Figure 5: Windows RegionInfo demo program 6
Figure 6: RegionInfo dropdown list design properties 7
Figure 7: Various views of the RegionInfoList test program 10
Figure 8: ResEditor.Exe showing PropertyDescriptions.resx 10
Figure 9: ResEditor.Exe showing MethodDescriptions.resx 11
Figure 10: Appearance of the Windows RegionInfo demo just after startup 12
Figure 11: Multiple resource files in an assembly 13
Figure 12: Multiple resource files in a satellite assembly 13
Figure 13: Preparing the RegionInfo demo for localization into Greek 13
Figure 14: Pseudo-Greek .resx file 14
Figure 15: Windows RegionInfo demo ‘translated’ into Greek 15
Figure 16: Strings describing RegionInfo properties 15
Figure 17: PropertyDescriptions.resx 15-16
Figure 18: CE RegionInfo demo 18
Figure 19: Partial output from AllRegionList.exe 18-19
Figure 20: CE Device with Ukrainian pseudo-translation 22
Figure 21: CE device resource fallback 22
Figure 22: RegionInfo web demo 23
Figure 23: Using a ‘Command Line Parameter’ on an ASPX web page 23
Figure 24: InnerHTML and control identifiers 25
Figure 25: Resource file for web RegionInfo demo program 28
Figure 26: Setting language preferences in the browser 28
Figure 27: RegionInfo web demo, French (France) 28
Figure 28: RegionInfo web demo, French 28
Figure 29: RegionInfo web demo, Sweden. Note sort order of regions 29

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

Listing 17: User control property 26


Listing 18: Table cells with InnerHtml that can be modified at startup 25
Listing 19: Loading InnerHtml text from a resource file 26
Listing 20: Reading and using HTTP_ACCEPT_LANGUAGE 26-27
Listing 21: Populating RegionInfo properties and methods 27

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

The TimeSpan Structure:


Date and Time Formatting, Part 1 TS-1-12
Overview 1
Public Constructors 2
Constructor Examples 2
Public Fields 3
Public Properties 4
Fields and Properties Demo 4
Public Methods 4
Notes on the Methods 4
Simple Examples With Methods 6
Advanced Examples 6
Julian Day Calculator 6
Current Time from a NIST Daytime Server 8
Chapter Summary 12
Notes 12

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

The DateTime Structure:


Date and Time Formatting, Part 2 DT1-20
Overview 1
DateTime Public Constructors 1
Fields 4
DateTime Properties 4
Windows-based Properties and Fields Demo 6
DateTime Public Methods 8
ToString 8
Addition and Subtraction Operations 11
Comparison and Equality 12
Conversions 13
Information 13
Format Enumeration 14
Default Formats 15
Parsing 17
Chapter Summary 19
Notes 20

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

Table 4: DayOfWeek enumeration 5


Table 5: DateTime ToString methods 8
Table 6: Standard DateTime format strings 8-9
Table 7: Custom formatters for DateTime 10
Table 8: DateTime addition and subtraction methods 12
Table 9: Comparison Methods of DateTime 12
Table 10: DateTime conversion methods 13
Table 11: Information methods of DateTime 14
Table 12: Examining leap years and days in a month 14
Table 13: Format enumeration methods of DateTime 14
Table 14: DateTime default formats 15
Table 15: Parse and ParseExact methods of DateTime 17
Table 16: DateTimeStyles enumeration 17

The DateTimeFormatInfo Class:


Date and Time Formatting, Part 3 DTFI1-22
Overview 1
Improving the Output 3
Public Constructors 4
Public Properties 5
CalendarWeekRule 5
Properties Demo 8
Public Methods 12
Days of the Week 13
Months of the Year 15
Era Names 16
Date/Time Formatting Patterns 19
Chapter Summary 21
Notes 21

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

Listing 6: Static thread for DaysOfWeek.cs 13-14


Listing 7: Partial listing of MonthsOfYear.cs 15
Listing 8: Partial listing from CalendarEras.cs 16-17
Listing 9: GetAllDateTimeFormats.cs 19-21

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

DateTimeFormatInfo Demonstration Programs:


Date and Time Formatting, Part 4 DTFD1-18
Overview 1
DateTimeFormatInfo Properties Demo 1
Program Details 3
Creating a List of Calendars 5
Localization Considerations 8
DateTimeFormatInfo Methods Demo 9
Localization Considerations 11
Windows CE Properties Demonstration 12
Calendar and CultureInfo Lists 13
Localization Considerations 15
Windows CE Methods Demonstration 16
Localization Considerations 17
Chapter Summary 17
Notes 17

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

Figure 9: Calendar data for the Japanese Taisho era 10


Figure 10: Alternate long date/short time patterns for Slovenia 10
Figure 11: Customizing a date/time format 10
Figure 12: Partial view of methods demo with pseudo-Russian localization 11
Figure 13: Windows CE emulator version of DateTimeFormatInfo properties 13
Figure 14: Using Regional Settings to change the UI language on a Windows CE device 15
Figure 15: Windows CE property demo with pseudo-French (France) pseudo-translation 15
Figure 16: Windows CE methods demo 16
Figure 17: DateTimeFormatInfo methods on an iPAC 3650 17

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

Anda mungkin juga menyukai