python-docs-theme icon indicating copy to clipboard operation
python-docs-theme copied to clipboard

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

Open jfine2358 opened this issue 7 years ago • 7 comments

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.

jfine2358 avatar Aug 21 '18 19:08 jfine2358

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.

theacodes avatar Aug 21 '18 19:08 theacodes

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.

jfine2358 avatar Aug 21 '18 20:08 jfine2358

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.

theacodes avatar Aug 21 '18 20:08 theacodes

This might be helpful. https://docs.djangoproject.com/en/2.1/topics/i18n/translation/#internationalization-in-javascript-code

jfine2358 avatar Aug 21 '18 20:08 jfine2358

I would talk to the people doing doc translations: https://devguide.python.org/experts/#documentation-translations. Discussion on https://mail.python.org/mailman/listinfo/doc-sig

terryjreedy avatar Aug 21 '18 20:08 terryjreedy

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.

ccwang002 avatar Feb 16 '19 21:02 ccwang002

I think it's a pretty reasonable approach.

On Sat, Feb 16, 2019, 1:12 PM Liang-Bo Wang [email protected] wrote:

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:

{% trans %}Hide the prompts and output{% endtrans %}

{% trans %}Show the prompts and output{% endtrans %}

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

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/python/python-docs-theme/issues/23#issuecomment-464385900, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPUc98QYUqid5cUJfdTLGnixaVYkSIcks5vOHRAgaJpZM4WGevB .

theacodes avatar Feb 17 '19 02:02 theacodes