|
54 | 54 | #import "GNUstepBase/NSTask+GNUstepBase.h" |
55 | 55 |
|
56 | 56 | #import "GSPrivate.h" |
| 57 | +#import "NSPropertyListPrivate.h" |
57 | 58 |
|
58 | 59 | /* Store the working directory at startup */ |
59 | 60 | static NSString *_launchDirectory = nil; |
@@ -2872,21 +2873,58 @@ - (NSString *) localizedStringForKey: (NSString *)key |
2872 | 2873 | tablePath = [self pathForResource: tableName ofType: @"strings"]; |
2873 | 2874 | if (tablePath != nil) |
2874 | 2875 | { |
| 2876 | + NSPropertyListFormat format; |
2875 | 2877 | NSStringEncoding encoding; |
2876 | 2878 | NSString *tableContent; |
2877 | 2879 | NSData *tableData; |
2878 | 2880 | const unsigned char *bytes; |
2879 | 2881 | unsigned length; |
2880 | 2882 |
|
2881 | | - tableData = [[NSData alloc] initWithContentsOfFile: tablePath]; |
2882 | | - bytes = [tableData bytes]; |
2883 | | - length = [tableData length]; |
2884 | 2883 | /* |
2885 | 2884 | * A localisation file can be: |
2886 | | - * - UTF-16 with a leading BOM, |
2887 | | - * - UTF-8, |
2888 | | - * - or ASCII with \U escapes. |
| 2885 | + * 1. A reduced "old-style" plist containing only one top-level dictionary |
| 2886 | + * 2. An xml-based or binary plist. |
| 2887 | + */ |
| 2888 | + tableData = [[NSData alloc] initWithContentsOfFile: tablePath]; |
| 2889 | + if (tableData == nil) |
| 2890 | + { |
| 2891 | + NSWarnMLog(@"Failed read contents of file at path %@", tablePath); |
| 2892 | + goto end; |
| 2893 | + } |
| 2894 | + |
| 2895 | + /* |
| 2896 | + * Check if tableData is a proper plist |
| 2897 | + */ |
| 2898 | + format = [NSPropertyListSerialization formatFromData: tableData]; |
| 2899 | + if (format != 0) |
| 2900 | + { |
| 2901 | + NSString *errorDescription = nil; |
| 2902 | + table = [NSPropertyListSerialization propertyListFromData: tableData |
| 2903 | + mutabilityOption: NSPropertyListImmutable |
| 2904 | + format: &format |
| 2905 | + errorDescription: &errorDescription]; |
| 2906 | + if (table == nil) |
| 2907 | + { |
| 2908 | + NSWarnMLog(@"Expected localization file at path %@ to be a property list" |
| 2909 | + @" of type %lu, but parsing failed - %@", tableData, format, errorDescription); |
| 2910 | + } |
| 2911 | + else |
| 2912 | + { |
| 2913 | + [_localizations setObject: table forKey: tableName]; |
| 2914 | + } |
| 2915 | + |
| 2916 | + goto end; |
| 2917 | + } |
| 2918 | + |
| 2919 | + |
| 2920 | + /* |
| 2921 | + * The following encodings are supported for the reduced "old-style" plist: |
| 2922 | + * - UTF-16 with a leading BOM, |
| 2923 | + * - UTF-8, |
| 2924 | + * - or ASCII with \U escapes. |
2889 | 2925 | */ |
| 2926 | + bytes = [tableData bytes]; |
| 2927 | + length = [tableData length]; |
2890 | 2928 | if (length > 2 |
2891 | 2929 | && ((bytes[0] == 0xFF && bytes[1] == 0xFE) |
2892 | 2930 | || (bytes[0] == 0xFE && bytes[1] == 0xFF))) |
@@ -2949,6 +2987,7 @@ - (NSString *) localizedStringForKey: (NSString *)key |
2949 | 2987 | [_localizations setObject: table forKey: tableName]; |
2950 | 2988 | } |
2951 | 2989 |
|
| 2990 | + end: |
2952 | 2991 | if (key == nil || (newString = [table objectForKey: key]) == nil) |
2953 | 2992 | { |
2954 | 2993 | NSString *show = [[NSUserDefaults standardUserDefaults] |
|
0 commit comments