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

docs: refactor languages list #1012

Merged
merged 8 commits into from
Nov 17, 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
41 changes: 41 additions & 0 deletions docs/src/components/languages-list.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
import { getEntry } from 'astro:content';
import translations from '../../../packages/starlight/translations';
import { slugToLocaleData } from '../../../packages/starlight/utils/slugs';

interface Props {
startsSentence?: boolean;
}

// The current page's slug.
const slug = Astro.url.pathname.replace(/^\//, '').replace(/\/$/, '');
// The docs entry for the current page, or `undefined` if the page is using fallback content.
const entry = await getEntry('docs', slug);
// The BCP-47 tag for the current page or fallback content's language.
const pageLang = entry ? slugToLocaleData(slug).lang : 'en';
// The BCP-47 tags for all supported languages in Starlight.
const supportedLangs = Object.keys(translations);
// An i18n helper that returns the language name for a given BCP-47 tag configured for the current page's language.
const langNames = new Intl.DisplayNames([pageLang], { type: 'language' });

// A list of the language names for all supported languages sorted alphabetically.
const supportedLangNames = supportedLangs
.map((supportedLang) => {
const langName = langNames.of(supportedLang);
if (!langName) throw new Error(`Failed to get the language name for '${supportedLang}'.`);
return langName;
})
.sort((a, b) => a.localeCompare(b, pageLang));

// The list of language names for all supported languages formatted according to the current page's language.
const langList = new Intl.ListFormat(pageLang, {
style: 'long',
type: 'conjunction',
}).format(supportedLangNames);
---

{
Astro.props.startsSentence
? langList[0]?.toLocaleUpperCase(pageLang) + langList.slice(1)
: langList
}
4 changes: 3 additions & 1 deletion docs/src/content/docs/guides/i18n.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ If a translation is not yet available for a language, Starlight will show reader

## Translate Starlight's UI

import LanguagesList from '../../../components/languages-list.astro';

In addition to hosting translated content files, Starlight allows you to translate the default UI strings (e.g. the "On this page" heading in the table of contents) so that your readers can experience your site entirely in the selected language.

English, Czech, French, German, Italian, Japanese, Portuguese, Dutch, Danish, Spanish, Turkish, Arabic, Norwegian, Farsi, Hebrew, Simplified Chinese, Korean, Indonesian, Romanian, Russian, Swedish, Ukrainian, Vietnamese, and Galician translated UI strings are provided out of the box, and we welcome [contributions to add more default languages](https://github.com/withastro/starlight/blob/main/CONTRIBUTING.md).
<LanguagesList startsSentence /> translated UI strings are provided out of the box, and we welcome [contributions to add more default languages](https://github.com/withastro/starlight/blob/main/CONTRIBUTING.md).

You can provide translations for additional languages you support — or override our default labels — via the `i18n` data collection.

Expand Down