Skip to content
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

fix(content): only collect required translations #8806

Merged
merged 2 commits into from
May 19, 2023
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
14 changes: 6 additions & 8 deletions build/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ interface GlobalMetadata {
}

async function buildDocumentInteractive(
documentPath,
interactive,
documentPath: string,
interactive: boolean,
invalidate = false
): Promise<SkippedDocumentBuild | InteractiveDocumentBuild> {
try {
Expand All @@ -65,12 +65,10 @@ async function buildDocumentInteractive(
}

if (!interactive) {
const translations = await translationsOf(document.metadata);
if (translations && translations.length > 0) {
document.translations = translations;
} else {
document.translations = [];
}
document.translations = translationsOf(
document.metadata.slug,
document.metadata.locale
);
}

return { document, doc: await buildDocument(document), skip: false };
Expand Down
79 changes: 29 additions & 50 deletions content/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,46 @@ const LANGUAGES = new Map(
})
);

const TRANSLATIONS_OF = new Map();
type Translation = {
locale: string;
title: string;
native: string;
};

async function gatherTranslations() {
const iter = (await Document.findAll()).iterDocs();
for (const {
metadata: { slug, locale, title },
} of iter) {
if (!slug || !locale || !title) {
continue;
}
const translation = {
title,
locale,
native: LANGUAGES.get(locale.toLowerCase()).native,
};
const translations = TRANSLATIONS_OF.get(slug.toLowerCase());
if (translations) {
translations.push(translation);
translations.sort(({ locale: a }, { locale: b }) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
} else {
TRANSLATIONS_OF.set(slug.toLowerCase(), [translation]);
}
}
}
const TRANSLATIONS_OF = new Map<string, Array<Translation>>();

export async function translationsOf({ slug, locale: currentLocale }) {
if (TRANSLATIONS_OF.size === 0) {
const label = "Time to gather all translations";
console.time(label);
await gatherTranslations();
console.timeEnd(label);
}
const translations = TRANSLATIONS_OF.get(slug.toLowerCase());
if (translations && currentLocale) {
return translations.filter(
({ locale }) => locale.toLowerCase() !== currentLocale.toLowerCase()
);
// gather and cache all translations of a document,
// then return all translations except the current locale
export function translationsOf(
slug: string,
currentLocale: string
): Translation[] {
let translations = TRANSLATIONS_OF.get(slug.toLowerCase());
if (!translations) {
translations = findTranslations(slug);
TRANSLATIONS_OF.set(slug.toLowerCase(), translations);
}
return translations;
return translations.filter(
({ locale }) => locale.toLowerCase() !== currentLocale.toLowerCase()
);
}

export function findDocumentTranslations(document) {
// return all translations of a document
export function findTranslations(
slug: string,
currentLocale: string = null
): Translation[] {
const translations = [];

for (const locale of VALID_LOCALES.values()) {
if (document.metadata.locale === locale) {
if (currentLocale?.toLowerCase() === locale.toLowerCase()) {
continue;
}
const translatedDocumentURL = document.url.replace(
`/${document.metadata.locale}/`,
`/${locale}/`
);
const translatedDocument = Document.findByURL(translatedDocumentURL);
if (translatedDocument) {
const documentURL = `/${locale}/docs/${slug}`;
const document = Document.findByURL(documentURL);
if (document) {
translations.push({
locale,
title: translatedDocument.metadata.title,
title: document.metadata.title,
native: LANGUAGES.get(locale.toLowerCase()).native,
});
}
Expand Down
9 changes: 6 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
buildLiveSamplePageFromURL,
renderContributorsTxt,
} from "../build/index.js";
import { findDocumentTranslations } from "../content/translations.js";
import { findTranslations } from "../content/translations.js";
import { Document, Redirect, Image } from "../content/index.js";
import { CSP_VALUE, DEFAULT_LOCALE } from "../libs/constants/index.js";
import {
Expand Down Expand Up @@ -47,7 +47,7 @@ import {
findPostPathBySlug,
} from "../build/blog.js";

async function buildDocumentFromURL(url) {
async function buildDocumentFromURL(url: string) {
const document = Document.findByURL(url);
if (!document) {
return null;
Expand All @@ -57,7 +57,10 @@ async function buildDocumentFromURL(url) {
// When you're running the dev server and build documents
// every time a URL is requested, you won't have had the chance to do
// the phase that happens when you do a regular `yarn build`.
document.translations = findDocumentTranslations(document);
document.translations = findTranslations(
document.metadata.slug,
document.metadata.locale
);
}
return await buildDocument(document, documentOptions);
}
Expand Down