FIX: Take into account language fallbacks for admin sidebar plugin links (#27061)

This commit fixes an issue where I18n.lookup was being used to
check if a link had a valid I18n key when the `addAdminSidebarSectionLink`
plugin API was used. However this was imperfect -- usually when we do
`I18n.t`, we fall back to the default locale (`en`), but if
`I18n.lookup` is used we do not do this, so we were removing the link
needlessly.

This issue was identified via the plugin API's use in https://github.com/discourse/docker_manager

c.f. https://meta.discourse.org/t/new-admin-sidebar-wheres-the-update-discourse-button/308213/15?u=martin
This commit is contained in:
Martin Brennan 2024-05-20 09:41:53 +10:00 committed by GitHub
parent e019f848bf
commit f2cdc3b2a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 1 deletions

View File

@ -210,7 +210,10 @@ export function addAdminSidebarSectionLink(sectionName, link) {
}
// label must be valid, don't want broken [XYZ translation missing]
if (link.label && typeof I18n.lookup(link.label) !== "string") {
if (
link.label &&
I18n.t(link.label) === I18n.missingTranslation(link.label, null, {})
) {
// eslint-disable-next-line no-console
console.debug(
"[AdminSidebar]",

View File

@ -8,6 +8,7 @@ import {
count,
exists,
} from "discourse/tests/helpers/qunit-helpers";
import I18n from "discourse-i18n";
acceptance("Admin Sidebar - Sections", function (needs) {
needs.user({
@ -210,3 +211,44 @@ acceptance("Admin Sidebar - Sections - Plugin API", function (needs) {
);
});
});
let _locale;
acceptance(
"Admin Sidebar - Sections - Plugin API - Translation Fallbacks",
function (needs) {
needs.user({
admin: true,
groups: [AUTO_GROUPS.admins],
use_admin_sidebar: true,
});
needs.hooks.beforeEach(() => {
_locale = I18n.locale;
I18n.locale = "fr_FOO";
withPluginApi("1.24.0", (api) => {
api.addAdminSidebarSectionLink("root", {
name: "test_section_link",
label: "admin.plugins.title",
route: "adminPlugins.index",
icon: "cog",
});
});
});
needs.hooks.afterEach(() => {
I18n.locale = _locale;
});
test("valid links that are yet to be translated can be added to a section with the plugin API because of I18n fallback", async function (assert) {
await visit("/admin");
assert.ok(
exists(
".sidebar-section[data-section-name='admin-root'] .sidebar-section-link-wrapper[data-list-item-name=\"admin_additional_root_test_section_link\"]"
),
"link is appended to the root section"
);
});
}
);