Differences From Artifact [ea48dae0ba]:
- File
src/OFLocalization.m
— part of check-in
[44f45c2e35]
at
2017-01-09 17:36:36
on branch trunk
— Update copyright
Forgot to add 2017, even though I already did quite some changes in
2017. (user: js, size: 5040) [annotate] [blame] [check-ins using]
To Artifact [d001be40f2]:
- File src/OFLocalization.m — part of check-in [e731dc4c75] at 2017-01-10 19:39:13 on branch trunk — Implement support for localized strings (user: js, size: 6883) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include "config.h" #include <locale.h> #import "OFLocalization.h" #import "OFString.h" #import "OFInvalidArgumentException.h" static OFLocalization *sharedLocalization = nil; @implementation OFLocalization @synthesize language = _language, territory = _territory, encoding = _encoding; | > > | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "config.h" #include <locale.h> #import "OFLocalization.h" #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFInvalidArgumentException.h" static OFLocalization *sharedLocalization = nil; @implementation OFLocalization @synthesize language = _language, territory = _territory, encoding = _encoding; |
︙ | ︙ | |||
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | return [sharedLocalization encoding]; } + (OFString*)decimalPoint { return [sharedLocalization decimalPoint]; } - initWithLocale: (char*)locale { self = [super init]; if (locale == NULL) { _encoding = OF_STRING_ENCODING_UTF_8; _decimalPoint = @"."; return self; } locale = of_strdup(locale); @try { char *tmp; /* We don't care for extras behind the @ */ if ((tmp = strrchr(locale, '@')) != NULL) *tmp = '\0'; /* Encoding */ if ((tmp = strrchr(locale, '.')) != NULL) { | > > > > > > > > > > > > > < < | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | return [sharedLocalization encoding]; } + (OFString*)decimalPoint { return [sharedLocalization decimalPoint]; } + (void)addLanguageDirectory: (OFString*)path { [sharedLocalization addLanguageDirectory: path]; } - initWithLocale: (char*)locale { self = [super init]; @try { _localizedStrings = [[OFMutableArray alloc] init]; } @catch (id e) { [self release]; @throw e; } if (locale == NULL) { _encoding = OF_STRING_ENCODING_UTF_8; _decimalPoint = @"."; return self; } locale = of_strdup(locale); @try { char *tmp; size_t tmpLen; /* We don't care for extras behind the @ */ if ((tmp = strrchr(locale, '@')) != NULL) *tmp = '\0'; /* Encoding */ if ((tmp = strrchr(locale, '.')) != NULL) { *tmp++ = '\0'; tmpLen = strlen(tmp); for (size_t i = 0; i < tmpLen; i++) tmp[i] = of_ascii_tolower(tmp[i]); if (strcmp(tmp, "utf8") == 0 || |
︙ | ︙ | |||
99 100 101 102 103 104 105 106 107 | else if (strcmp(tmp, "1252") == 0) _encoding = OF_STRING_ENCODING_WINDOWS_1252; } /* Territory */ if ((tmp = strrchr(locale, '_')) != NULL) { *tmp++ = '\0'; _territory = [[OFString alloc] initWithCString: tmp | > > > > > | > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | else if (strcmp(tmp, "1252") == 0) _encoding = OF_STRING_ENCODING_WINDOWS_1252; } /* Territory */ if ((tmp = strrchr(locale, '_')) != NULL) { *tmp++ = '\0'; tmpLen = strlen(tmp); for (size_t i = 0; i < tmpLen; i++) tmp[i] = of_ascii_tolower(tmp[i]); _territory = [[OFString alloc] initWithCString: tmp encoding: OF_STRING_ENCODING_ASCII length: tmpLen]; } tmpLen = strlen(tmp); for (size_t i = 0; i < tmpLen; i++) tmp[i] = of_ascii_tolower(tmp[i]); _language = [[OFString alloc] initWithCString: locale encoding: OF_STRING_ENCODING_ASCII length: tmpLen]; _decimalPoint = [[OFString alloc] initWithCString: localeconv()->decimal_point encoding: _encoding]; } @catch (id e) { [self release]; @throw e; } @finally { free(locale); } sharedLocalization = self; return self; } - (void)dealloc { [_language release]; [_territory release]; [_decimalPoint release]; [_localizedStrings release]; [super dealloc]; } - (void)addLanguageDirectory: (OFString*)path { void *pool = objc_autoreleasePoolPush(); OFString *mapPath = [path stringByAppendingPathComponent: @"languages.json"]; OFDictionary *map = [[OFString stringWithContentsOfFile: mapPath] JSONValue]; OFString *languageFile; languageFile = [[map objectForKey: _language] objectForKey: _territory]; if (languageFile == nil) languageFile = [[map objectForKey: _language] objectForKey: @""]; if (languageFile == nil) { objc_autoreleasePoolPop(pool); return; } languageFile = [path stringByAppendingPathComponent: [languageFile stringByAppendingString: @".json"]]; [_localizedStrings addObject: [[OFString stringWithContentsOfFile: languageFile] JSONValue]]; objc_autoreleasePoolPop(pool); } - (OFString*)localizedStringForID: (OFConstantString*)ID fallback: (OFConstantString*)fallback, ... { OFString *ret; va_list args; |
︙ | ︙ | |||
144 145 146 147 148 149 150 | - (OFString*)localizedStringForID: (OFConstantString*)ID fallback: (OFConstantString*)fallback arguments: (va_list)arguments { OFMutableString *ret = [OFMutableString string]; void *pool = objc_autoreleasePoolPush(); | | < | > > > > > > > > > > > > > > > > > > > > > | 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | - (OFString*)localizedStringForID: (OFConstantString*)ID fallback: (OFConstantString*)fallback arguments: (va_list)arguments { OFMutableString *ret = [OFMutableString string]; void *pool = objc_autoreleasePoolPush(); const char *UTF8String = NULL; size_t last, UTF8StringLength; int state = 0; for (OFDictionary *strings in _localizedStrings) { id string = [strings objectForKey: ID]; if (string == nil) continue; if ([string isKindOfClass: [OFArray class]]) string = [string componentsJoinedByString: @""]; UTF8String = [string UTF8String]; UTF8StringLength = [string UTF8StringLength]; break; } if (UTF8String == NULL) { UTF8String = [fallback UTF8String]; UTF8StringLength = [fallback UTF8StringLength]; } state = 0; last = 0; for (size_t i = 0; i < UTF8StringLength; i++) { switch (state) { case 0: if (UTF8String[i] == '%') { [ret appendUTF8String: UTF8String + last length: i - last]; |
︙ | ︙ |