-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Changes from 10 commits
b88f32b
d596069
2066728
c7b6598
056baa1
f1c13cd
1167599
9ca8fe5
b909b08
78f5466
f8cfef0
1a82001
4fadb90
fcd539c
8ae7dfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import type {BlogContent, PluginOptions, BlogPaginated} from './types'; | ||
import type {TranslationFileContent, TranslationFiles} from '@docusaurus/types'; | ||
|
||
function translateListPage( | ||
blogListPaginated: BlogPaginated[], | ||
translations: TranslationFileContent, | ||
) { | ||
return blogListPaginated.map((page) => { | ||
const {items, metadata} = page; | ||
return { | ||
items, | ||
metadata: { | ||
...metadata, | ||
blogTitle: translations.title.message, | ||
blogDescription: translations.description.message, | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
export function getTranslationFiles(options: PluginOptions): TranslationFiles { | ||
return [ | ||
{ | ||
path: 'options', | ||
content: { | ||
title: { | ||
message: options.blogTitle, | ||
description: 'The title for the blog used in SEO', | ||
}, | ||
description: { | ||
message: options.blogDescription, | ||
description: 'The description for the blog used in SEO', | ||
}, | ||
'sidebar.title': { | ||
message: options.blogSidebarTitle, | ||
description: 'The label for the left sidebar', | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
export function translateContent( | ||
content: BlogContent, | ||
translationFiles: TranslationFiles, | ||
): BlogContent { | ||
const {content: translations} = translationFiles[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🤪 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how to do this gracefully since const [{content: optonsTranslations}] = translationFiles; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return { | ||
...content, | ||
blogSidebarTitle: translations['sidebar.title'].message, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth double-checking that if the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 But having some basic tests for 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();
});
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
blogListPaginated: translateListPage( | ||
content.blogListPaginated, | ||
translations, | ||
), | ||
}; | ||
} |
There was a problem hiding this comment.
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:
Not sure it make sense though so I'll let you figure out, current implementation is good enough for me
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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