Skip to content

Commit

Permalink
fix for #368
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Feb 6, 2024
1 parent d6bb6de commit 79a1a6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2024-02-06 Richard Frith-Macdonald <[email protected]>

* Source/NSLocale.m: (+canonicalLocaleIdentifierFromString:)
Fix for #368 ... standardise hyphen to underscore then check for
presence of two underscores and delete the script identifier between
them, as suggested by the existing comments. Avoids the exception.

2024-02-02 Richard Frith-Macdonald <[email protected]>

Source/NSString.m: Return empty string of correct class when loading
Expand Down
33 changes: 26 additions & 7 deletions Source/NSLocale.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "Foundation/NSNumberFormatter.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSString.h"
#import "GNUstepBase/NSMutableString+GNUstepBase.h"
#import "GNUstepBase/GSLock.h"

#if defined(HAVE_UNICODE_ULOC_H)
Expand Down Expand Up @@ -272,18 +273,36 @@ + (NSString *) canonicalLocaleIdentifierFromString: (NSString *) string
if (result == nil)
result = string;

// Strip script info from locale
/* Strip script info (if present) from locale.
* We try to cope with zh-Hant_TW or zh_Hant-TW
*/
mStr = nil;
range = [result rangeOfString: @"-"];
if (range.length > 0)
{
mStr = [NSMutableString stringWithString: result];
[mStr replaceString: @"-" withString: @"_"];
result = mStr;
}
range = [result rangeOfString: @"_"];
if (range.location != NSNotFound)
{
NSUInteger start = range.location;
NSUInteger length;
range = [result rangeOfString: @"_"];
length = range.location - start;

mStr = [NSMutableString stringWithString: result];
[mStr deleteCharactersInRange: NSMakeRange (start, length)];

range = [result rangeOfString: @"_" options: NSBackwardsSearch];
if (range.location != start)
{
NSUInteger length = range.location - start;

if (nil == mStr)
{
mStr = [NSMutableString stringWithString: result];
}
[mStr deleteCharactersInRange: NSMakeRange(start, length)];
}
}
if (mStr)
{
result = [NSString stringWithString: mStr];
}

Expand Down

0 comments on commit 79a1a6b

Please sign in to comment.