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

Allow enabling translations by setting translations_list in config #302

Merged
merged 8 commits into from
May 9, 2023

Conversation

Eric-Arellano
Copy link
Collaborator

Part of #262. Before, to add translation support, you would have to copy and paste versionutils.py. Now, you ~only need to define translations_list in conf.py and qiskit_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 from example_docs/ and instead sets its values in conf.py. Translations still work:

Screenshot 2023-05-05 at 1 13 59 PM

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:

  • Removing the translations config option and HTML context. It wasn't being used by anything.
  • Removing the current_translation HTML context. It wasn't being used by anything.


* 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`.
Copy link
Collaborator Author

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",
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 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.

Copy link
Collaborator

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?

Copy link
Collaborator Author

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:

# 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")
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 need to activate jQuery for Furo.

qiskit_sphinx_theme/translations.py Show resolved Hide resolved
example_docs/docs/conf.py Show resolved Hide resolved
example_docs/docs/conf.py Outdated Show resolved Hide resolved


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
Copy link
Member

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.

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 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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good!

@Eric-Arellano
Copy link
Collaborator Author

This PR intentionally does not refactor any of the translations.py code.

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.

example_docs/docs/conf.py Show resolved Hide resolved
@@ -41,7 +40,7 @@
'jupyter_sphinx',
'sphinx_design',
"nbsphinx",
"sphinxcontrib.jquery",
Copy link
Collaborator

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?

example_docs/docs/conf.py Outdated Show resolved Hide resolved

Once the Translations team is ready, then update your `conf.py`:

* Ensure that `qiskit_sphinx_theme` is in the `extensions` setting.
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

example_docs/docs/conf.py Outdated Show resolved Hide resolved


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
Copy link
Collaborator

Choose a reason for hiding this comment

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

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 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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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

Copy link
Collaborator

@javabster javabster left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants