gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC] Locale handling fix


From: David Ayers
Subject: [RFC] Locale handling fix
Date: Tue, 10 Jun 2008 23:05:06 +0200
User-agent: Mozilla-Thunderbird 2.0.0.14 (X11/20080509)

Hello Everyone,

I here is a patch I have hat locally but never had the chance to verify.
 I'm personally uneasy about committing merely because of the timing.
But I thought I'd bring it up for review just in case folk believe it is
obviously correct.

It matches the decoding key for thousands separator with the encoding
key.  (Maybe the correct fix is the other way around wrt compatibility).

It also fixes stringForObjectValue: to respect the current locale wrt
thousands and decimal separators.

There may be optimization possibilities and it may be incomplete... I
just happened to stumble over it.  But I've been using it for quite some
time.

Cheers,
David
Index: Source/NSNumberFormatter.m
===================================================================
--- Source/NSNumberFormatter.m  (Revision 26624)
+++ Source/NSNumberFormatter.m  (Arbeitskopie)
@@ -36,6 +36,8 @@
 #include "Foundation/NSUserDefaults.h"
 #include "Foundation/NSCharacterSet.h"
 
+#include "GNUstepBase/GSLocale.h"
+
 @implementation NSNumberFormatter
 
 - (BOOL) allowsFloats
@@ -256,10 +258,10 @@
          [self setDecimalSeparator:
            [decoder decodeObjectForKey: @"NS.decimal"]];
        }
-      if ([decoder containsValueForKey: @"NS.hasthousands"])
+      if ([decoder containsValueForKey: @"NS.hasthousand"])
         {
          [self setHasThousandSeparators:
-           [decoder decodeBoolForKey: @"NS.hasthousands"]];
+           [decoder decodeBoolForKey: @"NS.hasthousand"]];
        }
       if ([decoder containsValueForKey: @"NS.localized"])
         {
@@ -534,7 +536,26 @@
   BOOL                 displayFractionalPart = NO;
   BOOL                 negativeNumber = NO;
   NSString             *useFormat;
+  NSString             *defaultDecimalSeparator = nil;
+  NSString             *defaultThousandsSeparator = nil;
 
+  if (_localizesFormat)
+    {
+      NSDictionary *defaultLocale = GSDomainFromDefaultLocale();
+      defaultDecimalSeparator 
+       = [defaultLocale objectForKey: NSDecimalSeparator];
+      defaultThousandsSeparator 
+       = [defaultLocale objectForKey: NSThousandsSeparator];
+    }
+
+  if (defaultDecimalSeparator == nil)
+    {
+      defaultDecimalSeparator = @".";
+    }
+  if (defaultThousandsSeparator == nil)
+    {
+      defaultThousandsSeparator = @",";
+    }
   formattingCharacters = [NSCharacterSet
     characterSetWithCharactersInString: @"0123456789#.,_"];
   placeHolders = [NSCharacterSet 
@@ -546,7 +567,8 @@
     return [[self attributedStringForNotANumber] string];
   if ([anObject isEqual: [NSDecimalNumber notANumber]])
     return [[self attributedStringForNotANumber] string];
-  if ([anObject isEqual: [NSDecimalNumber zero]])
+  if (_attributedStringForZero
+      && [anObject isEqual: [NSDecimalNumber zero]])
     return [[self attributedStringForZero] string];
   
   useFormat = _positiveFormat;
@@ -560,7 +582,10 @@
   // if no format specified, use the same default that Cocoa does
   if (nil == useFormat)
     {
-      useFormat = negativeNumber ? @"-#,###.##" : @"#,###.##";
+      useFormat = [NSString stringWithFormat: @"address@hidden@address@hidden",
+                           negativeNumber ? @"-" : @"",
+                           defaultThousandsSeparator,
+                           defaultDecimalSeparator];
     }
 
   prefixRange = [useFormat rangeOfCharacterFromSet: formattingCharacters];
@@ -580,15 +605,16 @@
   //should also set NSDecimalDigits?
   
   if ([self hasThousandSeparators]
-    && (0 != [useFormat rangeOfString:@","].length))
+    && (0 != [useFormat rangeOfString: defaultThousandsSeparator].length))
     {
       displayThousandsSeparators = YES;
     }
 
   if ([self allowsFloats]
-    && (NSNotFound != [useFormat rangeOfString:@"." ].location))
+    && (NSNotFound 
+       != [useFormat rangeOfString: defaultDecimalSeparator].location))
     {
-      decimalPlaceRange = [useFormat rangeOfString: @"."
+      decimalPlaceRange = [useFormat rangeOfString: defaultDecimalSeparator
                                           options: NSBackwardsSearch];
       if (NSMaxRange(decimalPlaceRange) == [useFormat length])
         {
@@ -636,14 +662,15 @@
       while (([placeHolders characterIsMember:
         [useFormat characterAtIndex: NSMaxRange(intPartRange)]]
         || [[useFormat substringFromRange:
-          NSMakeRange(NSMaxRange(intPartRange), 1)] isEqual: @","])
+          NSMakeRange(NSMaxRange(intPartRange), 1)] isEqual:
+           defaultThousandsSeparator])
         && NSMaxRange(intPartRange) < [useFormat length] - 1)
         {
           intPartRange.length++;
         }
     }
   intPad = [[useFormat substringWithRange: intPartRange] mutableCopy];
-  [intPad replaceOccurrencesOfString: @","
+  [intPad replaceOccurrencesOfString: defaultThousandsSeparator
     withString: @""
     options: 0
     range: NSMakeRange(0, [intPad length])];

reply via email to

[Prev in Thread] Current Thread [Next in Thread]