[Solved] Choice menu, tooltip and translation problem

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Potato0095
Regular
Posts: 84
Joined: Sun May 08, 2016 8:40 pm
Projects: LoveCraft
itch: potato95
Location: Brazil
Contact:

[Solved] Choice menu, tooltip and translation problem

#1 Post by Potato0095 »

Hi, I'm having a bit of a problem. I managed to customize my screen choice to have a tooltip be shown dynamically through the usage of a split " | " in the i.caption (viewtopic.php?f=8&t=43454).

My original English code (shows the first part (before the |) in the button, and shows the second as a tooltip)

Code: Select all

menu:
        "Sure.":
            pass
        "I'm okay for now. | NOT recommended for players on their first run.":
            pass
Image

My "custom" screen choice inside screen.rpy

Code: Select all

screen choice(items):
    default choice_context=None
    style_prefix "choice"

    add "gui/overlay/confirm.png" at basicfade

    vbox:
        for i in items:
            if '|' in i.caption:
                textbutton i.caption.split('|')[0] action i.action hovered SetScreenVariable("choice_context", i.caption.split('|')[1]) unhovered SetScreenVariable("choice_context", None)
            else:
                textbutton i.caption action i.action hovered SetScreenVariable("choice_context", None)

        hbox:
            xalign 0.5
            ypos -25

            if choice_context is not None:
                text choice_context
            else:
                text ""
The problem is that it breaks when translating to another language. It doesn't translate this choice, it is kept in English.

Image

Removing the " | " fixes this problem, but now the game won't acknowledge the " | " in the translated file, showing all the text and not splitting at all.

Image

My altered English code and translation file (the text is translated but the game doesn't acknowledge the split in the custom screen choice.)

Code: Select all

menu:
        "Sure.":
            pass
        "I'm okay for now.":
            pass

Code: Select all

# game/scripts/game/scripts_game_chapter_1.rpy:1197
    old "I'm okay for now."
    new "Estou de boa no momento. | NÃO recomendado para aqueles jogando pela primeira vez."
Is there any way to translate the game and keep the splitting that's happening in the default language?
Last edited by Potato0095 on Thu Feb 04, 2021 8:28 am, edited 1 time in total.
"There are two types of lies: Lies that hurt, and lies that don't."

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Choice menu and translation problem

#2 Post by Remix »

I am guessing that the translation occurs after the test
Basically that i.caption is in the None language (English) up until it is displayed. So it splits
"I'm okay for now. | NOT recommended for players on their first run."
then looks for a translation for each side separately, does not find just "I'm okay for now. " so defaults to using the None English only...

Possible fix:

Code: Select all

        for i in items:
            $ translated_caption = renpy.substitute(i.caption)
            if '|' in translated_caption:
                textbutton translated_caption.split('|')[0]:
                    action i.action 
                    hovered SetScreenVariable("choice_context", translated_caption.split('|')[1]) 
                    unhovered SetScreenVariable("choice_context", None)
            else:
                textbutton i.caption action i.action hovered SetScreenVariable("choice_context", None)
Worth trying...
Not fully sure it will find the translation though (as it might not know where to look for i.caption)
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Choice menu and translation problem

#3 Post by Remix »

Sub-note:
You would likely do a bit better using choice attributes to pass the tooltip in (if using modern Ren'Py)

"I'm okay for now." (tooltip = _("NOT recommended for players on their first run.") ):

If the hacky fix above does not work, just ask for advice on implementing choice attributes... they are quite easy and generally better.
Frameworks & Scriptlets:

User avatar
Potato0095
Regular
Posts: 84
Joined: Sun May 08, 2016 8:40 pm
Projects: LoveCraft
itch: potato95
Location: Brazil
Contact:

Re: Choice menu and translation problem

#4 Post by Potato0095 »

What I ended up doing was a little workaround of mine.

I assigned two variables, "first_part" and "second_part", defaulted at "" (an empty string). The textbutton shows the first_part as i.caption and the tooltip gets the second_part to show when hovered.

screens.rpy:

Code: Select all

screen choice(items):
    default choice_context=None
    style_prefix "choice"

    vbox:
        for i in items:
            if '|' in i.caption:
                textbutton first_part action i.action hovered SetScreenVariable("choice_context", second_part) unhovered SetScreenVariable("choice_context", None)
            else:
                textbutton i.caption action i.action

        hbox:
            xalign 0.5
            ypos -25

            if choice_context is not None:
                text choice_context
            else:
                text ""
Menu choices that have a " | " for splitting now need to have their text and tooltip assigned inside "first_part" and "second_part" (I haven't found a way to easily assign values to variables inside a menu, so it's best to assign it prior to the menu) before prompting the user. After assigning a new string into both variables, you use the menu interpolating the variable inside one of the choices, separating both by a " | " and with the !t to assign them for translation.

Code: Select all

menu:
        "Sure.":
            pass
        "[first_part!t] | [second_part!t]":
            pass
The problem with this is that it only lets you have one choice with a tooltip. If I had a way to dynamically assign new variables when hovering menu choices I could probably fix this, but this workaround suits my needs for now.
"There are two types of lies: Lies that hurt, and lies that don't."

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Choice menu and translation problem

#5 Post by Remix »

Code: Select all

screen choice(items):
    default choice_context=None
    style_prefix "choice"

    vbox:
        for i in items:
            textbutton i.caption:
                action i.action
                # All buttons have tooltips, ones with an actual (tooltip= ) attribute use that, others use None 
                hovered SetScreenVariable("choice_context", i.kwargs.get('tooltip', None))
                unhovered SetScreenVariable("choice_context", None)

        hbox:
            xalign 0.5
            ypos -25

            if choice_context is not None:
                text choice_context
            else:
                text ""

label test_menu:
    menu:
        "First Part" (tooltip=_("First Part Tooltip")): ## note the _() wrapping the tooltip (adding it to translation)
            pass
        "Second Part" (tooltip=_("Second Part Tooltip")):
            pass
        "Third Part": ### no tooltip
            pass
Frameworks & Scriptlets:

User avatar
Potato0095
Regular
Posts: 84
Joined: Sun May 08, 2016 8:40 pm
Projects: LoveCraft
itch: potato95
Location: Brazil
Contact:

Re: Choice menu and translation problem

#6 Post by Potato0095 »

Never learned how to use i.kwargs properly. This is perfect! Thank you Remix!
"There are two types of lies: Lies that hurt, and lies that don't."

Post Reply

Who is online

Users browsing this forum: Dark79, Google [Bot]