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

Refactor translation system to be reusable in non-Astro code #1003

Merged
merged 10 commits into from
Nov 1, 2023

Conversation

delucis
Copy link
Member

@delucis delucis commented Oct 28, 2023

What kind of changes does this PR include?

  • Changes to Starlight code

Description

  • Refactors the existing Starlight translations system so it can be instantiated from either getCollection() calls (for modules loaded by Astro) or file system calls (for modules running before Astro starts).
  • The file-system version makes it possible for work like Add Expressive Code to Starlight #742 to re-use our existing logic.
  • It will also help us unblock Support translating default aside titles #517.
  • This PR instantiates the file-system version in the main Starlight integration. In follow-up PRs we’d be able to pass useTranslations to code that needs them (i.e. the Expressive Code integration and the asides plugin)

To-do

  • Write some tests for the new code

Background

This is needed because our current i18n system depends on two bits of code that are only available after Astro starts: getCollection() imported from the astro:content virtual module is used to load the i18n data collection and get any user-configured JSON dictionaries of strings and Starlight user config imported from virtual:starlight/user-config.

These dependencies prevented pre-Astro code like remark/rehype plugins or other integration code from accessing our translation system. So far, when trying to support i18n in those cases, we either got stuck or ended up making a copy of our i18n logic that didn’t cover all cases. This refactor helps make sure we share as much logic as possible between the code that runs before and after Astro starts.

@changeset-bot
Copy link

changeset-bot bot commented Oct 28, 2023

🦋 Changeset detected

Latest commit: bd048d1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Oct 28, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit bd048d1
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/6542cc39d23e3f0008e3f5ff
😎 Deploy Preview https://deploy-preview-1003--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Oct 28, 2023
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Oct 28, 2023

size-limit report 📦

Path Size
/index.html 5.13 KB (0%)
/_astro/*.js 19.27 KB (0%)
/_astro/*.css 9.7 KB (0%)

@@ -26,6 +27,7 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
name: '@astrojs/starlight',
hooks: {
'astro:config:setup': ({ config, injectRoute, updateConfig }) => {
const useTranslations = createTranslationSystemFromFs(userConfig, config);
Copy link
Member Author

Choose a reason for hiding this comment

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

This variable is not in use yet, but would be passed into anything that needs it, such as the Starlight integration added in #742.

Comment on lines +51 to +63
/**
* Get the BCP-47 language tag for the given locale.
* @param locale Locale string or `undefined` for the root locale.
*/
function localeToLang(
locale: string | undefined,
locales: StarlightConfig['locales'],
defaultLocale: StarlightConfig['defaultLocale']
): string {
const lang = locale ? locales?.[locale]?.lang : locales?.root?.lang;
const defaultLang = defaultLocale?.lang || defaultLocale?.locale;
return lang || defaultLang || 'en';
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I’ve duplicated this function for now from slugs.ts:

/**
* Get the BCP-47 language tag for the given locale.
* @param locale Locale string or `undefined` for the root locale.
*/
export function localeToLang(locale: string | undefined): string {
const lang = locale ? config.locales?.[locale]?.lang : config.locales?.root?.lang;
const defaultLang = config.defaultLocale?.lang || config.defaultLocale?.locale;
return lang || defaultLang || 'en';
}

We could refactor in a similar spirit to make this version the base and then have a module that exports the easier to use methods that depend on virtual:starlight/user-config.

Copy link
Member Author

Choose a reason for hiding this comment

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

Our base useTranslations() method is unchanged but is refactored to use the new createTranslationSystem() factory, passing in files loaded with getCollection().

packages/starlight/utils/translations-fs.ts Outdated Show resolved Hide resolved
@delucis delucis added the 🌟 patch Change that triggers a patch release label Nov 1, 2023
@delucis delucis merged commit f1fdb50 into main Nov 1, 2023
9 checks passed
@delucis delucis deleted the chris/i18n-refactor-2 branch November 1, 2023 22:31
@astrobot-houston astrobot-houston mentioned this pull request Nov 1, 2023
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Nov 2, 2023
* main: (22 commits)
  fix(docs-i18n-tracker): update `translations` import (withastro#1025)
  [ci] format
  i18n(zh-cn): Update css-and-tailwind.mdx (withastro#1018)
  [ci] format
  i18n(zh-cn): Update authoring-content.md (withastro#1016)
  i18n(ko-KR): update `configuration.mdx` (withastro#1015)
  i18n(ko-KR): update `sidebar.mdx` (withastro#1014)
  i18n(ko-KR): update `i18n.mdx` (withastro#1013)
  [ci] format
  i18n(ko-KR): update `frontmatter.md` (withastro#1017)
  [ci] format
  i18n(pt-BR): Update `css-and-tailwind.mdx`, `authoring-content.md` and `overrides.md` (withastro#1009)
  [ci] format
  [ci] release (withastro#996)
  Fix Prettier-compatibility of i18n test fixture
  Refactor translation system to be reusable in non-Astro code (withastro#1003)
  Add social icons to mobile menu footer (withastro#988)
  [ci] format
  Add Galician language support (withastro#1004)
  feat: add support for light / dark hero images (withastro#280)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants