-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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: readMetadata & env.localization #907
refactor: readMetadata & env.localization #907
Conversation
046070b
to
20664ff
Compare
Deploy preview for docusaurus-preview ready! Built with commit 046070b |
Deploy preview for docusaurus-preview ready! Built with commit d5e5aee |
20664ff
to
43914b7
Compare
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.
I like the idea of removing translations.js
and replacing it with Translation.t(language, key, category)
interface but unfortunately removing translation.js will be causing breaking changes to user and we want to avoid that for now because v2 will have breaking changes as well 😄
Reasonml for example use translation
https://github.com/reasonml/reasonml.github.io/blob/42e9a33610244fbf84838cbd29c02d8f111cf11e/website/pages/en/index.js#L9
const translation = require('../../server/translation.js');
Can you revert it back and I'll continue to review ?
@endiliey ok. I can restore that file, but probably it would be better to add a deprecation warning in that file? If the user will access the data through the interface it will allow Docusaurus developers to safely change the way that data handled. Regarding
This will make internals of
|
Hmm.. I like the idea of that but I think I need extra opinion on this. Cc @yangshun @JoelMarcey |
Yeah, we would definitely have to start with a deprecation and give a few releases and warnings in the release notes about it since if any project that uses translations would require the current file. But I think that is ok. |
@endiliey the When some one will try to use I have merged with current master and added support for keys as array, that was introduced in #917, e.g.: const title = translation.t(['docs', id, 'title']) || defaultTitle; |
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.
This is a huge change 😲
Overall it looks good to me, but definitely we'll have to keep on reviewing & this need more than just my review.
Another thing is that this will definitely clash/conflicts with #892. Whichever got merged first 😬
lib/server/generate.js
Outdated
@@ -386,6 +389,8 @@ async function execute() { | |||
const pageID = path.basename(normalizedFile, '.html'); | |||
const parts = normalizedFile.split('pages'); | |||
const targetFile = join(buildDir, parts[1]); | |||
env.translation.setLanguage('en'); |
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.
There are some codes in this file that use translate.setLanguage(language);
and sometimes we use env.translation.setLanguage()
although its pointing to the same logic. Shouldn't we go for consistency ?
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.
You are right. I'll fix it.
Can I safely remove setLanguage from server/translate.js
? Or should I add deprecation warning there?
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.
https://docusaurus.io/docs/en/translation#enabling-translations-on-pages
It seems safe to remove
lib/server/env/Translation.js
Outdated
.replace(new RegExp('\\\\', 'g'), '\\'); | ||
} | ||
|
||
resolveMessage(translations, keyParts) { |
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.
I think this is too generic to be put inside translation.
resolveMessage(translations, keyParts) {
return keyParts.reduce((obj, key) => obj && obj[key], translations);
}
We should preserve the safe-getter idx
function in core/utils.js
to access deeply nested object and this class can instead import that utility function
const blogPages = {}; | ||
const perPage = 10; | ||
|
||
env.translation.setLanguage('en'); |
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.
Do we need this ? There is no translation in blog
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.
But there are translations in <Site>
component. I think it is safer to explicitly set language to default en
, because it may happen, that current translation language will not be 'en' due to some doc page rendered previously using other language.
I have also finished with #868 and #827, which are improvements of sidebar too. I haven't submitted PR, because I'm coding on base of current PR. I can wait till #892 merged and then resolve all the conflicts or if my changes are too big, I can drop them and try to resolve 868 and 827 without any refactoring. |
I am more inclined to merge this before #892 actually. We are going for rewrite in v2 so I favor this refactoring and test more than feature. However I am not confident to merge such big change alone. Current architecture has a lot of circular require(), for example the env & siteConfig.js. Ideally we want v2 to have unidirectional data flow. |
a5d41ae
to
ab196d2
Compare
cbecd77
to
b4c564e
Compare
812b2f1
to
7342942
Compare
* Add method to access localized strings * Add deprecation warning for old api usage * Implement translation fallbacks * Ensure setLanguage called before any rendering
7342942
to
d5e5aee
Compare
Hey @SleepWalker, Sorry for another late response. First of all, you might want to see https://docusaurus.io/blog/2018/09/11/Towards-Docusaurus-2
I personally prefer not to merge this since this part of docs metadata codes has been re-written & changed on v2 into pure functions without CWD/ global require anymore (no more side effects, yay !!) with > 90% test coverage. However, will you be interested in helping for v2 instead? I'll notify you when we've moved part of the work on this repository's v2 branch. I'm sure you'll be a great help 😃 |
Hi, @endiliey yep. I've read that post and it would be interesting to help with the v2 :) I'm closing this PR |
Hi @SleepWalker. I think this can now be done in v2. You might want to checkout v2 folder (although its still wip) Feel free to ping me on GitHub or discord |
Motivation
This PR appeared during during my work on #868. It contains no new features or logic changes. I've rearranged the code to decouple it from the environment and be able to cover it with tests.
The docs-related logic from
server/readMetadata
was moved into a separate classDocsMetadata
and covered with test.I've also removed translations.js and replaced it with
Translation.t(language, key, category)
interface and movedgetLanguage
implementation from utils toTranslation
. TheTranslation
class was moved fromserver/env
into a separate fileHave you read the Contributing Guidelines on pull requests?
Yes
Test Plan
The best way is to compare
metadata.js
before and after my refactoring applied (e.g. with jsondiff.com). They should be the same. The problem is that I can not test with translations applied. I've compared only themetadata.js
files I can get locally.Another one option is to compare current gh-pages version and netlify version using different locales and doc versions.