Skip to content

Commit

Permalink
Merge pull request #1286 from visualize-admin/fix/allow-localized-lan…
Browse files Browse the repository at this point in the history
…ding-pages

feat: Landing page can be localized
  • Loading branch information
bprusinowski authored Dec 1, 2023
2 parents 08fbffb + 35912dd commit 484475f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

## Unreleased

Nothing yet.
- Features
- Localized cube landing pages are now supported (dcat:landingPage) 🌎

# [3.24.2] - 2023-11-28

Expand Down
5 changes: 4 additions & 1 deletion app/rdf/query-cube-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ export const getCubeMetadata = async (
?contactPoint ${ns.vcard.hasEmail} ?contactPointEmail .
}
OPTIONAL { ?iri ${ns.dcterms.publisher} ?publisher . }
OPTIONAL { ?iri ${ns.dcat.landingPage} ?landingPage . }
${buildLocalizedSubQuery("iri", "dcat:landingPage", "landingPage", {
locale,
fallbackToNonLocalized: true,
})}
OPTIONAL { ?iri ${ns.schema.expires} ?expires . }
OPTIONAL { ?iri ${ns.schema.workExample} ?workExample . }
`.GROUP().BY`?iri`.THEN.BY`?identifier`.THEN.BY`?title`.THEN.BY`?description`
Expand Down
67 changes: 67 additions & 0 deletions app/rdf/query-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { buildLocalizedSubQuery } from "./query-utils";

describe("buildLocalizedSubQuery", () => {
it("should build a subquery with the given locale", () => {
const subQuery = buildLocalizedSubQuery("s", "p", "o", {
locale: "it",
});
expect(subQuery).toEqual(
// it locale must be first!
`OPTIONAL {
?s p ?o_it .
FILTER(LANG(?o_it) = "it")
}
OPTIONAL {
?s p ?o_de .
FILTER(LANG(?o_de) = "de")
}
OPTIONAL {
?s p ?o_fr .
FILTER(LANG(?o_fr) = "fr")
}
OPTIONAL {
?s p ?o_en .
FILTER(LANG(?o_en) = "en")
}
OPTIONAL {
?s p ?o_ .
FILTER(LANG(?o_) = "")
}
BIND(COALESCE(?o_it, ?o_de, ?o_fr, ?o_en, ?o_) AS ?o)`
);
});

it("should build a subquery with the given locale, falling back to non-localized property", () => {
const subQuery = buildLocalizedSubQuery("s", "p", "o", {
locale: "en",
fallbackToNonLocalized: true,
});
expect(subQuery).toEqual(
// en locale must be first!
`OPTIONAL {
?s p ?o_en .
FILTER(LANG(?o_en) = "en")
}
OPTIONAL {
?s p ?o_de .
FILTER(LANG(?o_de) = "de")
}
OPTIONAL {
?s p ?o_fr .
FILTER(LANG(?o_fr) = "fr")
}
OPTIONAL {
?s p ?o_it .
FILTER(LANG(?o_it) = "it")
}
OPTIONAL {
?s p ?o_ .
FILTER(LANG(?o_) = "")
}
OPTIONAL {
?s p ?o_raw .
}
BIND(COALESCE(?o_en, ?o_de, ?o_fr, ?o_it, ?o_, ?o_raw) AS ?o)`
);
});
});
29 changes: 20 additions & 9 deletions app/rdf/query-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@ export const buildLocalizedSubQuery = (
s: string,
p: string,
o: string,
{ locale }: { locale: string }
{
locale,
fallbackToNonLocalized,
}: {
locale: string;
fallbackToNonLocalized?: boolean;
}
) => {
// Include the empty locale as well.
const locales = getOrderedLocales(locale).concat("");

return `${locales
.map(
(locale) => `OPTIONAL {
?${s} ${p} ?${o}_${locale} .
FILTER(LANG(?${o}_${locale}) = "${locale}")
}`
?${s} ${p} ?${o}_${locale} .
FILTER(LANG(?${o}_${locale}) = "${locale}")
}`
)
.join("\n")}
BIND(COALESCE(${locales
.map((locale) => `?${o}_${locale}`)
.join(", ")}) AS ?${o})
`;
.join("\n")}${
fallbackToNonLocalized
? `\nOPTIONAL {
?${s} ${p} ?${o}_raw .
}`
: ""
}
BIND(COALESCE(${locales.map((locale) => `?${o}_${locale}`).join(", ")}${
fallbackToNonLocalized ? `, ?${o}_raw` : ``
}) AS ?${o})`;
};

const getOrderedLocales = (locale: string) => {
Expand Down

1 comment on commit 484475f

@vercel
Copy link

@vercel vercel bot commented on 484475f Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app

Please sign in to comment.