Skip to content

The help text for [>>>] toggle always in English #23

Closed

Description

@jfine2358

See, for example, https://docs.python.org/fr/3/tutorial/introduction.html. It applies to all interactive code examples.

This is https://bugs.python.org/issue34452, transferred here, as a third party issue. It is related to
PEP 545 - Python Documentation Translations.

Here's what I've found. The purpose of python_docs_theme/static/copybutton.js is

Add a [>>>] button on the top-right corner of code samples to hide the >>> and ... prompts and the output and thus make the code copyable.

To do this it contains code

    var hide_text = 'Hide the prompts and output';
    var show_text = 'Show the prompts and output';

Somehow, this text needs to be internationalised.

Activity

  1. theacodes commented on Aug 21, 2018

    @theacodes
    Collaborator

    Tricky. Not sure how to internationalize text within a JS file.

    We could see about moving that text into the Jinja templates and use JS to show/hide it.

  2. jfine2358 commented on Aug 21, 2018

    @jfine2358
    Author

    Thank you for your prompt attention. It's not something I've done, either. I looked at view-source:https://docs.python.org/3/tutorial/introduction.html. It contains

    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>

    Perhaps best not to change this. Instead load an extra file, something like static/fr_text.js. that contains all the language variable text. This file should I think be part of the FRench translation of the docs (or generated from such). Once you've got that, use it in copybutton.js.
    There are libraries and tools associated with GNU gettext, that will help you with this. I hope this helps.

  3. theacodes commented on Aug 21, 2018

    @theacodes
    Collaborator

    I've honestly never done translation stuff, but I'm happy to give it a shot once I find some time.

    In the meantime, it would be awesome if anyone with specific experience here can help.

  4. terryjreedy commented on Aug 21, 2018

    @terryjreedy
    Member
  5. ccwang002 commented on Feb 16, 2019

    @ccwang002
    Contributor

    While is it hard to add translation directly into the javascript file, I was wondering if it is possible to add the strings in a HTML file, and use jQuery to fetch the translated string back.

    Sphinx has the ability to extract sentences from HTML. For example, we could add the strings into layout.html, on which all the HTML pages are based:

    <div style="display: none">
        <p id="copybutton-hide-text">{% trans %}Hide the prompts and output{% endtrans %}</p>
        <p id="copybutton-show-text">{% trans %}Show the prompts and output{% endtrans %}</p>
    </div>

    And in the copybutton.js, we try to fetch the translated string and provide the English fallback:

    // search for the translated prompt strings and fallback to use English if not found
    var hide_text_p = $('#copybutton-hide-text');
    var hide_text = hide_text_p.length > 0 ? hide_text_p.text() : "Hide the prompts and output";
    var show_text_p = $('#copybutton-show-text');
    var show_text = show_text_p.length > 0 ? show_text_p.text() : "Show the prompts and output";

    I have some questions about this approach:

    1. Where in the HTML template should we place the translated string? In block footer or header?
    2. Should we instead interpolate the javascript itself, say, treating copybutton.js like a Sphinx template so it directly supports i18n?
    3. Will there be a certain configuration where a user might accidentally see this div and get confused?

    I can submit a PR if people think the approach is reasonable.

  6. theacodes commented on Feb 17, 2019

    @theacodes
    Collaborator
  7. hugovk commented on Apr 6, 2025

    @hugovk
    Member

    Fixed by #225.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions