[solved] Translation of strings

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
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

[solved] Translation of strings

#1 Post by thelastsecret »

I have a strange issue with translations of strings:
I have defined a function to say certain texts (I do this, since the speaker changes depending on a variable). It's something like:

Code: Select all

label hike_say(text):
    if hike=="Meilin":
        m "[text]"
    if hike=="Karina":
        k "[text]"
    return
This function gets called like that:

Code: Select all

call hike_say(_("That's a sample text."))
I would expect that _() makes sure that this text gets translated, and thus the translated text appears on screen. Indeed, the string is correctly listed in the translation file and I have added the translation. However, instead of the translated text, the original text is displayed.

And here are my questions:
1. Why on earth is this the case?
2. What can I do to change that?

I would be super happy if somebody could help me out here!
Last edited by thelastsecret on Wed May 25, 2022 11:27 pm, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 950
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Translation of strings

#2 Post by m_from_space »

If you want to have variables inside strings translated, you have to add "!t".

Code: Select all

label hike_say(text):
    if hike=="Meilin":
        m "[text!t]"
    if hike=="Karina":
        k "[text!t]"
    return

User avatar
m_from_space
Miko-Class Veteran
Posts: 950
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Translation of strings

#3 Post by m_from_space »

By the way you could achieve the same thing writing, no need for an extra label.

Code: Select all

$ renpy.say(eval(hike), _("That's a sample text."))
edit: Don't use this, it's bad. :D
Last edited by m_from_space on Wed May 25, 2022 2:30 am, edited 2 times in total.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Translation of strings

#4 Post by PyTom »

Calling renpy.say and eval are quite bad.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
m_from_space
Miko-Class Veteran
Posts: 950
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Translation of strings

#5 Post by m_from_space »

PyTom wrote: Wed May 25, 2022 2:15 am Calling renpy.say and eval are quite bad.
Ok. :D Is there a reason? Is there a better solution?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Translation of strings

#6 Post by PyTom »

Eval is very slow, as Ren'Py does a lot of transformations to the Python to compile it - versus just looking up a name in a dict.

renpy.say can't be predicted, while a normal say statement can be. Predictability is a big win in Ren'Py.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
m_from_space
Miko-Class Veteran
Posts: 950
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Translation of strings

#7 Post by m_from_space »

Yeah okay, that makes sense.

User avatar
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

Re: Translation of strings

#8 Post by thelastsecret »

Thanks a lot, that was great help! I'll try it out asap! :)

User avatar
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

Re: Translation of strings

#9 Post by thelastsecret »

I have changed the function as described:

Code: Select all

label hike_say(text):
    if hike=="Meilin":
        m "[text!t]"
    if hike=="Karina":
        k "[text!t]"
    return
Now most cases work, but not unfortunately, not all, since there is one more special case, namely constructions like this:

Code: Select all

call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello ")+playername+"!")
In this case, the text still doesn't get translated, although I have a translation for the string "This is a sample, addressing the player with the player's name in a variable: Hello " in the translation file (generated by Ren'py). I think that's because it looks for a translation of "This is a sample, addressing the player with the player's name in a variable: Hello Anna!" (if, let's say, playername=="Anna") and doesn't find it this string.
But how can I fix that? I'm clueless...

Edit: The other suggested hack gives the same result.

User avatar
m_from_space
Miko-Class Veteran
Posts: 950
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Translation of strings

#10 Post by m_from_space »

Don't use my hack since Pytom knows what he's talking about. ;)

For your latest issue you should think about how renpy translates: It automatically translates strings that exactly matches the ones inside the translation file when it shows that string on screen. Since you alter your string before it can be translated, it will not find it. (You put "playername" + "!" at the end.)

I suggest:

Code: Select all

call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello {nw}"))
extend "[playername]!"
edit: Don't forget to update your translation file, since I added a "{nw}".

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

Re: Translation of strings

#11 Post by Ocelot »

You can write your string with substitution inside like call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello [playername]!") and use [ . . . !ti] to interpolate translated string.

But you can just assign your speaker to variable directly to avoid most of that hassle.

Code: Select all

# was
$ hike = "Meilin":
call hike_say("some text . . .")
$ hike = "Karina"
call hike_say("other text . . .")

# now
$ hike = m
hike "some text . . ."
$ hike = k
hike "other text . . ."
< < insert Rick Cook quote here > >

User avatar
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

Re: Translation of strings

#12 Post by thelastsecret »

m_from_space wrote: Wed May 25, 2022 3:58 pm I suggest:

Code: Select all

call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello {nw}"))
extend "[playername]!"
Okay, that's an elegant workaround, thank you!. I should have come up with that, too. :? Looks like I'm not smart enough. :roll:
EDIT: However, it doesn't work as easily! Or actually, at first it does, so the text translates and is displayed, but then it's immediately replaced by:

Code: Select all

[text!t][playername]?
It took me a while to find the reason for that, although it's fairly easy: the variable text is only defined in the function, but the extend command happens after the function had been called in the main program, so Ren'py doesn't know the variable text then and so just prints the "source code".
I found a fix for that, however: I just define the variable in the main program with the same value:

Code: Select all

$text="This is a sample, addressing the player with the player's name in a variable: Hello {nw}"
call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello {nw}"))
extend "[playername]!"
Then it finally works!
Fortunately, there are only three such instances where the player is addressed directly, otherwise that would be tedious.

So thanks again for the help! That was surprisingly tricky, I must say...
Last edited by thelastsecret on Wed May 25, 2022 11:27 pm, edited 1 time in total.

User avatar
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

Re: Translation of strings

#13 Post by thelastsecret »

Ocelot wrote: Wed May 25, 2022 5:00 pm You can write your string with substitution inside like call hike_say(_("This is a sample, addressing the player with the player's name in a variable: Hello [playername]!") and use [ . . . !ti] to interpolate translated string.
Okay, I need to digest this interpolation thingy, but it sounds cool.
But you can just assign your speaker to variable directly to avoid most of that hassle.

Code: Select all

# was
$ hike = "Meilin":
call hike_say("some text . . .")
$ hike = "Karina"
call hike_say("other text . . .")

# now
$ hike = m
hike "some text . . ."
$ hike = k
hike "other text . . ."
You can do - WHAT??? :shock:
Sometimes Ren'py is like magic! :lol:
However, this does not work completely, since it just prints "m" instead of "Meilin" as the speaker's name. Also the color of the speaker is just the standard yellow one.

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: [solved] Translation of strings

#14 Post by philat »

You probably used $ hike = "m" instead of $ hike = m.

User avatar
thelastsecret
Regular
Posts: 117
Joined: Tue Mar 01, 2022 1:32 pm
Completed: The Last Secret
Projects: Sweet Science – The Girls from Silversee Castle
itch: thelastsecret
Discord: TheLastSecret #5266
Contact:

Re: [solved] Translation of strings

#15 Post by thelastsecret »

philat wrote: Wed May 25, 2022 11:35 pm You probably used $ hike = "m" instead of $ hike = m.
*face palm*
Yep, that's it. This would have been the easy solution. Just define a new speaker hike wither as m or k and then use that throughout this section. I'm certified stupid.
Next time I'll program something like this, I know how to save my time and make the code simple.
Thank you so much!

Post Reply

Who is online

Users browsing this forum: Andredron