-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-language.js
30 lines (25 loc) · 1013 Bytes
/
auto-language.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
videojs.registerPlugin('autoLanguage', function() {
var myPlayer = this;
myPlayer.on("loadedmetadata", function () {
// +++ Get the browser language +++
var browser_language = navigator.language ||
navigator.userLanguage; // IE <= 10;
browser_language = browser_language.substr(0, 2);
// +++ Get the captions +++
var track_language;
var tracks = myPlayer.textTracks();
// +++ Loop through captions +++
for (var i = 0; i < (tracks.length); i++) {
track_language = tracks[i].language.substr(0, 2);
// +++ Set the default caption language +++
// When the caption language equals the browser language, then set it as default
if (track_language) {
if (track_language === browser_language) {
tracks[i].mode = "showing";
} else {
tracks[i].mode = "disabled";
}
}
}
});
});