-
Notifications
You must be signed in to change notification settings - Fork 30
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
Allow enabling translations by setting translations_list
in config
#302
Conversation
|
||
* Ensure that `qiskit_sphinx_theme` is in the `extensions` setting. | ||
* Set the option `translations_list` to a list of pairs of the locale code and the language name, e.g. `[..., ("de_DE", "German")]`. | ||
* Set the option `content_prefix` to your project's URL prefix, like `ecosystem/finance`. |
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 a couple of things I don't like about this option, including the name not being clear what it is: #303.
I plan to make some improvements as part of Sphinx Theme 0.12. But I want to do that in a follow up PR to make the code review easier.
For now, this simply copies the name and logic we had in all of our Terra and Ecosystem projects.
As long as we make any changes before 0.12 is released, it is safe for us to make breaking changes like renaming this.
@@ -41,7 +40,7 @@ | |||
'jupyter_sphinx', | |||
'sphinx_design', | |||
"nbsphinx", | |||
"sphinxcontrib.jquery", |
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 figured out now why jQuery wasn't being activated automatically by qiskit-sphinx-theme
like I hoped for in #277. It's not enough to set html_theme = qiskit_sphinx_theme
. We need to also add it to extensions
.
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.
so does this mean we don't need sphinxcontrib.jquery
anymore?
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.
If you set qiskit_sphinx_theme
in extensions
, then it is redundant to also include sphinxcontrib.jquery
. That is because we have this code that tells our own extension to also activate jQuery:
qiskit_sphinx_theme/qiskit_sphinx_theme/__init__.py
Lines 35 to 37 in 086f163
# Sphinx 6 stopped including jQuery by default. Our Pytorch theme depend on jQuery, | |
# so install it for our users automatically. | |
app.setup_extension("sphinxcontrib.jquery") |
else: | ||
# Sphinx 6 stopped including jQuery by default. Our Pytorch theme depend on jQuery, | ||
# so activate it for our users automatically. | ||
app.setup_extension("sphinxcontrib.jquery") |
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 need to activate jQuery for Furo.
|
||
|
||
def _extend_html_context(app, config): | ||
context = config.html_context | ||
context['translations'] = config.translations | ||
context['translations_list'] = translations_list | ||
context['current_translation'] = _get_current_translation(config) or config.language |
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 you know if context['current_translation']
is now used else where?
It was added, IIRC, for the page to remember the language the user was on, when they go from one page to another.
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 couldn't find anywhere it's used. html_context
exists so that HTML templates can reference values that are inserted by Python.
Our templates are using language_label
instead. If you look at the Python implementation, language_label
and current_translation
are computed almost the same way.
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 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.
It is indeed set by Qiskit and all the ecosystem projects. But it is never consumed by anything. We're setting a value for no benefit, which is confusing.
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.
ah ok, let's remove it for now but make a mental note if we find something breaks for someones project we might need to add it back
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.
Sounds good!
I opened #305 to track adding unit tests to this file in a follow-up PR. I don't do that in this PR because we don't yet have the infrastructure to run Python unit tests and I wanted to keep the review simple. |
@@ -41,7 +40,7 @@ | |||
'jupyter_sphinx', | |||
'sphinx_design', | |||
"nbsphinx", | |||
"sphinxcontrib.jquery", |
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.
so does this mean we don't need sphinxcontrib.jquery
anymore?
|
||
Once the Translations team is ready, then update your `conf.py`: | ||
|
||
* Ensure that `qiskit_sphinx_theme` is in the `extensions` setting. |
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.
is this step only necessary for enabling translations? Or should it be in extensions anyway?
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.
Sort of. If you don't add qiskit_sphinx_theme
to extensions, then you need to add sphinxcontrib.jquery
to the extensions
list because Pytorch requires jQuery. At least until we switch to Furo.
So, I think we can simplify things to say that you should always add qiskit_sphinx_theme
to the extensions list.
|
||
|
||
def _extend_html_context(app, config): | ||
context = config.html_context | ||
context['translations'] = config.translations | ||
context['translations_list'] = translations_list | ||
context['current_translation'] = _get_current_translation(config) or config.language |
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 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.
have you cross checked this file with https://github.com/Qiskit/qiskit-metapackage/blob/master/docs/versionutils.py? I think there are a couple of functions used in qiskit that aren't here, e.g. _get_language_label
and _get_translation_url
. I'm not really sure what those functions do but it might be important if it's used in qiskit
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.
Those are here too :) This is based on that file, and only remove the Git/Previous Versions code. Along with what I mention in the PR description about translations
config value and current_translation
.
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.
Oh! I didn't expand the file view in the github diff 😅 my bad, I see them now
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.
LGTM 🚀
Part of #262. Before, to add translation support, you would have to copy and paste
versionutils.py
. Now, you ~only need to definetranslations_list
inconf.py
andqiskit_sphinx_theme
will set up the rest for you.That duplication of
versionutils.py
was annoying to copy and also made it too easy to fall out of sync, resulting in #272.To prove this works, this PR removes
language_utils.py
fromexample_docs/
and instead sets its values inconf.py
. Translations still work:This PR intentionally does not refactor any of the
translations.py
code. It only copies and pastes what we had from all our projects, to make it easier to review. The only divergences are:translations
config option and HTML context. It wasn't being used by anything.current_translation
HTML context. It wasn't being used by anything.