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

enhance(l10n): localize en-US link title #11011

Merged
merged 11 commits into from
May 2, 2024
4 changes: 3 additions & 1 deletion build/flaws/broken-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isValidLocale } from "../../libs/locale-utils/index.js";
import * as cheerio from "cheerio";
import { Doc } from "../../libs/types/document.js";
import { Flaw } from "./index.js";
import { ONLY_AVAILABLE_IN_ENGLISH } from "../../libs/l10n/l10n.js";

const _safeToHttpsDomains = new Map();

Expand Down Expand Up @@ -50,13 +51,14 @@ function mutateLink(
if (isSelfLink) {
$element.attr("aria-current", "page");
} else if (enUSFallback) {
const locale = $element.attr("href").match(/^\/([^/]+)\//)[1];
// If we have an English (US) fallback, then we use this first.
// As we still suggest the translated version even if we only
// have an English (US) version.
$element.attr("href", enUSFallback);
$element.append(` <small>(${DEFAULT_LOCALE})<small>`);
$element.addClass("only-in-en-us");
$element.attr("title", "Currently only available in English (US)");
$element.attr("title", ONLY_AVAILABLE_IN_ENGLISH(locale));
} else if (suggestion) {
$element.attr("href", suggestion);
} else {
Expand Down
7 changes: 6 additions & 1 deletion kumascript/src/api/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

import { CONTENT_ROOT } from "../../../libs/env/index.js";
import { KumaThis } from "../environment.js";
import { DEFAULT_LOCALE } from "../../../libs/constants/index.js";

Check warning on line 8 in kumascript/src/api/web.ts

View workflow job for this annotation

GitHub Actions / lint

'DEFAULT_LOCALE' is defined but never used
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import { DEFAULT_LOCALE } from "../../../libs/constants/index.js";

import { ONLY_AVAILABLE_IN_ENGLISH } from "../../../libs/l10n/l10n.js";

const DUMMY_BASE_URL = "https://example.com";

const _warned = new Map();

// The purpose of this function is to make sure `console.warn` is only called once
// per 'macro' per 'href'.
// There are some macros that use `smartLink` within themselves and these macros
Expand Down Expand Up @@ -152,9 +155,11 @@
)}"`;
}
content ??= enUSPage.short_title ?? enUSPage.title;
const title = ONLY_AVAILABLE_IN_ENGLISH(this.env.locale);

return (
'<a class="only-in-en-us" ' +
'title="Currently only available in English (US)" ' +
`title="${title}" ` +
caugner marked this conversation as resolved.
Show resolved Hide resolved
`href="${enUSPage.url}"${flawAttribute}>${content} <small>(en-US)</small></a>`
);
}
Expand Down
30 changes: 30 additions & 0 deletions libs/l10n/l10n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type Locale =
| "en-US"
| "es"
| "fr"
| "ja"
| "ko"
| "pt-BR"
| "ru"
| "zh-CN"
| "zh-TW";
type TranslatedLocale = Exclude<Locale, "en-US">;
Copy link
Member

Choose a reason for hiding this comment

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

nit: I imagine these will be useful to use elsewhere, perhaps we export them already to save the code change later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 8f33cc6.

type Strings = {
"en-US": string;
} & Record<TranslatedLocale, string>;
caugner marked this conversation as resolved.
Show resolved Hide resolved

function localString(strings: Strings) {
caugner marked this conversation as resolved.
Show resolved Hide resolved
return (locale: string) => strings[locale] ?? strings["en-US"];
}

export const ONLY_AVAILABLE_IN_ENGLISH = localString({
"en-US": "This page is currently only available in English.",
es: "Esta página está disponible solo en inglés.",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mdn/yari-content-es Can you please check this translation? 🙏

Copy link
Member

Choose a reason for hiding this comment

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

It looks good to me 👍 cc @Graywolf9

fr: "Cette page est actuellement disponible uniquement en anglais.",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mdn/yari-content-fr Can you please check this translation? 🙏

ja: "このページは現在、英語のみで利用可能です。",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mdn/yari-content-ja Can you please check this translation? 🙏

ko: "이 페이지는 현재 영어로만 제공됩니다.",
caugner marked this conversation as resolved.
Show resolved Hide resolved
"pt-BR": "Esta página está disponível apenas em inglês no momento.",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mdn/yari-content-pt-br Can you please check this translation? 🙏

ru: "Эта страница в настоящее время доступна только на английском.",
caugner marked this conversation as resolved.
Show resolved Hide resolved
caugner marked this conversation as resolved.
Show resolved Hide resolved
"zh-CN": "此页面目前仅提供英文版本。",
"zh-TW": "此頁面目前僅提供英文版本。",
caugner marked this conversation as resolved.
Show resolved Hide resolved
});
Loading