Replace Strings in variables

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
lorum
Newbie
Posts: 1
Joined: Tue Apr 19, 2022 2:22 pm
Contact:

Replace Strings in variables

#1 Post by lorum »

Hi, for some time I've been looking for how to translate some texts that are outside the normal translations, this is "text" tags or variables of the type: $ var = "hello world", used in some games, I've tried with _( ) and com __() but they don't always work, so I made this little script to help me:

Code: Select all

init python:

    def replace_text(t):
        if _preferences.language == "spanish":

                    t = t.replace("You've created a {i}new{/i} \n Ren'Py game.", "Otro texto 1")
                    t = t.replace("other text 2 ", "otro texto 2 ")

        return t

    config.say_menu_text_filter = replace_text

And it works until I come across variables of this type:

Code: Select all

   $ f += "the {i}bee{/i}\n"
   $ f += "is very big\n"

    e "hi [f]"
Due to the use of {} and to the fact that the variable "f" is concatenated with other texts, the script does not recognize them, my question is is there any way to see the content of a variable in the script to be able to replace it?

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Replace Strings in variables

#2 Post by Imperf3kt »

Does it work for you, using .format?

Code: Select all

    $ f += "the {i}bee{/i}\n"
    $ f += "is very big\n"
    e "hi {}".format(f)
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

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: Replace Strings in variables

#3 Post by Remix »

You should be registering the strings for translation ...

$ f += _("the {i}bee{/i}\n")

(the _() registers it)

If the translation still does not occur, either use the !t modifier in interpolation ( "[var!t]" ) or renpy.substitute() as s string wrapper ( e( renpy.substitute( _("Translate this") ) ) )
Frameworks & Scriptlets:

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2405
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Replace Strings in variables

#4 Post by Ocelot »

Remix wrote: Fri Apr 22, 2022 7:29 pm You should be registering the strings for translation ...

$ f += _("the {i}bee{/i}\n")

(the _() registers it)

If the translation still does not occur, either use the !t modifier in interpolation ( "[var!t]" ) or renpy.substitute() as s string wrapper ( e( renpy.substitute( _("Translate this") ) ) )
You should use double underscore for immideate translation if you are building strings: __("the {i}bee{/i}\n"). Translation framework translates strings when they are displayed, just before substitutions happen. And it will do that only if exact match is found. So you want to translate parts of the strings before they are glued together. Or use translated interpolation, as you suggested.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]