@@ -34,11 +34,12 @@ namespace UnitsNet
34
34
public partial class UnitSystem
35
35
{
36
36
private static readonly Dictionary < CultureInfo , UnitSystem > CultureToInstance ;
37
+ private static readonly CultureInfo DefaultCulture = CultureInfo . GetCultureInfo ( "en-US" ) ;
37
38
38
39
/// <summary>
39
40
/// The culture of which this unit system is based on. Either passed in to constructor or the default culture.
40
41
/// </summary>
41
- [ PublicAPI ] public readonly CultureInfo Culture ;
42
+ [ NotNull ] [ PublicAPI ] public readonly CultureInfo Culture ;
42
43
43
44
/// <summary>
44
45
/// Per-unit-type dictionary of enum values by abbreviation. This is the inverse of
@@ -66,7 +67,7 @@ static UnitSystem()
66
67
public UnitSystem ( [ CanBeNull ] CultureInfo cultureInfo = null )
67
68
{
68
69
if ( cultureInfo == null )
69
- cultureInfo = new CultureInfo ( "en-US" ) ;
70
+ cultureInfo = new CultureInfo ( DefaultCulture . Name ) ;
70
71
71
72
Culture = cultureInfo ;
72
73
_unitTypeToUnitValueToAbbrevs = new Dictionary < Type , Dictionary < int , List < string > > > ( ) ;
@@ -75,6 +76,11 @@ public UnitSystem([CanBeNull] CultureInfo cultureInfo = null)
75
76
LoadDefaultAbbreviatons ( cultureInfo ) ;
76
77
}
77
78
79
+ public bool IsDefaultCulture
80
+ {
81
+ get { return Culture . Equals ( DefaultCulture ) ; }
82
+ }
83
+
78
84
[ PublicAPI ]
79
85
public static void ClearCache ( )
80
86
{
@@ -145,7 +151,7 @@ public string GetDefaultAbbreviation<TUnit>(TUnit unit)
145
151
public void MapUnitToAbbreviation < TUnit > ( TUnit unit , params string [ ] abbreviations )
146
152
where TUnit : /*Enum constraint hack*/ struct , IComparable , IFormattable
147
153
{
148
- // Assuming TUnit is an enum, this conversion is safe. Not possible to cleanly enforce this today.
154
+ // Assuming TUnit is an enum, this conversion is safe. Seems not possible to enforce this today.
149
155
// Src: http://stackoverflow.com/questions/908543/how-to-convert-from-system-enum-to-base-integer
150
156
// http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum
151
157
int unitValue = Convert . ToInt32 ( unit ) ;
@@ -197,22 +203,31 @@ public bool TryParse<TUnit>(string unitAbbreviation, out TUnit unit)
197
203
Type unitType = typeof ( TUnit ) ;
198
204
199
205
Dictionary < string , int > abbrevToUnitValue ;
200
- if ( ! _unitTypeToAbbrevToUnitValue . TryGetValue ( unitType , out abbrevToUnitValue ) )
201
- throw new NotImplementedException (
202
- string . Format ( "No abbreviations defined for unit type [{0}] for culture [{1}]." , unitType ,
203
- Culture . EnglishName ) ) ;
204
-
205
206
int unitValue ;
206
- if ( ! abbrevToUnitValue . TryGetValue ( unitAbbreviation , out unitValue ) )
207
+
208
+ if ( ! _unitTypeToAbbrevToUnitValue . TryGetValue ( unitType , out abbrevToUnitValue ) ||
209
+ ! abbrevToUnitValue . TryGetValue ( unitAbbreviation , out unitValue ) )
207
210
{
208
- unit = default ( TUnit ) ;
209
- return false ;
211
+ if ( IsDefaultCulture )
212
+ {
213
+ unit = default ( TUnit ) ;
214
+ return false ;
215
+ }
216
+
217
+ // Fall back to default culture
218
+ return GetCached ( DefaultCulture ) . TryParse ( unitAbbreviation , out unit ) ;
210
219
}
211
220
212
221
unit = ( TUnit ) ( object ) unitValue ;
213
222
return true ;
214
223
}
215
224
225
+ /// <summary>
226
+ /// Get all abbreviations for unit.
227
+ /// </summary>
228
+ /// <typeparam name="TUnit">Enum type for units.</typeparam>
229
+ /// <param name="unit">Enum value for unit.</param>
230
+ /// <returns>Unit abbreviations associated with unit.</returns>
216
231
[ PublicAPI ]
217
232
public string [ ] GetAllAbbreviations < TUnit > ( TUnit unit )
218
233
where TUnit : /*Enum constraint hack*/ struct , IComparable , IFormattable
@@ -221,18 +236,20 @@ public string[] GetAllAbbreviations<TUnit>(TUnit unit)
221
236
int unitValue = Convert . ToInt32 ( unit ) ;
222
237
223
238
Dictionary < int , List < string > > unitValueToAbbrevs ;
224
- if ( ! _unitTypeToUnitValueToAbbrevs . TryGetValue ( unitType , out unitValueToAbbrevs ) )
225
- throw new NotImplementedException (
226
- string . Format ( "No abbreviations defined for unit type [{0}] for culture [{1}]." , unitType ,
227
- Culture . EnglishName ) ) ;
228
-
229
239
List < string > abbrevs ;
230
- if ( ! unitValueToAbbrevs . TryGetValue ( unitValue , out abbrevs ) )
231
- throw new NotImplementedException (
232
- string . Format ( "No abbreviations defined for unit type [{0}.{1}] for culture [{2}]." , unitType ,
233
- unitValue , Culture . EnglishName ) ) ;
234
240
235
- // Return the first (most commonly used) abbreviation for this unit)
241
+ if ( ! _unitTypeToUnitValueToAbbrevs . TryGetValue ( unitType , out unitValueToAbbrevs ) ||
242
+ ! unitValueToAbbrevs . TryGetValue ( unitValue , out abbrevs ) )
243
+ {
244
+ if ( IsDefaultCulture )
245
+ {
246
+ return new [ ] { string . Format ( "(no abbreviation for {0})" , unit . ToString ( ) ) } ;
247
+ }
248
+
249
+ // Fall back to default culture
250
+ return GetCached ( DefaultCulture ) . GetAllAbbreviations ( unit ) ;
251
+ }
252
+
236
253
return abbrevs . ToArray ( ) ;
237
254
}
238
255
0 commit comments