Skip to content

Set language attribute on resource title and descriptions #2346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2025
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 @@ -114,6 +114,25 @@ describe("Learning Resource Expanded", () => {
screen.getByText(resource.title)
})

test("Sets lang attribute on title and description", () => {
const resource = factories.learningResources.resource({
resource_type: ResourceTypeEnum.Course,
runs: [
factories.learningResources.run({
languages: ["en-us"],
}),
],
})

setup({ resource })

const title = screen.getByText(resource.title)
expect(title).toHaveAttribute("lang", "en-us")

const description = screen.getByText(resource.description!)
expect(description).toHaveAttribute("lang", "en-us")
})

test.each([ResourceTypeEnum.Program, ResourceTypeEnum.LearningPath])(
'Renders CTA button for resource type "%s"',
(resourceType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { useCallback, useRef, useState } from "react"
import styled from "@emotion/styled"
import { Skeleton, theme, Link } from "ol-components"
import type { LearningResource } from "api"
import { getResourceLanguage } from "ol-utilities"

const DescriptionContainer = styled.div({
display: "flex",
flexDirection: "column",
Expand Down Expand Up @@ -83,6 +85,7 @@ const ResourceDescription = ({ resource }: { resource?: LearningResource }) => {
* backend during ETL. This is safe to render.
*/
dangerouslySetInnerHTML={{ __html: resource.description || "" }}
lang={getResourceLanguage(resource)}
/>
{(isClamped || clampedOnFirstRender.current) && (
<Link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const TitleSection: React.FC<{
>
{type}
</Typography>
{title}
<span lang={resource?.runs?.[0]?.languages?.[0]}>{title}</span>
</Typography>
<CloseButton
variant="text"
Expand Down
1 change: 1 addition & 0 deletions frontends/ol-components/src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type TitleProps = {
href?: string
lines?: number
style?: CSSProperties
lang?: string
}

type SlotProps = { children?: ReactNode; style?: CSSProperties }
Expand Down
7 changes: 5 additions & 2 deletions frontends/ol-components/src/components/Card/ListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const Info = styled.div`
export type TitleProps = {
children?: ReactNode
href?: string
lang?: string
}
export const Title: React.FC<TitleProps> = styled(Linkable)`
flex-grow: 1;
Expand Down Expand Up @@ -247,8 +248,10 @@ const ListCard: Card = ({
<Body>
<Info>{info}</Info>
{title && (
<Title data-card-link={!!title.href} {...title} href={title.href}>
<TruncateText lineClamp={2}>{title.children}</TruncateText>
<Title data-card-link={!!title.href} href={title.href}>
<TruncateText lineClamp={2} lang={title.lang}>
{title.children}
</TruncateText>
</Title>
)}
<Bottom>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ const ListCardCondensed: Card = ({
<Body>
<Info>{info}</Info>
<Title data-card-link={!!title.href} href={title.href}>
<TruncateText lineClamp={4}>{title.children}</TruncateText>
<TruncateText lineClamp={4} lang={title.lang}>
{title.children}
</TruncateText>
</Title>
<Bottom>
<Footer>{footer}</Footer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ describe("Learning Resource Card", () => {
},
)

test("Sets lang attribute on title and description", () => {
const resource = factories.learningResources.resource({
resource_type: ResourceTypeEnum.Course,
runs: [
factories.learningResources.run({
languages: ["en-us"],
}),
],
})

setup({ resource })

const title = screen.getByText(resource.title)
expect(title).toHaveAttribute("lang", "en-us")
})

test("Displays run start date", () => {
const resource = factories.learningResources.resource({
resource_type: ResourceTypeEnum.Course,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getLearningResourcePrices,
getResourceDate,
showStartAnytime,
getResourceLanguage,
} from "ol-utilities"
import { Card } from "../Card/Card"
import type { Size } from "../Card/Card"
Expand Down Expand Up @@ -241,7 +242,9 @@ const LearningResourceCard: React.FC<LearningResourceCardProps> = ({
<Card.Info>
<Info resource={resource} size={size} />
</Card.Info>
<Card.Title href={href}>{resource.title}</Card.Title>
<Card.Title href={href} lang={getResourceLanguage(resource)}>
{resource.title}
</Card.Title>
<Card.Actions>
{onAddToLearningPathClick && (
<CardActionButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describe("Learning Resource List Card", () => {
},
)

test("Sets lang attribute on title and description", () => {
const resource = factories.learningResources.resource({
resource_type: ResourceTypeEnum.Course,
runs: [
factories.learningResources.run({
languages: ["pt-pt"],
}),
],
})

setup({ resource })

const title = screen.getByText(resource.title)
expect(title).toHaveAttribute("lang", "pt-pt")
})

test("Displays run start date", () => {
const resource = factories.learningResources.resource({
resource_type: ResourceTypeEnum.Course,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getLearningResourcePrices,
getResourceDate,
showStartAnytime,
getResourceLanguage,
} from "ol-utilities"
import { ListCard } from "../Card/ListCard"
import type { ActionButtonProps } from "@mitodl/smoot-design"
Expand Down Expand Up @@ -319,7 +320,9 @@ const LearningResourceListCard: React.FC<LearningResourceListCardProps> = ({
<ListCard.Info>
<Info resource={resource} />
</ListCard.Info>
<ListCard.Title href={href}>{resource.title}</ListCard.Title>
<ListCard.Title href={href} lang={getResourceLanguage(resource)}>
{resource.title}
</ListCard.Title>
<ListCard.Actions>
{onAddToLearningPathClick && (
<CardActionButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LearningResource } from "api"
import {
getReadableResourceType,
getLearningResourcePrices,
getResourceLanguage,
} from "ol-utilities"
import { ListCardCondensed } from "../Card/ListCardCondensed"
import {
Expand Down Expand Up @@ -128,7 +129,7 @@ const LearningResourceListCardCondensed: React.FC<
<ListCardCondensed.Info>
<Info resource={resource} />
</ListCardCondensed.Info>
<ListCardCondensed.Title href={href}>
<ListCardCondensed.Title href={href} lang={getResourceLanguage(resource)}>
{resource.title}
</ListCardCondensed.Title>
<ListCardCondensed.Actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ const allRunsAreIdentical = (resource: LearningResource) => {
)
}

const getResourceLanguage = (resource: LearningResource) => {
return resource.runs?.[resource.runs.length - 1]?.languages?.[0]
}

export {
DEFAULT_RESOURCE_IMG,
embedlyCroppedImage,
Expand All @@ -169,5 +173,6 @@ export {
findBestRun,
formatRunDate,
allRunsAreIdentical,
getResourceLanguage,
}
export type { EmbedlyConfig }
Loading