Skip to content

Commit

Permalink
Detect dir of rtl or ltr based on BCP47 language codes (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid authored Dec 12, 2024
1 parent f088b28 commit 7f8f210
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/components/Scroll/Annotation/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EmbeddedResource } from "@iiif/presentation-3";
import { ScrollContext } from "src/context/scroll-context";
import { TextualBody } from "src/components/Scroll/Annotation/Body.styled";
import { getSearchResultSnippet } from "src/lib/search-helpers";
import { getLanguageDirection } from "src/lib/annotation-helpers";
import useMarkdown from "@nulib/use-markdown";

const ScrollAnnotationBody = ({
Expand Down Expand Up @@ -62,7 +63,7 @@ const ScrollAnnotationBody = ({
}

const id = [body.id, type].join("-");
const isRtl = ["ar"].includes(String(body.language));
const isRtl = getLanguageDirection(String(body.language)) === "RTL";
const dir = isRtl ? "rtl" : "ltr";
const fontSize = isRtl ? "1.3em" : "1em";
const lang = String(body.language);
Expand Down
48 changes: 48 additions & 0 deletions src/lib/annotation-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
import {
getLanguageDirection,
parseAnnotationTarget,
AnnotationTargetExtended,
} from "./annotation-helpers";

describe("getLanguageDirection", () => {
it("returns 'RTL' for Arabic", () => {
const result = getLanguageDirection("ar");
expect(result).toEqual("RTL");
});
it("returns 'RTL' for Hebrew", () => {
const result = getLanguageDirection("he");
expect(result).toEqual("RTL");
});
it("returns 'LTR' for English", () => {
const result = getLanguageDirection("en");
expect(result).toEqual("LTR");
});
it("returns 'LTR' for French", () => {
const result = getLanguageDirection("fr");
expect(result).toEqual("LTR");
});
it("returns 'RTL' for Persian", () => {
const result = getLanguageDirection("fa");
expect(result).toEqual("RTL");
});
it("returns 'RTL' for Urdu", () => {
const result = getLanguageDirection("ur");
expect(result).toEqual("RTL");
});
it("returns 'LTR' for Spanish", () => {
const result = getLanguageDirection("es");
expect(result).toEqual("LTR");
});
it("returns 'LTR' for German", () => {
const result = getLanguageDirection("de");
expect(result).toEqual("LTR");
});
it("returns 'RTL' for Kurdish", () => {
const result = getLanguageDirection("ku");
expect(result).toEqual("RTL");
});
it("returns 'RTL' for Pashto", () => {
const result = getLanguageDirection("ps");
expect(result).toEqual("RTL");
});
it("returns 'RTL' for Divehi", () => {
const result = getLanguageDirection("dv");
expect(result).toEqual("RTL");
});
});

describe("parseAnnotationTarget", () => {
it("handles target strings with xywh", () => {
const target = "http://example.com/canvas/1#xywh=100,200,300,400";
Expand Down
25 changes: 24 additions & 1 deletion src/lib/annotation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ export type AnnotationTargetExtended = AnnotationTarget & {
svg?: string;
};

const getLanguageDirection = (bcp47Code) => {
// Exhaustive list of RTL languages
const rtlLanguages = [
"ar",
"fa",
"ur",
"ps",
"dv",
"sd",
"ug",
"ku",
"he",
"yi",
"jrb",
"jpr",
"nqo",
];

// Get the base language from the BCP47 code
const baseLang = bcp47Code.split("-")[0]; // Extract the base language
return rtlLanguages.includes(baseLang) ? "RTL" : "LTR";
};

const parseAnnotationTarget = (target: AnnotationTargetExtended | string) => {
let parsedTarget: ParsedAnnotationTarget = {
id: typeof target === "string" ? target : target.source,
Expand Down Expand Up @@ -98,4 +121,4 @@ function extractLanguages(annotations: AnnotationNormalized[]) {
return Array.from(languages);
}

export { extractLanguages, parseAnnotationTarget };
export { getLanguageDirection, extractLanguages, parseAnnotationTarget };

0 comments on commit 7f8f210

Please sign in to comment.