diff --git a/sources/NSArray+iTerm.h b/sources/NSArray+iTerm.h index 979e132746..8f3b8e1a5b 100644 --- a/sources/NSArray+iTerm.h +++ b/sources/NSArray+iTerm.h @@ -21,6 +21,8 @@ - (NSArray *)mapWithBlock:(id (^)(ObjectType anObject))block; - (NSArray *)flatMapWithBlock:(NSArray *(^)(ObjectType anObject))block; +- (NSArray *)flattenedArray; + - (id)reduceWithBlock:(id (^)(ObjectType first, ObjectType second))block; - (id)reduceWithFirstValue:(id)firstValue block:(id (^)(id first, ObjectType second))block; diff --git a/sources/NSArray+iTerm.m b/sources/NSArray+iTerm.m index c3af9aaa68..2b3524d0c0 100644 --- a/sources/NSArray+iTerm.m +++ b/sources/NSArray+iTerm.m @@ -67,7 +67,19 @@ - (NSArray *)flatMapWithBlock:(NSArray *(^)(id anObject))block { return temp; } -- (NSArray *)filteredArrayUsingBlock:(BOOL (^NS_NOESCAPE)(id anObject))block { +- (NSArray *)flattenedArray { + NSMutableArray *result = [NSMutableArray array]; + for (id object in self) { + if ([object isKindOfClass:[NSArray class]]) { + [result addObjectsFromArray:object]; + } else { + [result addObject:object]; + } + } + return result; +} + +- (NSArray *)filteredArrayUsingBlock:(BOOL (^NS_NOESCAPE)(id))block { NSIndexSet *indexes = [self indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { diff --git a/sources/PTYSession.m b/sources/PTYSession.m index 5388cd8c7b..d218833302 100644 --- a/sources/PTYSession.m +++ b/sources/PTYSession.m @@ -2015,7 +2015,7 @@ - (void)computeEnvironmentForNewJobFromEnvironment:(NSDictionary *)environment if (!_profile[KEY_SET_LOCALE_VARS] || [_profile[KEY_SET_LOCALE_VARS] boolValue]) { DLog(@"Setting locale vars..."); - NSString *lang = [self _lang]; + NSString *lang = [self valueForLanguageEnvironmentVariable]; if (lang) { DLog(@"set LANG=%@", lang); env[@"LANG"] = lang; @@ -7812,46 +7812,48 @@ - (NSString *)localeForLanguage:(NSString *)languageCode } } -- (NSArray*)possibleLocales { - NSString* countryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; - NSString* languageCode = nil; - if (@available(macOS 10.14, *)) { - NSArray *options = [[NSLocale preferredLanguages] mapWithBlock:^id(NSString *mostPreferredLanguage) { - NSUInteger index = [mostPreferredLanguage rangeOfString:@"-" options:NSBackwardsSearch].location; - if (index == NSNotFound) { - return nil; - } - return [self localeForLanguage:[mostPreferredLanguage substringToIndex:index] country:countryCode]; - }]; - if (options.count > 0) { - return options; +- (NSString *)valueForLanguageEnvironmentVariable { + DLog(@"Looking for a locale..."); + NSArray *languageCodes = [[NSLocale preferredLanguages] mapWithBlock:^id(NSString *language) { + DLog(@"Found preferred language: %@", language); + NSUInteger index = [language rangeOfString:@"-" options:0].location; + if (index == NSNotFound) { + return language; + } else { + return [language substringToIndex:index]; } + }]; + DLog(@"Preferred languages are: %@", languageCodes); + + NSString *const countryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; + NSArray *languagePlusCountryCodes = @[]; + if (countryCode) { + languagePlusCountryCodes = [languageCodes mapWithBlock:^id(NSString *language) { + return [self localeForLanguage:language country:countryCode]; + }]; } - // 10.13 code path. On 10.14 the language is always en. - languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]; - NSString *theLocale = [self localeForLanguage:languageCode country:countryCode]; - DLog(@"Taking legacy code path and returning locale of %@", theLocale); - return theLocale ? @[ theLocale ] : nil; -} + DLog(@"Country code is %@. Combos are %@", countryCode, languagePlusCountryCodes); -- (NSString*)_lang { - NSArray *possibleLocales = [self possibleLocales]; NSString *encoding = [self encodingName]; - DLog(@"locale candidates=%@, encoding=%@", possibleLocales, encoding); - if (encoding && possibleLocales.count) { - NSArray *candidatesWithEncoding = [possibleLocales mapWithBlock:^id(NSString *locale) { - return [NSString stringWithFormat:@"%@.%@", locale, encoding]; - }]; - DLog(@"Locale candidates with encoding are %@", candidatesWithEncoding); - NSString *result = [candidatesWithEncoding objectPassingTest:^BOOL(NSString *locale, NSUInteger index, BOOL *stop) { - return [self _localeIsSupported:locale]; + NSArray *languageCountryEncoding = @[]; + if (encoding) { + languageCountryEncoding = [languagePlusCountryCodes mapWithBlock:^id(NSString *languageCountry) { + return [NSString stringWithFormat:@"%@.%@", languageCountry, encoding]; }]; - DLog(@"First supported locale is %@", result); - return result; - } else { - DLog(@"No locale or encoding, returning nil language"); - return nil; } + DLog(@"Encoding is %@. Combos are %@", encoding, languageCountryEncoding); + + NSArray *candidates = [@[ languageCountryEncoding, languagePlusCountryCodes, languageCodes ] flattenedArray]; + DLog(@"Candidates are: %@", candidates); + for (NSString *candidate in candidates) { + DLog(@"Check if %@ is supported", candidate); + if ([self _localeIsSupported:candidate]) { + DLog(@"YES. Using %@", candidate); + return candidate; + } + DLog(@"No"); + } + return nil; } - (void)setDvrFrame {