is it possible to register python variable strings as translatable 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
sandpie
Newbie
Posts: 18
Joined: Tue Apr 25, 2023 1:43 pm
Contact:

is it possible to register python variable strings as translatable strings?

#1 Post by sandpie »

Hi I am not sure I am expressing myself properly here :)

I know that I can mark strings for translation inserting them between _( ... ) and this works out of the box for everything, BUT:

is there a python way to do the same thing to avoid having to add underscore and brackets to every string wich is not already a part of a renpy element by default marked to translate?

I mean something like this (I know this doesn't work.. and this is just an example, I know it's a silly one but I think it explains better than my words :D ):

Code: Select all

init python:
    def msg(test):
            t = _(test) #register the string for "export/translation".
            renpy.notify(t)


label start:
    scene bg room
    show eileen happy


    "Welcome"
    $ msg("my test message")

Thank you :)

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

Re: is it possible to register python variable strings as translatable strings?

#2 Post by m_from_space »

sandpie wrote: Sun Dec 03, 2023 12:46 pm is there a python way to do the same thing to avoid having to add underscore and brackets to every string wich is not already a part of a renpy element by default marked to translate?
As you know _(...) is just a function you call. So it's already Python ready. ;)

If you want function arguments (that are strings) to be registered as translatable, just do it like this:

Code: Select all

$ msg(_("my test message"))

sandpie
Newbie
Posts: 18
Joined: Tue Apr 25, 2023 1:43 pm
Contact:

Re: is it possible to register python variable strings as translatable strings?

#3 Post by sandpie »

$ msg(_("my test message"))
I guess you are saying to do it like in the following code block, but that is not what I wanted to ask: :)

Code: Select all

label start:
    scene bg room
    show eileen happy


    "Welcome"
    $ msg(_("my test message"))



I don't want to pass the already "registered for translation" string (the one between brackets and underscore) to my python defined function as a parameter,

I was trying to ask if there is a way to make the python defined function to handle the string and register it for translation inside itself: something like I asked before:

Code: Select all

    def msg(test):
            t = _(test) #register the string for "export/translation".
            #do other stuff with t, but first ADD this string to the translatable strings list.



Since it's python (which I am still learning) I thought I could do this somehow, but I can't simply write it plain as I expected, because the function _(s) seems to just return the same exact string without adding it to the list of translatable strings: I tried to generate translations several times, but my test strings were never added.
Now I guess this might be part of some complex encoding process done by renpy itself and it's not possible to do otherwise.

The reason to do this is to explore the possibility of renpy, which seems much capable already, and I have a VN idea where there might be a lot of stuff to translate that has to be "outside of the standard elements", and if I could handle the "translation stuff" through a function I would avoid a lot of underscore and brackets, let the function decide what should and should not be translatable, and make things easier for the people who might join to help me.

For example I could write:

Code: Select all


#release(label, title, author, content) label must not be translated.
newspaper.release("december", "title", "author", "a lot of rain came lorem ipsun dolor etc...." )

instead of:

newspaper.release("december", _("title"), _("author"), _("a lot of rain came lorem ipsun dolor etc....") )


I know that there are workarounds and this is only an example scenario: I would prewrite a dictionary in this specific one, anyway.. :)

I hope I made sense this time. Thank you

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: is it possible to register python variable strings as translatable strings?

#4 Post by enaielei »

If you're too lazy to let renpy know what strings are translatable using _(), then you can always skip that process and add your translations directly to the translation files.
The only purpose of that is to let renpy handle the detection of translatable strings automatically.

In one of your translation file scripts or in a new translation file script of your own, you can specify these strings.

Code: Select all

translate filipino strings:

    old "title"
    new ""

    old "author"
    new ""

    old "a lot of rain came lorem ipsun dolor etc...."
    new ""
Ren'Py don't overwrite translation files (if you're using the launcher to generate the translation files) instead it always appends the changes at the end of the files.

Now, in your function, instead of using _() you will need to call renpy.translate to the parameters/variables that you want to be displayed in your game as translated.

Code: Select all

def msg(test):
            t = renpy.translate(test)
            renpy.notify(t)

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

Re: is it possible to register python variable strings as translatable strings?

#5 Post by m_from_space »

sandpie wrote: Mon Dec 04, 2023 7:12 pmI tried to generate translations several times, but my test strings were never added.
That's because you don't understand what the function does. You still have to hit "Generate Translations" inside the Renpy GUI, when you make your game ready for translation. It doesn't make a lot of sense if something would be added to a translation file while you run your game. What's the point if a player runs a game and while they do so, some string is added to a file, and that string isn't even translated yet?
I hope I made sense this time. Thank you
I think you should rethink what translating a game means. It's not something you do while the game is running, but before creating a release, at least if you're not using on-demand translation via some LLM or whatnot. :)
enaielei wrote: Mon Dec 04, 2023 10:00 pm Now, in your function, instead of using _() you will need to call renpy.translate to the parameters/variables that you want to be displayed in your game as translated.
Not really, since the notify screen already uses a !t as part of the message interpolation. And if it wasn't using interpolation, then even then it's not necessary, since texts on screens are always automatically translated when shown.

sandpie
Newbie
Posts: 18
Joined: Tue Apr 25, 2023 1:43 pm
Contact:

Re: is it possible to register python variable strings as translatable strings?

#6 Post by sandpie »

enaielei wrote: Mon Dec 04, 2023 10:00 pm If you're too lazy to let renpy know what strings are translatable using _(), then you can always skip that process and add your translations directly to the translation files.
It's not lazyness, it is to avoid potential insertion mistakes. :)
I will experiment with renpy.translate as you suggested, thank you for the heads up.

That's because you don't understand what the function does. You still have to hit "Generate Translations" inside the Renpy GUI, when you make your game ready for translation. It doesn't make a lot of sense if something would be added to a translation file while you run your game. What's the point if a player runs a game and while they do so, some string is added to a file, and that string isn't even translated yet?
I think you should rethink what translating a game means. It's not something you do while the game is running, but before creating a release
I am making these questions because i am still evaluating renpy and I know what translating a game means :D it's better to know the limits and possibilities ahead of the potential issues, which will always happen anyway.
Since a lot here happens under the hood I presumed that Generating Translation might mean that renpy was parsing the whole code, and maybe even running it once, following every possible option. It's not like that? It's fine: I was just asking if it was possible to do it or not, and apparently the answer is not, unless enaielei suggestion is applicable. :)

Thank you

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], LittleRainySeasons