Skip to content

Commit

Permalink
fix: handle site lang with region subtags in text track sorting (#1163)
Browse files Browse the repository at this point in the history
Co-authored-by: Maham Akif <maham.akif@A006-00821.local>
  • Loading branch information
mahamakifdar19 and Maham Akif authored Aug 21, 2024
1 parent 9e9ef9b commit 94a1676
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/components/video/data/tests/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ describe('useTranscripts', () => {
const textTracks = { en: 'https://example.com/transcript-en.txt' };
fetchAndAddTranscripts.mockResolvedValueOnce(textTracks);

const { result, waitForNextUpdate } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions }));
const { result, waitForNextUpdate } = renderHook(() => useTranscripts({
player: mockPlayer,
customOptions,
siteLanguage: 'en',
}));

await waitForNextUpdate();

Expand Down
8 changes: 6 additions & 2 deletions src/components/video/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ export const createWebVttFile = (webVttContent) => {
};

export const sortTextTracks = (tracks, siteLanguage) => {
// Some language codes returned by getLocale include a hyphen, which may not match the codes in the text tracks.
// To ensure proper matching, the hyphen is removed from the site language code if present.
const preferredLanguage = siteLanguage?.includes('-') ? siteLanguage.split('-')[0].toLowerCase() : siteLanguage.toLowerCase();

const sortedKeys = Object.keys(tracks).sort((a, b) => {
if (a === siteLanguage) { return -1; }
if (b === siteLanguage) { return 1; }
if (a === preferredLanguage) { return -1; }
if (b === preferredLanguage) { return 1; }
return a.localeCompare(b);
});

Expand Down

0 comments on commit 94a1676

Please sign in to comment.