Files
docmd-io--docmd/packages/ui/templates/partials/language-switcher.ejs
T
wehub-resource-sync 6db8fca185
docmd CI verification / verify (push) Failing after 0s
chore: import upstream snapshot with attribution
2026-07-13 12:31:55 +08:00

84 lines
4.2 KiB
Plaintext

<%# ---------------------------------------------------------------
# docmd : the zero-config documentation engine.
# @website https://docmd.io
# [docmd-source] - Please do not remove this header.
# ---------------------------------------------------------------
%>
<% if (allLocales && allLocales.length > 1) {
const current = activeLocale || allLocales.find(l => l.id === defaultLocale);
const isCompact = typeof compact !== 'undefined' && compact;
// Compute current page path relative to locale root
// Strip the active locale prefix if present (outputPrefix bakes it into navPath now)
let pagePath = (typeof currentPagePath !== 'undefined' && currentPagePath) ? currentPagePath.replace(/^\//, '') : '';
const activeLocaleId = activeLocale ? activeLocale.id : null;
if (activeLocaleId && activeLocaleId !== defaultLocale && pagePath.startsWith(activeLocaleId + '/')) {
pagePath = pagePath.slice(activeLocaleId.length + 1);
}
// Get the active version prefix if versioning is active
const activeVersion = config._activeVersion;
const currentVersionId = config.versions && config.versions.current;
const isOldVersion = activeVersion && currentVersionId && activeVersion.id !== currentVersionId;
const versionPrefix = isOldVersion ? activeVersion.id + '/' : '';
// Strip version prefix from pagePath if it's already baked in (avoids duplication)
if (versionPrefix && pagePath.startsWith(versionPrefix)) {
pagePath = pagePath.slice(versionPrefix.length);
}
// Determine which locales are available for this version
// Old versions without locale subdirectories only support the default locale
const versionHasI18n = !isOldVersion || (typeof config._versionHasI18n !== 'undefined' ? config._versionHasI18n : !isOldVersion);
%>
<div class="docmd-language-switcher<%= isCompact ? ' language-compact' : '' %>">
<button class="language-switcher-toggle" aria-expanded="false" aria-label="<%= typeof t === 'function' ? t('selectLanguage') : 'Select Language' %>">
<%- renderIcon('globe', { class: 'language-icon' }) %>
<% if (!isCompact) { %>
<span class="language-label"><%= current ? current.label : (typeof t === 'function' ? t('language') : 'Language') %></span>
<%- renderIcon('chevron-down', { class: 'language-chevron' }) %>
<% } %>
</button>
<ul class="language-switcher-menu">
<% allLocales.forEach(loc => {
const isCurrentActive = current && loc.id === current.id;
const isLocDefault = loc.id === defaultLocale;
// Non-default locales are disabled when:
// 1. This version doesn't have i18n support, OR
// 2. The locale's source directory doesn't exist (no content was generated)
const builtLocales = config._builtLocales;
const isLocaleBuilt = !builtLocales || builtLocales.has ? builtLocales?.has(loc.id) : (Array.isArray(builtLocales) ? builtLocales.includes(loc.id) : true);
const isDisabled = !isLocDefault && (!versionHasI18n || !isLocaleBuilt);
// Build locale-aware URL preserving version and page path
// Use centralised URL builder — single source of truth
const locPrefix = isLocDefault ? '' : loc.id + '/';
const absoluteHref = isDisabled
? '#'
: ((typeof buildAbsoluteUrl === 'function')
? buildAbsoluteUrl(config.base || '/', locPrefix, versionPrefix, pagePath)
: ((config.base || '/').replace(/\/?$/, '/') + locPrefix + versionPrefix + pagePath));
%>
<li>
<a href="<%= absoluteHref %>"
class="language-switcher-item <%= isCurrentActive ? 'active' : '' %><%= isDisabled ? ' disabled' : '' %>"
data-spa-ignore
data-locale-id="<%= loc.id %>"
lang="<%= loc.id %>"
dir="<%= loc.dir || 'ltr' %>"
<% if (isDisabled) { %>aria-disabled="true" title="<%= typeof t === 'function' ? t('translationUnavailable') : 'Translation not available for this version' %>"<% } %>>
<%= loc.label %>
<% if (isCurrentActive) { %>
<%- renderIcon('check', { class: 'language-check' }) %>
<% } %>
<% if (isDisabled) { %>
<span class="language-unavailable-badge"><%= typeof t === 'function' ? t('unavailable') : 'N/A' %></span>
<% } %>
</a>
</li>
<% }) %>
</ul>
</div>
<% } %>