Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,22 @@ function createAudioTracks(
const languagesOccurences: Partial<Record<string, number>> = {};
for (let i = 0; i < audioTracks.length; i++) {
const audioTrack = audioTracks[i];
const language = audioTrack.language === "" ? "nolang" :
audioTrack.language;
const language = audioTrack.language === "" ? "nolang" : audioTrack.language;
const occurences = languagesOccurences[language] ?? 1;
const id = "gen_audio_" +
language +
"_" +
occurences.toString();
const id = "gen_audio_" + language + "_" + occurences.toString();
languagesOccurences[language] = occurences + 1;
const track = { language: audioTrack.language,
id,
normalized: normalizeLanguage(audioTrack.language),
audioDescription: audioTrack.kind === "descriptions" ||
// Safari seem to prefer the non-standard singular
// version, funnily enough
audioTrack.kind === "description",
representations: [] as Representation[] };
newAudioTracks.push({ track,
nativeTrack: audioTrack });
const track = {
language: audioTrack.language,
id,
normalized: normalizeLanguage(audioTrack.language),
audioDescription:
audioTrack.kind === "descriptions" ||
// Safari seem to prefer the non-standard singular
// version, funnily enough
audioTrack.kind === "description",
representations: [] as Representation[],
};
newAudioTracks.push({ track, nativeTrack: audioTrack });
}
return newAudioTracks;
}
Expand All @@ -126,30 +124,25 @@ function createTextTracks(
const languagesOccurences: Partial<Record<string, number>> = {};
for (let i = 0; i < textTracks.length; i++) {
const textTrack = textTracks[i];
const language = textTrack.language === "" ? "nolang" :
textTrack.language;
const language = textTrack.language === "" ? "nolang" : textTrack.language;
const occurences = languagesOccurences[language] ?? 1;
const id = "gen_text_" +
language +
"_" +
occurences.toString();
const id = "gen_text_" + language + "_" + occurences.toString();
languagesOccurences[language] = occurences + 1;

// Safari seems to be indicating that the subtitles track is a forced
// subtitles track by setting the `kind` attribute to `"forced"`.
// As of now (2023-04-04), this is not standard.
// @see https://github.com/whatwg/html/issues/4472
const forced = (textTrack.kind as string) === "forced" ?
true :
undefined;
const track = { language: textTrack.language,
forced,
label: textTrack.label,
id,
normalized: normalizeLanguage(textTrack.language),
closedCaption: textTrack.kind === "captions" };
newTextTracks.push({ track,
nativeTrack: textTrack });
const forced = (textTrack.kind as string) === "forced" ? true : undefined;
const track = {
language: textTrack.language,
forced,
label: textTrack.label,
id,
normalized: normalizeLanguage(textTrack.language),
closedCaption: textTrack.kind === "captions",
};
newTextTracks.push({ track, nativeTrack: textTrack });
}
return newTextTracks;
}
Expand All @@ -168,13 +161,9 @@ function createVideoTracks(
const languagesOccurences: Partial<Record<string, number>> = {};
for (let i = 0; i < videoTracks.length; i++) {
const videoTrack = videoTracks[i];
const language = videoTrack.language === "" ? "nolang" :
videoTrack.language;
const language = videoTrack.language === "" ? "nolang" : videoTrack.language;
const occurences = languagesOccurences[language] ?? 1;
const id = "gen_video_" +
language +
"_" +
occurences.toString();
const id = "gen_video_" + language + "_" + occurences.toString();
languagesOccurences[language] = occurences + 1;
newVideoTracks.push({ track: { id,
representations: [] as Representation[] },
Expand Down
14 changes: 8 additions & 6 deletions src/utils/languages/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import normalizeLanguage, {
} from "../normalize";

describe("utils - normalizeLanguage", () => {
it("should translate an empty string to an empty string", () => {
expect(normalizeLanguage("")).toBe("");
it("should translate an empty string to an undetermined code", () => {
expect(normalizeLanguage("")).toBe("und");
});

it("should translate ISO639-1 to ISO639-3", () => {
Expand Down Expand Up @@ -58,9 +58,11 @@ describe("utils - normalizeAudioTrack", () => {
});

it("should format a normalized audio track for an empty string", () => {
expect(normalizeAudioTrack("")).toEqual({ language: "",
audioDescription: false,
normalized: "" });
expect(normalizeAudioTrack("")).toEqual({
language: "",
audioDescription: false,
normalized: "und",
});
});

it("should format a normalized audio track for a given language", () => {
Expand Down Expand Up @@ -194,7 +196,7 @@ describe("utils - normalizeTextTrack", () => {
expect(normalizeTextTrack("")).toEqual({
language: "",
closedCaption: false,
normalized: "",
normalized: "und",
});
});

Expand Down
5 changes: 4 additions & 1 deletion src/utils/languages/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ interface INormalizedTextTrackObject extends IMinimalTextTrackObject {
*/
function normalizeLanguage(_language : string) : string {
if (isNullOrUndefined(_language) || _language === "") {
return "";
/**
* "und" is a special value in ISO 639-3 that stands for "undetermined language".
*/
return "und";
}
const fields = ("" + _language).toLowerCase().split("-");
const base = fields[0];
Expand Down