Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Core / CSharp / MS / Internal / TextFormatting / CultureMapper.cs / 1 / CultureMapper.cs
//------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description: The CultureMapper class implements static methods for mapping
// CultureInfo objects from WPF clients to CultureInfo objects
// that can be used internally by text formatting code.
//
// History:
// 1-30-2006 : niklasb - Created
//
//-----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Diagnostics;
using MS.Internal.PresentationCore;
namespace MS.Internal.TextFormatting
{
///
/// Implements static methods for mapping CultureInfo objects.
///
internal static class CultureMapper
{
///
/// Returns a specific culture given an arbitrary CultureInfo, which may be null, the invariant
/// culture, or a neutral culture.
///
public static CultureInfo GetSpecificCulture(CultureInfo runCulture)
{
// Assume default culture unless we can do better.
CultureInfo specificCulture = DefaultTextCulture;
if (runCulture != null)
{
// Assign _cachedCultureMap to a local variable for thread safety. The reference assignment
// is atomic and the CachedCultureMap class is immutable.
CachedCultureMap cachedCultureMap = _cachedCultureMap;
if (cachedCultureMap != null && object.ReferenceEquals(cachedCultureMap.OriginalCulture, runCulture))
return cachedCultureMap.SpecificCulture;
// Unfortunately we cannot use reference comparison here because, for example, new CultureInfo("")
// creates an invariant culture which (being a new object) is obviously not the same instance as
// CultureInfo.InvariantCulture.
if (runCulture != CultureInfo.InvariantCulture)
{
if (!runCulture.IsNeutralCulture)
{
// It's already a specific culture (neither neutral nor InvariantCulture)
specificCulture = runCulture;
}
else
{
// Get the culture name. Note that the string expected by CreateSpecificCulture corresponds
// to the Name property, not IetfLanguageTag, so that's what we use.
string cultureName = runCulture.Name;
if (!string.IsNullOrEmpty(cultureName))
{
try
{
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
specificCulture = SafeSecurityHelper.GetCultureInfoByIetfLanguageTag(culture.IetfLanguageTag);
}
catch (ArgumentException)
{
// This exception occurs if the culture name is invalid or has no corresponding specific
// culture. we can safely ignore the exception and fall back to DefaultTextCulture.
specificCulture = DefaultTextCulture;
}
}
}
}
// Save the mapping so the next call will be fast if we're given the same runCulture.
// Again, the reference assignment is atomic so this is thread safe.
_cachedCultureMap = new CachedCultureMap(runCulture, specificCulture);
}
return specificCulture;
}
private class CachedCultureMap
{
public CachedCultureMap(CultureInfo originalCulture, CultureInfo specificCulture)
{
_originalCulture = originalCulture;
_specificCulture = specificCulture;
}
///
/// Original CultureInfo object from text formatting client; could be the invariant culture,
/// a neutral culture, or a specific culture.
///
public CultureInfo OriginalCulture
{
get { return _originalCulture; }
}
///
/// CultureInfo object to use for text formatting. This is guaranteed to be a specific (i.e.,
/// neither neutral nor invariant) culture. It may be the same object as OriginalCulture if
/// the latter is already a specific culture.
///
public CultureInfo SpecificCulture
{
get { return _specificCulture; }
}
private CultureInfo _originalCulture;
private CultureInfo _specificCulture;
}
private static readonly CultureInfo DefaultTextCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("en-US");
private static CachedCultureMap _cachedCultureMap = null;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description: The CultureMapper class implements static methods for mapping
// CultureInfo objects from WPF clients to CultureInfo objects
// that can be used internally by text formatting code.
//
// History:
// 1-30-2006 : niklasb - Created
//
//-----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Diagnostics;
using MS.Internal.PresentationCore;
namespace MS.Internal.TextFormatting
{
///
/// Implements static methods for mapping CultureInfo objects.
///
internal static class CultureMapper
{
///
/// Returns a specific culture given an arbitrary CultureInfo, which may be null, the invariant
/// culture, or a neutral culture.
///
public static CultureInfo GetSpecificCulture(CultureInfo runCulture)
{
// Assume default culture unless we can do better.
CultureInfo specificCulture = DefaultTextCulture;
if (runCulture != null)
{
// Assign _cachedCultureMap to a local variable for thread safety. The reference assignment
// is atomic and the CachedCultureMap class is immutable.
CachedCultureMap cachedCultureMap = _cachedCultureMap;
if (cachedCultureMap != null && object.ReferenceEquals(cachedCultureMap.OriginalCulture, runCulture))
return cachedCultureMap.SpecificCulture;
// Unfortunately we cannot use reference comparison here because, for example, new CultureInfo("")
// creates an invariant culture which (being a new object) is obviously not the same instance as
// CultureInfo.InvariantCulture.
if (runCulture != CultureInfo.InvariantCulture)
{
if (!runCulture.IsNeutralCulture)
{
// It's already a specific culture (neither neutral nor InvariantCulture)
specificCulture = runCulture;
}
else
{
// Get the culture name. Note that the string expected by CreateSpecificCulture corresponds
// to the Name property, not IetfLanguageTag, so that's what we use.
string cultureName = runCulture.Name;
if (!string.IsNullOrEmpty(cultureName))
{
try
{
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
specificCulture = SafeSecurityHelper.GetCultureInfoByIetfLanguageTag(culture.IetfLanguageTag);
}
catch (ArgumentException)
{
// This exception occurs if the culture name is invalid or has no corresponding specific
// culture. we can safely ignore the exception and fall back to DefaultTextCulture.
specificCulture = DefaultTextCulture;
}
}
}
}
// Save the mapping so the next call will be fast if we're given the same runCulture.
// Again, the reference assignment is atomic so this is thread safe.
_cachedCultureMap = new CachedCultureMap(runCulture, specificCulture);
}
return specificCulture;
}
private class CachedCultureMap
{
public CachedCultureMap(CultureInfo originalCulture, CultureInfo specificCulture)
{
_originalCulture = originalCulture;
_specificCulture = specificCulture;
}
///
/// Original CultureInfo object from text formatting client; could be the invariant culture,
/// a neutral culture, or a specific culture.
///
public CultureInfo OriginalCulture
{
get { return _originalCulture; }
}
///
/// CultureInfo object to use for text formatting. This is guaranteed to be a specific (i.e.,
/// neither neutral nor invariant) culture. It may be the same object as OriginalCulture if
/// the latter is already a specific culture.
///
public CultureInfo SpecificCulture
{
get { return _specificCulture; }
}
private CultureInfo _originalCulture;
private CultureInfo _specificCulture;
}
private static readonly CultureInfo DefaultTextCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("en-US");
private static CachedCultureMap _cachedCultureMap = null;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DialogResultConverter.cs
- Processor.cs
- XslVisitor.cs
- GridViewSelectEventArgs.cs
- CodeTypeReferenceExpression.cs
- ValidatingReaderNodeData.cs
- ProcessRequestAsyncResult.cs
- AQNBuilder.cs
- OpenTypeCommon.cs
- DataGridViewMethods.cs
- GroupDescription.cs
- GraphicsContainer.cs
- PolicyReader.cs
- CommaDelimitedStringAttributeCollectionConverter.cs
- CodeBlockBuilder.cs
- TriState.cs
- Function.cs
- LabelAutomationPeer.cs
- BoundsDrawingContextWalker.cs
- xmlformatgeneratorstatics.cs
- Vector3DKeyFrameCollection.cs
- VisualTreeUtils.cs
- DBSqlParserTable.cs
- WebServiceReceive.cs
- SmtpNetworkElement.cs
- DataPointer.cs
- RawStylusActions.cs
- ConfigUtil.cs
- ComboBoxItem.cs
- BaseDataListComponentEditor.cs
- TransformCollection.cs
- ListBase.cs
- LoginNameDesigner.cs
- SectionUpdates.cs
- HttpCacheParams.cs
- TransformDescriptor.cs
- MarshalByRefObject.cs
- Constant.cs
- UnicodeEncoding.cs
- Variable.cs
- PtsHost.cs
- SectionVisual.cs
- SingleResultAttribute.cs
- RoleGroupCollection.cs
- ContractMapping.cs
- DataGridViewColumnTypeEditor.cs
- CodeDirectionExpression.cs
- RegexParser.cs
- ToggleButton.cs
- SystemColorTracker.cs
- MapPathBasedVirtualPathProvider.cs
- CacheAxisQuery.cs
- NativeMethods.cs
- DataColumnCollection.cs
- KeyEvent.cs
- UnionCodeGroup.cs
- CachedCompositeFamily.cs
- ChineseLunisolarCalendar.cs
- PropertyValueUIItem.cs
- ModulesEntry.cs
- CodeTypeDeclarationCollection.cs
- IndependentlyAnimatedPropertyMetadata.cs
- PointCollectionValueSerializer.cs
- WebPartZoneCollection.cs
- BitmapData.cs
- SmtpClient.cs
- ResourceProperty.cs
- ExtensionQuery.cs
- MobileResource.cs
- XmlSchemaInferenceException.cs
- ScalarConstant.cs
- ChangeInterceptorAttribute.cs
- SerializeAbsoluteContext.cs
- AuthenticationModulesSection.cs
- BitmapScalingModeValidation.cs
- DWriteFactory.cs
- FtpWebResponse.cs
- WebHeaderCollection.cs
- TextBox.cs
- RsaSecurityKey.cs
- ExclusiveTcpTransportManager.cs
- ScaleTransform.cs
- XmlProcessingInstruction.cs
- GeometryValueSerializer.cs
- KeyEvent.cs
- ConnectorDragDropGlyph.cs
- FreezableOperations.cs
- CodeConstructor.cs
- UrlMappingsSection.cs
- ReadOnlyDictionary.cs
- VisemeEventArgs.cs
- TypeResolver.cs
- XmlSignatureManifest.cs
- ADMembershipUser.cs
- WindowsAuthenticationModule.cs
- DragCompletedEventArgs.cs
- ImmComposition.cs
- StylusTouchDevice.cs
- MouseButtonEventArgs.cs
- StylusPointPropertyUnit.cs