* Source/NSBundle.m (-localizedStringForKey:value:table): Correct

output when NSShowNonLocalizedStrings is YES.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28013 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
fredkiefer 2009-03-01 17:39:09 +00:00
parent 6d71b03cb4
commit 9027f0777b
2 changed files with 92 additions and 87 deletions

View file

@ -1,3 +1,8 @@
2009-03-01 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSBundle.m (-localizedStringForKey:value:table): Correct
output when NSShowNonLocalizedStrings is YES.
2009-02-28 Richard Frith-Macdonald <rfm@gnu.org> 2009-02-28 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSURLProtocol.m: * Source/NSURLProtocol.m:

View file

@ -1935,8 +1935,8 @@ IF_NO_GC(
} }
- (NSString *) localizedStringForKey: (NSString *)key - (NSString *) localizedStringForKey: (NSString *)key
value: (NSString *)value value: (NSString *)value
table: (NSString *)tableName table: (NSString *)tableName
{ {
NSDictionary *table; NSDictionary *table;
NSString *newString = nil; NSString *newString = nil;
@ -1969,107 +1969,107 @@ IF_NO_GC(
tablePath = [self pathForResource: tableName ofType: @"strings"]; tablePath = [self pathForResource: tableName ofType: @"strings"];
if (tablePath != nil) if (tablePath != nil)
{ {
NSStringEncoding encoding; NSStringEncoding encoding;
NSString *tableContent; NSString *tableContent;
NSData *tableData; NSData *tableData;
const unsigned char *bytes; const unsigned char *bytes;
unsigned length; unsigned length;
tableData = [[NSData alloc] initWithContentsOfFile: tablePath]; tableData = [[NSData alloc] initWithContentsOfFile: tablePath];
bytes = [tableData bytes]; bytes = [tableData bytes];
length = [tableData length]; length = [tableData length];
/* /*
* A localisation file can be ... * A localisation file can be ...
* UTF16 with a leading BOM, * UTF16 with a leading BOM,
* UTF8 with a leading BOM, * UTF8 with a leading BOM,
* or ASCII (the original standard) with \U escapes. * or ASCII (the original standard) with \U escapes.
*/ */
if (length > 2 if (length > 2
&& ((bytes[0] == 0xFF && bytes[1] == 0xFE) && ((bytes[0] == 0xFF && bytes[1] == 0xFE)
|| (bytes[0] == 0xFE && bytes[1] == 0xFF))) || (bytes[0] == 0xFE && bytes[1] == 0xFF)))
{ {
encoding = NSUnicodeStringEncoding; encoding = NSUnicodeStringEncoding;
} }
else if (length > 2 else if (length > 2
&& bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
{ {
encoding = NSUTF8StringEncoding; encoding = NSUTF8StringEncoding;
} }
else else
{ {
encoding = NSASCIIStringEncoding; encoding = NSASCIIStringEncoding;
} }
tableContent = [[NSString alloc] initWithData: tableData tableContent = [[NSString alloc] initWithData: tableData
encoding: encoding]; encoding: encoding];
if (tableContent == nil && encoding == NSASCIIStringEncoding) if (tableContent == nil && encoding == NSASCIIStringEncoding)
{ {
encoding = [NSString defaultCStringEncoding]; encoding = [NSString defaultCStringEncoding];
tableContent = [[NSString alloc] initWithData: tableData tableContent = [[NSString alloc] initWithData: tableData
encoding: encoding]; encoding: encoding];
if (tableContent != nil) if (tableContent != nil)
{ {
NSWarnMLog (@"Localisation file %@ not in portable encoding" NSWarnMLog (@"Localisation file %@ not in portable encoding"
@" so I'm using the default encoding for the current" @" so I'm using the default encoding for the current"
@" system, which may not display messages correctly.\n" @" system, which may not display messages correctly.\n"
@"The file should be ASCII (using \\U escapes for unicode" @"The file should be ASCII (using \\U escapes for unicode"
@" characters) or Unicode (UTF16 or UTF8) with a leading " @" characters) or Unicode (UTF16 or UTF8) with a leading "
@"byte-order-marker.\n", tablePath); @"byte-order-marker.\n", tablePath);
} }
} }
if (tableContent == nil) if (tableContent == nil)
{ {
NSWarnMLog(@"Failed to load strings file %@ - bad character" NSWarnMLog(@"Failed to load strings file %@ - bad character"
@" encoding", tablePath); @" encoding", tablePath);
} }
else else
{ {
NS_DURING NS_DURING
{ {
table = [tableContent propertyListFromStringsFileFormat]; table = [tableContent propertyListFromStringsFileFormat];
} }
NS_HANDLER NS_HANDLER
{ {
NSWarnMLog(@"Failed to parse strings file %@ - %@", NSWarnMLog(@"Failed to parse strings file %@ - %@",
tablePath, localException); tablePath, localException);
} }
NS_ENDHANDLER NS_ENDHANDLER
} }
RELEASE(tableData); RELEASE(tableData);
RELEASE(tableContent); RELEASE(tableContent);
} }
else else
{ {
NSDebugMLLog(@"NSBundle", @"Failed to locate strings file %@", NSDebugMLLog(@"NSBundle", @"Failed to locate strings file %@",
tableName); tableName);
} }
/* /*
* If we couldn't found and parsed the strings table, we put it in * If we couldn't found and parsed the strings table, we put it in
* the cache of strings tables in this bundle, otherwise we will just * the cache of strings tables in this bundle, otherwise we will just
* be keeping the empty table in the cache so we don't keep retrying. * be keeping the empty table in the cache so we don't keep retrying.
*/ */
if (table != nil) if (table != nil)
[_localizations setObject: table forKey: tableName]; [_localizations setObject: table forKey: tableName];
} }
if (key == nil || (newString = [table objectForKey: key]) == nil) if (key == nil || (newString = [table objectForKey: key]) == nil)
{ {
NSString *show = [[NSUserDefaults standardUserDefaults] NSString *show = [[NSUserDefaults standardUserDefaults]
objectForKey: NSShowNonLocalizedStrings]; objectForKey: NSShowNonLocalizedStrings];
if (show && [show isEqual: @"YES"]) if (show && [show isEqual: @"YES"])
{ {
/* It would be bad to localize this string! */ /* It would be bad to localize this string! */
NSLog(@"Non-localized string: %@\n", newString); NSLog(@"Non-localized string: %@\n", key);
newString = [key uppercaseString]; newString = [key uppercaseString];
} }
else else
{ {
newString = value; newString = value;
if (newString == nil || [newString isEqualToString: @""] == YES) if (newString == nil || [newString isEqualToString: @""] == YES)
newString = key; newString = key;
} }
if (newString == nil) if (newString == nil)
newString = @""; newString = @"";
} }
return newString; return newString;