Skip to content

Commit e0176db

Browse files
committed
Change how LANG is computed. Try language+country+encoding, then language+country, then language. Use same algorithm regardless of OS version. Ignore everything after the first - in the language since locales do not look like zh-Hans_CN.UTF-8, but do look like zh_CN.UTF-8
1 parent f5d7f8f commit e0176db

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

sources/NSArray+iTerm.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- (NSArray *)mapWithBlock:(id (^)(ObjectType anObject))block;
2222
- (NSArray *)flatMapWithBlock:(NSArray *(^)(ObjectType anObject))block;
2323

24+
- (NSArray<ObjectType> *)flattenedArray;
25+
2426
- (id)reduceWithBlock:(id (^)(ObjectType first, ObjectType second))block;
2527
- (id)reduceWithFirstValue:(id)firstValue block:(id (^)(id first, ObjectType second))block;
2628

sources/NSArray+iTerm.m

+13-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ - (NSArray *)flatMapWithBlock:(NSArray *(^)(id anObject))block {
6767
return temp;
6868
}
6969

70-
- (NSArray *)filteredArrayUsingBlock:(BOOL (^NS_NOESCAPE)(id anObject))block {
70+
- (NSArray *)flattenedArray {
71+
NSMutableArray *result = [NSMutableArray array];
72+
for (id object in self) {
73+
if ([object isKindOfClass:[NSArray class]]) {
74+
[result addObjectsFromArray:object];
75+
} else {
76+
[result addObject:object];
77+
}
78+
}
79+
return result;
80+
}
81+
82+
- (NSArray *)filteredArrayUsingBlock:(BOOL (^NS_NOESCAPE)(id))block {
7183
NSIndexSet *indexes = [self indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj,
7284
NSUInteger idx,
7385
BOOL * _Nonnull stop) {

sources/PTYSession.m

+37-35
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ - (void)computeEnvironmentForNewJobFromEnvironment:(NSDictionary *)environment
20152015
if (!_profile[KEY_SET_LOCALE_VARS] ||
20162016
[_profile[KEY_SET_LOCALE_VARS] boolValue]) {
20172017
DLog(@"Setting locale vars...");
2018-
NSString *lang = [self _lang];
2018+
NSString *lang = [self valueForLanguageEnvironmentVariable];
20192019
if (lang) {
20202020
DLog(@"set LANG=%@", lang);
20212021
env[@"LANG"] = lang;
@@ -7812,46 +7812,48 @@ - (NSString *)localeForLanguage:(NSString *)languageCode
78127812
}
78137813
}
78147814

7815-
- (NSArray<NSString *>*)possibleLocales {
7816-
NSString* countryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
7817-
NSString* languageCode = nil;
7818-
if (@available(macOS 10.14, *)) {
7819-
NSArray *options = [[NSLocale preferredLanguages] mapWithBlock:^id(NSString *mostPreferredLanguage) {
7820-
NSUInteger index = [mostPreferredLanguage rangeOfString:@"-" options:NSBackwardsSearch].location;
7821-
if (index == NSNotFound) {
7822-
return nil;
7823-
}
7824-
return [self localeForLanguage:[mostPreferredLanguage substringToIndex:index] country:countryCode];
7825-
}];
7826-
if (options.count > 0) {
7827-
return options;
7815+
- (NSString *)valueForLanguageEnvironmentVariable {
7816+
DLog(@"Looking for a locale...");
7817+
NSArray<NSString *> *languageCodes = [[NSLocale preferredLanguages] mapWithBlock:^id(NSString *language) {
7818+
DLog(@"Found preferred language: %@", language);
7819+
NSUInteger index = [language rangeOfString:@"-" options:0].location;
7820+
if (index == NSNotFound) {
7821+
return language;
7822+
} else {
7823+
return [language substringToIndex:index];
78287824
}
7825+
}];
7826+
DLog(@"Preferred languages are: %@", languageCodes);
7827+
7828+
NSString *const countryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
7829+
NSArray<NSString *> *languagePlusCountryCodes = @[];
7830+
if (countryCode) {
7831+
languagePlusCountryCodes = [languageCodes mapWithBlock:^id(NSString *language) {
7832+
return [self localeForLanguage:language country:countryCode];
7833+
}];
78297834
}
7830-
// 10.13 code path. On 10.14 the language is always en.
7831-
languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
7832-
NSString *theLocale = [self localeForLanguage:languageCode country:countryCode];
7833-
DLog(@"Taking legacy code path and returning locale of %@", theLocale);
7834-
return theLocale ? @[ theLocale ] : nil;
7835-
}
7835+
DLog(@"Country code is %@. Combos are %@", countryCode, languagePlusCountryCodes);
78367836

7837-
- (NSString*)_lang {
7838-
NSArray<NSString *> *possibleLocales = [self possibleLocales];
78397837
NSString *encoding = [self encodingName];
7840-
DLog(@"locale candidates=%@, encoding=%@", possibleLocales, encoding);
7841-
if (encoding && possibleLocales.count) {
7842-
NSArray<NSString *> *candidatesWithEncoding = [possibleLocales mapWithBlock:^id(NSString *locale) {
7843-
return [NSString stringWithFormat:@"%@.%@", locale, encoding];
7844-
}];
7845-
DLog(@"Locale candidates with encoding are %@", candidatesWithEncoding);
7846-
NSString *result = [candidatesWithEncoding objectPassingTest:^BOOL(NSString *locale, NSUInteger index, BOOL *stop) {
7847-
return [self _localeIsSupported:locale];
7838+
NSArray<NSString *> *languageCountryEncoding = @[];
7839+
if (encoding) {
7840+
languageCountryEncoding = [languagePlusCountryCodes mapWithBlock:^id(NSString *languageCountry) {
7841+
return [NSString stringWithFormat:@"%@.%@", languageCountry, encoding];
78487842
}];
7849-
DLog(@"First supported locale is %@", result);
7850-
return result;
7851-
} else {
7852-
DLog(@"No locale or encoding, returning nil language");
7853-
return nil;
78547843
}
7844+
DLog(@"Encoding is %@. Combos are %@", encoding, languageCountryEncoding);
7845+
7846+
NSArray<NSString *> *candidates = [@[ languageCountryEncoding, languagePlusCountryCodes, languageCodes ] flattenedArray];
7847+
DLog(@"Candidates are: %@", candidates);
7848+
for (NSString *candidate in candidates) {
7849+
DLog(@"Check if %@ is supported", candidate);
7850+
if ([self _localeIsSupported:candidate]) {
7851+
DLog(@"YES. Using %@", candidate);
7852+
return candidate;
7853+
}
7854+
DLog(@"No");
7855+
}
7856+
return nil;
78557857
}
78567858

78577859
- (void)setDvrFrame {

0 commit comments

Comments
 (0)