-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b88f32b
Translate versions dropdown label
Josh-Cena d596069
Make blog options tranlatable
Josh-Cena 2066728
Fix names
Josh-Cena c7b6598
Rename functions
Josh-Cena 056baa1
Rename translations path
Josh-Cena f1c13cd
Update docs
Josh-Cena 1167599
Merge branch 'master' into blog-sidebar-translate
Josh-Cena 9ca8fe5
Update Chinese translations
Josh-Cena b909b08
Remove space
Josh-Cena 78f5466
Update docs
Josh-Cena f8cfef0
Add jest test
Josh-Cena 1a82001
Fix copyright typing
Josh-Cena 4fadb90
Update test
Josh-Cena fcd539c
Update options
Josh-Cena 8ae7dfa
Fix test
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...ages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/translations.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getContentTranslationFiles should return translation files matching snapshot 1`] = ` | ||
Array [ | ||
Object { | ||
"content": Object { | ||
"description": Object { | ||
"description": "The description for the blog used in SEO", | ||
"message": "Someone's random blog", | ||
}, | ||
"sidebar.title": Object { | ||
"description": "The label for the left sidebar", | ||
"message": "All my posts", | ||
}, | ||
"title": Object { | ||
"description": "The title for the blog used in SEO", | ||
"message": "My blog", | ||
}, | ||
}, | ||
"path": "options", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`translateContent should return translated loaded content matching snapshot 1`] = ` | ||
Object { | ||
"blogListPaginated": Array [ | ||
Object { | ||
"items": Array [ | ||
"hello", | ||
], | ||
"metadata": Object { | ||
"blogDescription": "Someone's random blog (translated)", | ||
"blogTitle": "My blog (translated)", | ||
"nextPage": null, | ||
"page": 1, | ||
"permalink": "/", | ||
"postsPerPage": 10, | ||
"previousPage": null, | ||
"totalCount": 1, | ||
"totalPages": 1, | ||
}, | ||
}, | ||
], | ||
"blogPosts": Array [ | ||
Object { | ||
"id": "hello", | ||
"metadata": Object { | ||
"date": 2021-07-19T00:00:00.000Z, | ||
"description": "/blog/2021/06/19/hello", | ||
"formattedDate": "June 19, 2021", | ||
"permalink": "/blog/2021/06/19/hello", | ||
"source": "/blog/2021/06/19/hello", | ||
"tags": Array [], | ||
"title": "Hello", | ||
"truncated": true, | ||
}, | ||
}, | ||
], | ||
"blogSidebarTitle": "All my posts (translated)", | ||
"blogTags": Object {}, | ||
"blogTagsListPath": "/tags", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/docusaurus-plugin-content-blog/src/__tests__/translations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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 {BlogPost, BlogContent, PluginOptions} from '../types'; | ||
import {getTranslationFiles, translateContent} from '../translations'; | ||
import {DEFAULT_OPTIONS} from '../pluginOptionSchema'; | ||
import {updateTranslationFileMessages} from '@docusaurus/utils'; | ||
|
||
const sampleBlogOptions: PluginOptions = { | ||
...DEFAULT_OPTIONS, | ||
blogSidebarTitle: 'All my posts', | ||
blogTitle: 'My blog', | ||
blogDescription: "Someone's random blog", | ||
}; | ||
|
||
const sampleBlogPosts: BlogPost[] = [ | ||
{ | ||
id: 'hello', | ||
metadata: { | ||
permalink: '/blog/2021/06/19/hello', | ||
source: '/blog/2021/06/19/hello', | ||
description: '/blog/2021/06/19/hello', | ||
date: new Date(2021, 6, 19), | ||
formattedDate: 'June 19, 2021', | ||
tags: [], | ||
title: 'Hello', | ||
truncated: true, | ||
}, | ||
}, | ||
]; | ||
|
||
const sampleBlogContent: BlogContent = { | ||
blogSidebarTitle: sampleBlogOptions.blogSidebarTitle, | ||
blogListPaginated: [ | ||
{ | ||
items: ['hello'], | ||
metadata: { | ||
permalink: '/', | ||
page: 1, | ||
postsPerPage: 10, | ||
totalPages: 1, | ||
totalCount: 1, | ||
previousPage: null, | ||
nextPage: null, | ||
blogTitle: sampleBlogOptions.blogTitle, | ||
blogDescription: sampleBlogOptions.blogDescription, | ||
}, | ||
}, | ||
], | ||
blogPosts: sampleBlogPosts, | ||
blogTags: {}, | ||
blogTagsListPath: '/tags', | ||
}; | ||
|
||
function getSampleTranslationFiles() { | ||
return getTranslationFiles(sampleBlogOptions); | ||
} | ||
function getSampleTranslationFilesTranslated() { | ||
const translationFiles = getSampleTranslationFiles(); | ||
return translationFiles.map((translationFile) => | ||
updateTranslationFileMessages( | ||
translationFile, | ||
(message) => `${message} (translated)`, | ||
), | ||
); | ||
} | ||
|
||
describe('getContentTranslationFiles', () => { | ||
test('should return translation files matching snapshot', async () => { | ||
expect(getSampleTranslationFiles()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('translateContent', () => { | ||
test('should not translate anything if translation files are untranslated', () => { | ||
const translationFiles = getSampleTranslationFiles(); | ||
expect(translateContent(sampleBlogContent, translationFiles)).toEqual( | ||
sampleBlogContent, | ||
); | ||
}); | ||
|
||
test('should return translated loaded content matching snapshot', () => { | ||
const translationFiles = getSampleTranslationFilesTranslated(); | ||
expect( | ||
translateContent(sampleBlogContent, translationFiles), | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/docusaurus-plugin-content-blog/src/translations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: optonsTranslations}] = translationFiles; | ||
return { | ||
...content, | ||
blogSidebarTitle: optonsTranslations['sidebar.title'].message, | ||
blogListPaginated: translateListPage( | ||
content.blogListPaginated, | ||
optonsTranslations, | ||
), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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