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

Include built-in UI translation for regional docs #475

Merged
merged 13 commits into from
Aug 10, 2023

Conversation

yanthomasdev
Copy link
Member

What kind of changes does this PR include?

  • Changes to Starlight code

Description

This PR makes it possible for regional languages, e.g. pt-BR (Brazilian Portuguese) to use the built-in UI translation for the main language (e.g pt).

I've intentionally made that this would only work for the built-in translations, so you still can create an i18n/pt-BR.json file and override the built-in translation in case you want.

I've made a fairly simple case-insensitive function looking for the expected BCP-47 tag, we could throw an error if it isn't a valid string or make some more complex type checking for it, but I went simple to not overengineer for now.

TO-DO: docs! (let's see if we're going to change the API or something before)

Testing

The feature was tested with a copy branch of #469, with the following steps:

  • Add getting-started.mdx translation for pt-BR, the UI elements should be translated with the built-in UI i18n ✅
  • Add i18n/pt-BR.json, the UI elements should ignore the built-in UI and use the user-provided file instead ✅

@changeset-bot
Copy link

changeset-bot bot commented Aug 8, 2023

🦋 Changeset detected

Latest commit: 30015cc

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 Aug 8, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 30015cc
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/64d4c45b0adc5f0009694d4d
😎 Deploy Preview https://deploy-preview-475--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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 Aug 8, 2023
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Aug 8, 2023

size-limit report 📦

Path Size
/index.html 9.48 KB (0%)
/_astro/*.js 15.85 KB (0%)
/_astro/*.css 8.39 KB (0%)

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

This looks great @Yan-Thomas! Would you be up for writing a couple of tests for the cases you mentioned?

You could expand on translations.test.ts and translations-with-user-config.test.ts.

Let me know if you have questions about this. In the test with user config, the fake i18n dictionaries are created here:

vi.mock('astro:content', async () =>
(await import('../test-utils')).mockedAstroContent({
i18n: [['en', { 'page.editLink': 'Modify this doc!' }]],
})
);

Re: docs, I don’t know how much are needed. In general, I don’t think there’s new API here, just smoothing over use of regional specifiers.

Co-authored-by: Chris Swithinbank <[email protected]>
@yanthomasdev
Copy link
Member Author

Would you be up for writing a couple of tests for the cases you mentioned?

Sure, will take care of making some tests!

Re: docs, I don’t know how much are needed. In general, I don’t think there’s new API here, just smoothing over use of regional specifiers.

Yeah, I feel like probably just a note on the internationalization page would suffice! I am leaving for today but will add that as part of the PR tomorrow!

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Adding a note so I don’t forget — this needs a changeset. I’d suggest this is a patch as it fixes usage that is arguably expected.

@github-actions github-actions bot added the 📚 docs Documentation website changes label Aug 9, 2023
@yanthomasdev
Copy link
Member Author

Note that I haven't added tests to ensure the built-in regional variant gets precedence over the base language one, it seems we don't even have support for them yet, since the translations/index.ts implementation won't allow for dashes to match with the language.

// import statements can't have dashes
// and renaming to something like `zhTw`
// won't match with the user-provided lang.
import zh-TW from './zh-TW.json';

const { parse } = builtinI18nSchema();

export default Object.fromEntries(
	Object.entries({ cs, en, es, de, ja, pt, fr, it, nl, da, tr, ar, nb, zh }).map(([key, dict]) => [
		key,
		parse(dict),
	])
);

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Thanks @Yan-Thomas — this looks good! Suggesting two docs tweaks and then should be good to go 🙌

@delucis
Copy link
Member

delucis commented Aug 10, 2023

Note that I haven't added tests to ensure the built-in regional variant gets precedence over the base language one, it seems we don't even have support for them yet, since the translations/index.ts implementation won't allow for dashes to match with the language.

Oh we could still support it! We just couldn’t rely on the shorthand object notation:

import en from './en.json';
import zhTW from './zh-TW.json';

const { parse } = builtinI18nSchema();

export default Object.fromEntries(
	Object.entries({ en, 'zh-TW': zhTW }).map(([key, dict]) => [
		key,
		parse(dict),
	])
);

@delucis delucis added the 🌟 patch Change that triggers a patch release label Aug 10, 2023
yanthomasdev and others added 2 commits August 10, 2023 07:35
Co-authored-by: Chris Swithinbank <[email protected]>
Co-authored-by: Chris Swithinbank <[email protected]>
@github-actions github-actions bot removed the 📚 docs Documentation website changes label Aug 10, 2023
Co-Authored-By: Chris Swithinbank <[email protected]>
@github-actions github-actions bot added the 📚 docs Documentation website changes label Aug 10, 2023
@yanthomasdev
Copy link
Member Author

Oh we could still support it! We just couldn’t rely on the shorthand object notation:

Oh nice! Tested locally and worked perfectly, we're good to ship then! 🚀

@delucis delucis merged commit 06a205e into main Aug 10, 2023
@delucis delucis deleted the yan/lang-subtag-ui-support branch August 10, 2023 11:08
@astrobot-houston astrobot-houston mentioned this pull request Aug 10, 2023
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 📚 docs Documentation website changes 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants