diff --git a/src/player.js b/src/player.js index ac4c3c7ea..598b14192 100644 --- a/src/player.js +++ b/src/player.js @@ -2507,12 +2507,20 @@ export default class Player extends FakeEventTarget { * @private */ _setDefaultTrack(tracks: Array, language: string, defaultTrack: ?Track): void { - const track: ?T = tracks.find(track => Track.langComparer(language, track.language)); - if (track) { + const updateTrack = track => { this.selectTrack(track); this._markActiveTrack(track); - } else if (defaultTrack && !defaultTrack.active) { - this.selectTrack(defaultTrack); + }; + const sameTrack: ?T = tracks.find(track => Track.langComparer(language, track.language, true)); + if (sameTrack) { + updateTrack(sameTrack); + } else { + const track: ?T = tracks.find(track => Track.langComparer(language, track.language, false)); + if (track) { + updateTrack(track); + } else if (defaultTrack && !defaultTrack.active) { + this.selectTrack(defaultTrack); + } } } diff --git a/src/track/track.js b/src/track/track.js index c4b4fa251..d9fc23299 100644 --- a/src/track/track.js +++ b/src/track/track.js @@ -9,13 +9,18 @@ export default class Track { * Comparing language strings. * @param {string} inputLang - The configured language. * @param {string} trackLang - The default track language. + * @param {boolean} equal - Optional flag to check for matching languages. * @returns {boolean} - Whether the strings are equal or starts with the same substring. */ - static langComparer(inputLang: string, trackLang: string): boolean { + static langComparer(inputLang: string, trackLang: string, equal: ?boolean): boolean { try { inputLang = inputLang.toLowerCase(); trackLang = trackLang.toLowerCase(); - return inputLang ? inputLang.startsWith(trackLang) || trackLang.startsWith(inputLang) : false; + if (equal) { + return inputLang ? inputLang === trackLang : false; + } else { + return inputLang ? inputLang.startsWith(trackLang) || trackLang.startsWith(inputLang) : false; + } } catch (e) { return false; } diff --git a/test/src/track/track.spec.js b/test/src/track/track.spec.js index 16b49c880..f167ed98d 100644 --- a/test/src/track/track.spec.js +++ b/test/src/track/track.spec.js @@ -19,5 +19,11 @@ describe('Track', () => { it('should return false if the input lang is empty', () => { Track.langComparer('', 'rus').should.be.false; }); + + it('should compare languages using equality flag', () => { + Track.langComparer('zh', 'zh', true).should.be.true; + Track.langComparer('zh_tw', 'zh', true).should.be.false; + Track.langComparer('zh_tw', 'zh', false).should.be.true; + }); }); });