Skip to content

Commit

Permalink
Change how LANG is computed. Try language+country+encoding, then lang…
Browse files Browse the repository at this point in the history
…uage+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
  • Loading branch information
gnachman committed Oct 17, 2018
1 parent f5d7f8f commit e0176db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
2 changes: 2 additions & 0 deletions sources/NSArray+iTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- (NSArray *)mapWithBlock:(id (^)(ObjectType anObject))block;
- (NSArray *)flatMapWithBlock:(NSArray *(^)(ObjectType anObject))block;

- (NSArray<ObjectType> *)flattenedArray;

- (id)reduceWithBlock:(id (^)(ObjectType first, ObjectType second))block;
- (id)reduceWithFirstValue:(id)firstValue block:(id (^)(id first, ObjectType second))block;

Expand Down
14 changes: 13 additions & 1 deletion sources/NSArray+iTerm.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
72 changes: 37 additions & 35 deletions sources/PTYSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -7812,46 +7812,48 @@ - (NSString *)localeForLanguage:(NSString *)languageCode
}
}

- (NSArray<NSString *>*)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<NSString *> *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<NSString *> *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<NSString *> *possibleLocales = [self possibleLocales];
NSString *encoding = [self encodingName];
DLog(@"locale candidates=%@, encoding=%@", possibleLocales, encoding);
if (encoding && possibleLocales.count) {
NSArray<NSString *> *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<NSString *> *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<NSString *> *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 {
Expand Down

0 comments on commit e0176db

Please sign in to comment.