Skip to content

Commit aba7e2b

Browse files
committed
improve accept language header
1 parent 2679ae4 commit aba7e2b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
FB_USER_AGENT: 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)',
5656

5757
ACCEPT_LANGUAGE_SUFFIX: ';q=0.9,en;q=0.7,*;q=0.5',
58+
DEFAULT_ACCEPT_LANGUAGE: 'en,*',
5859

5960
SKIP_IFRAMELY_RENDERS: false,
6061
DEFAULT_ASPECT_RATIO: 16 / 9,
@@ -441,4 +442,4 @@
441442
config.HTTP2_RETRY_CODES[item] = 1;
442443
});
443444

444-
export default config;
445+
export default config;

lib/utils.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,31 @@ export function prepareRequestOptions(request_options, options) {
142142
}
143143
}
144144

145-
var lang = options && options.getProviderOptions && options.getProviderOptions('locale', 'en-US') || 'en-US';
145+
const DEFAULT_LANGUAGE = 'DEFAULT';
146+
var lang = options?.getProviderOptions
147+
// DEFAULT_LANGUAGE returned if value is undefined.
148+
&& options.getProviderOptions('locale', DEFAULT_LANGUAGE)
149+
// DEFAULT_LANGUAGE returned if value is ''.
150+
|| DEFAULT_LANGUAGE;
151+
146152
if (lang && !disable_language) {
153+
154+
if (lang === DEFAULT_LANGUAGE) {
155+
lang = CONFIG.DEFAULT_ACCEPT_LANGUAGE;
156+
} else {
157+
158+
lang = lang.replace('_', '-');
159+
160+
var m = lang.match(/^(\w+)-\w+$/);
161+
if (m) {
162+
lang += `,${m[1]}`;
163+
}
164+
165+
lang = lang + CONFIG.ACCEPT_LANGUAGE_SUFFIX;
166+
}
167+
147168
request_options.headers = request_options.headers || {};
148-
request_options.headers['Accept-Language'] = lang.replace('_', '-') + CONFIG.ACCEPT_LANGUAGE_SUFFIX;
169+
request_options.headers['Accept-Language'] = lang;
149170
}
150171

151172
prepareEncodedUri(request_options, 'uri');
@@ -1388,4 +1409,4 @@ export function isBlocked(url, options, cb) {
13881409
return re.test(domain);
13891410
}
13901411
});
1391-
}
1412+
}

0 commit comments

Comments
 (0)