Help with Changelog (Ren'Py 7.4.2) renpy.translate_string ()

Oct 4, 2020
435
871
Sorry for my English.

In the changelog for version 7.4.2 it says:

renpy.translate_string () is now a documented function that provides the ability to translate a string to a selected language.

I have looked everywhere and the only thing I have found has been in renpy documentation that says:

renpy.translate_string (s, language = <renpy.object.Sentinel object at 0x7faa89a44890>)
Returns s immediately translated into language. If language is Default, uses the language set in the preferences. Strings enclosed in this function will not be added to the list of translatable strings. Note that the string may be double-translated, if it matches a string translation when it is displayed.


Could someone please be so kind as to explain its use and create a little code for me to understand it well?
Thanks a lot
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,384
15,293
renpy.translate_string (s, language = <renpy.object.Sentinel object at 0x7faa89a44890>)
This part of the documentation is an error. ; whatever was used to generate it did a literal interpretation. It should be :
Code:
renpy.translate_string (s, language="default")
In short, you provide a string in the default language, (s), then the language in which you want to translate it (language), and it return the translated version of the string, accordingly to the translation files located in the "game/tl" directory ; what imply that you probably need to manually create and populate the file.

Code:
label whatever:
    $ translatedString = renpy.translate_string( "Quick save complete.", "french" )
    "[translatedString]"
Will display "Sauvegarde rapide effectuée.".
You can try it from the console when using the SDK with renpy.translate_string( "Quick save complete.", "french" ), I know that this translation exist.


This being said, for what are you needing this function ? The translation of dialog lines is automatic, and for screens it's better to use _( ) (text _( "this will be translated" )) than trying to handle the translation manually.
 
Oct 4, 2020
435
871
This being said, for what are you needing this function ? The translation of dialog lines is automatic, and for screens it's better to use _( ) (text _( "this will be translated" )) than trying to handle the translation manually.
you're right
but since I am very interested in the subject of translations and did not understand this function, I wanted to learn it
I already knew the methods you mention, but lately some developers are making it more and more difficult to generate translations believe me
and I try to find out new methods
Thank you very much, even if you don't believe it, it can help me
I will study it
I also tried things with:
def replace_text (t):

but it was a failure because it doesn't translate whole strings

Thanks a lot
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,384
15,293
Forgive me anne O'nymous I have done a thousand tests and I still do not get it.
the option to use it from the SDK I did it without problems, I understood.
but in game I can't do it :cry:
Note: I haven't used it, nor even tested it, yet, so don't take this as granted.

From my understanding, it's just another way to address the translation files. Therefore you firstly need to have a translation entry for the strings.
Like it's a Ren'py function and not a statement, I assume that the translation entry isn't generated automatically ; unless you use it in a user defined statement to which you gave a translation_strings entry. What imply that you've to generate the translation file by yourself. Either by updating the "common.rpy" file for the given language, or by creating a new file (named however you want).
And like it's not a dialog line translation, you need to use the "old/new" syntax for this.

Assuming that you want to translate in "whatever" language, you need to do this :
  • Create a "whatever" directory in the "game/tl" directory, unless it already exist.
  • Create a "myTranslation.rpy" (or whatever other name) file in this directory.
  • Fill it this way :
    Python:
    # Note: "whatever" is the language name as it will be used inside Ren'py.
    translate whatever strings:
    
        #  Comment if you want, it's not mandatory but it can help the guy that 
        # that will do the translation to have a context.
        old "translate this string"
        new "THE TRANSLATED VERSION"
  • Then in the game code itself, you use it like this :
    Python:
    label whatever:
        # Once again, "whatever" is the name you gave to the language.
        $ translatedString = renpy.translate_string( "translate this string", "whatever" )
        "[translatedString]"

As I said, I haven't tested it, but it's how I would start my test.
 
Oct 4, 2020
435
871
Thank you....

old "translate this string"
new "THE TRANSLATED VERSION"

that was my first test and ... nothing

"[translatedString]" is what puzzles me

then I tried adding:
old "[translatedString]"
new "THE TRANSLATED VERSION"

and nothing

then i tried
new "[translatedString]"
old "[translatedString! t]"

and nothing
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,384
15,293
old "translate this string"
new "THE TRANSLATED VERSION"

that was my first test and ... nothing
Then you forgot something ; to put it in a translate block perhaps.

game/tl/french/mine.rpy
Code:
translate french strings:
    old "Is this working ?"
    new "Est-ce que cela fonctionne ?"
game/script.rpy
Code:
label start:
    $ translatedString = renpy.translate_string( "Is this working ?", "french" )
    "[translatedString]"
Just tested it, and it works perfectly.


"[translatedString]" is what puzzles me
It's just the way Ren'py interpolate the content of a variable into a text string for dialog, menu and text/textbutton statement. Therefore it just tell to Ren'py that it need to display the content of the translatedString variable.