@@ -2015,7 +2015,7 @@ - (void)computeEnvironmentForNewJobFromEnvironment:(NSDictionary *)environment
2015
2015
if (!_profile[KEY_SET_LOCALE_VARS] ||
2016
2016
[_profile[KEY_SET_LOCALE_VARS] boolValue]) {
2017
2017
DLog(@"Setting locale vars...");
2018
- NSString *lang = [self _lang ];
2018
+ NSString *lang = [self valueForLanguageEnvironmentVariable ];
2019
2019
if (lang) {
2020
2020
DLog(@"set LANG=%@", lang);
2021
2021
env[@"LANG"] = lang;
@@ -7812,46 +7812,48 @@ - (NSString *)localeForLanguage:(NSString *)languageCode
7812
7812
}
7813
7813
}
7814
7814
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];
7828
7824
}
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
+ }];
7829
7834
}
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);
7836
7836
7837
- - (NSString*)_lang {
7838
- NSArray<NSString *> *possibleLocales = [self possibleLocales];
7839
7837
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];
7848
7842
}];
7849
- DLog(@"First supported locale is %@", result);
7850
- return result;
7851
- } else {
7852
- DLog(@"No locale or encoding, returning nil language");
7853
- return nil;
7854
7843
}
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;
7855
7857
}
7856
7858
7857
7859
- (void)setDvrFrame {
0 commit comments