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

feat: make blog config options and navbar versions dropdown label translatable #5371

Merged
merged 15 commits into from Aug 20, 2021

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Aug 17, 2021

Motivation

Resolve (partially) #4542. This makes the "Versions" label and all the blog options (blogSidebarTitle, blogTitle, blogDescription) translatable.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

yarn start --locale zh-Hans

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 17, 2021
@Josh-Cena Josh-Cena marked this pull request as draft August 17, 2021 14:29
@netlify
Copy link

netlify bot commented Aug 17, 2021

✔️ [V2]
Built without sensitive environment variables

🔨 Explore the source changes: 8ae7dfa

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/611f9ccd3a53b50008d87096

😎 Browse the preview: https://deploy-preview-5371--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Aug 17, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟢 Performance 97
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5371--docusaurus-2.netlify.app/

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@Josh-Cena
Copy link
Collaborator Author

@slorber Translating the options seems less straightforward than I thought because the translateLoadedContent only translates the BlogContent which does not include the sidebar title / all the other plugin options. Do you think including those translated fields in the BlogContent interface is a good solution?

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@Josh-Cena Josh-Cena marked this pull request as ready for review August 18, 2021 00:34
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@slorber
Copy link
Collaborator

slorber commented Aug 19, 2021

@slorber Translating the options seems less straightforward than I thought because the translateLoadedContent only translates the BlogContent which does not include the sidebar title / all the other plugin options. Do you think including those translated fields in the BlogContent interface is a good solution?

Yes, any option that need to be translated should be added to the loadedContent result

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, that looks good 👍

Just let some comments in case you want to take a look, but I'm ok to merge this.

content: BlogContent,
translationFiles: TranslationFiles,
): BlogContent {
const {content: translations} = translationFiles[0];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to have code to get the options file, this won't break if we add other translation files later? Not a big deal for now we may not have many translation files for the blog 🤪

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't know how to do this gracefully since plugin-docs and theme-classic handle this in pretty different ways. I hope destructuring is better:

const [{content: optonsTranslations}] = translationFiles;

Copy link
Collaborator

Choose a reason for hiding this comment

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

let's keep this for now 👍 we'll see later if there's a need to improve it

const {content: translations} = translationFiles[0];
return {
...content,
blogSidebarTitle: translations['sidebar.title'].message,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's worth double-checking that if the options.json file is empty or incomplete, it doesn't crash the translation process.
I think it's safe because translated content is merged into untranslated content to make sure all keys are always available, but worth double-checking that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a jest test, not sure if it's representative though (not very confident with unit tests)

Copy link
Collaborator

Choose a reason for hiding this comment

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

have you pushed? I don't see any change?

I don't think a Jest test can test it anyway, it's more a contract of how core is supposed to call the lifecycle (ie, provide exactly ALL the files with ALL the keys that getTranslationFiles have). I checked code and it should be the case, so was just asking for a local test ;)

But having some basic tests for translations.ts can be useful. Something simple, similar to what we have for docs:

function getSampleTranslationFiles() {
  return getLoadedContentTranslationFiles(SampleLoadedContent);
}
function getSampleTranslationFilesTranslated() {
  const translationFiles = getSampleTranslationFiles();
  return translationFiles.map((translationFile) =>
    updateTranslationFileMessages(
      translationFile,
      (message) => `${message} (translated)`,
    ),
  );
}

describe('getLoadedContentTranslationFiles', () => {
  test('should return translation files matching snapshot', async () => {
    expect(getSampleTranslationFiles()).toMatchSnapshot();
  });
});

describe('translateLoadedContent', () => {
  test('should not translate anything if translation files are untranslated', () => {
    const translationFiles = getSampleTranslationFiles();
    expect(
      translateLoadedContent(SampleLoadedContent, translationFiles),
    ).toEqual(SampleLoadedContent);
  });

  test('should return translated loaded content matching snapshot', () => {
    const translationFiles = getSampleTranslationFilesTranslated();
    expect(
      translateLoadedContent(SampleLoadedContent, translationFiles),
    ).toMatchSnapshot();
  });
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ugh, forgot to push😅 My tests are pretty similar to what you have here, indeed

metadata: {
...metadata,
blogTitle: translations.title.message,
blogDescription: translations.description.message,
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of translating each page title this way, I'm wondering if we shouldn't create a bundle with all those generic blog infos, so that multiple pages could use the same bundle?

Like:

const blogMetadataProp = await createData({title,description,sidebarTitle})

addRoute({
  modules: {
    blogMetadata: blogMetadataProp
  }
});

Not sure it make sense though so I'll let you figure out, current implementation is good enough for me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No strong feelings, I also considered this, but the current implementation equally makes sense. I was thinking if it is possible to have a translateOptions lifecycle?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep it this way, I may refactor this plugin a bit in the near future because it's a bit messy.

About translateOptions, I think adding options to content is fine and we don't need another new lifecycle, but let's see, we may add this later if this becomes a common need for ourselves or other plugin authors

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@slorber
Copy link
Collaborator

slorber commented Aug 20, 2021

Thanks LGTM 👍

About feed options, I'd rather use feedOptions = null to disable rather than feedOptions.type = null , but this would be a breaking change, not sure it's worth it now

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Aug 20, 2021
@slorber slorber merged commit 8e1cde1 into facebook:main Aug 20, 2021
@Josh-Cena Josh-Cena deleted the blog-sidebar-translate branch August 20, 2021 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants