Implementing Translations in Ren'Py [Updated Tutorial]

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Message
Author
User avatar
Andredron
Veteran
Posts: 340
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#16 Post by Andredron » Mon Jun 03, 2019 9:03 pm

richycapy wrote:
Mon Jun 03, 2019 8:40 pm
Andredron wrote:
Mon Jun 03, 2019 8:25 pm
imagebutton..... action.....
Hi

Im doing this:

Code: Select all

vbox xalign 1.0 yalign 1.0:
        if baselang == 'spanish':
            imagebutton auto "images/gui/botones/english_%s.png" hovered tt.Action("Play game in english") action 
        else:
            imagebutton auto "images/gui/botones/spanish_%s.png" hovered tt.Action("Juega el juego en español") action Language("spanish")
And its not doing anything :(

It did change to spanish, but is not changing back to english, also, is there a way to restart the game after changing the lenguage?

Code: Select all


translate.rpy

init -3 python: 
    if persistent.lang is None: 
        persistent.lang = "english"
    lang = persistent.lang
init python: 
    config.main_menu.insert(3, (u'Language', "language_chooser", "True"))
init python:
    if lang == "english":
        style.default.font = "font_name_here_1.ttf" #english font here 
    elif lang == "russian": 
        style.default.font = "font_name_here_2.ttf" #russian font here

Code: Select all


Screen perfirence:
....... 
.......... vbox xalign 1.0 yalign 1.0:
................imagebutton.....  action Language(None)
................ imagebutton..... action Language("russian)
I’ve only seen this code at all this year(cookbook) , and I’ve used the example that launcher Renpy has in training. There, everything is clearly defined in the settings, how to register the list of languages in the screen
I know, I'm writing terribly in English.

I'm writing a Renpy textbook (in Russian). https://yadi.sk/d/ZX_DonP63USRru Update 22.06.18

Honest Critique

User avatar
richycapy
Regular
Posts: 35
Joined: Mon May 27, 2019 8:53 pm
Organization: EN Productions
Location: Mexico
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#17 Post by richycapy » Thu Jun 06, 2019 12:49 am

I just found out whats is the problem

When I change the lenguaje in the menu, it changes the text lenguaje, but the var "persistent.lang" it never changes from "english", so this

Code: Select all

init -3 python:
    if persistent.lang is None:
        persistent.lang = "english"
    lang = persistent.lang

vbox xalign 1.0 yalign 1.0:
        if lang == 'spanish':
            imagebutton auto "images/gui/botones/english_%s.png" hovered tt.Action("Play game in english") action Language(None) 
        else:
            imagebutton auto "images/gui/botones/spanish_%s.png" hovered tt.Action("Juega el juego en español") action Language("spanish")
Will always show the "else" option, it never ask to play the game in english

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#18 Post by Matalla » Thu Jun 06, 2019 8:01 am

richycapy wrote:
Thu Jun 06, 2019 12:49 am
I just found out whats is the problem

When I change the lenguaje in the menu, it changes the text lenguaje, but the var "persistent.lang" it never changes from "english", so this

Code: Select all

init -3 python:
    if persistent.lang is None:
        persistent.lang = "english"
    lang = persistent.lang

vbox xalign 1.0 yalign 1.0:
        if lang == 'spanish':
            imagebutton auto "images/gui/botones/english_%s.png" hovered tt.Action("Play game in english") action Language(None) 
        else:
            imagebutton auto "images/gui/botones/spanish_%s.png" hovered tt.Action("Juega el juego en español") action Language("spanish")
Will always show the "else" option, it never ask to play the game in english
You don't need to use any init phython code, nor the variable lang.
The simplest, more accurate way of checking what langauge the game is currently using is:

Code: Select all

if _preferences.language == "spanish": # Or "english", None or something else
This is the proper way of checking the current language. To change it, either use the action like you do, or renpy.change_language. _preferences.language is read only and you can't use it to change the language.

Be careful when using cookbook stuff for your project, most of it is old code (which sometimes doesn't work anymore) and/or there are better ways of doing things with the actual version of Renpy.
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
Ayael
Regular
Posts: 57
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#19 Post by Ayael » Fri Jun 07, 2019 10:54 am

I have a question, once the translation script are created, what happen if we modify the original one ? By adding music or image for example. Same, what happen if we want to add a new line of text ?

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#20 Post by Matalla » Fri Jun 07, 2019 11:57 am

Ayael wrote:
Fri Jun 07, 2019 10:54 am
I have a question, once the translation script are created, what happen if we modify the original one ? By adding music or image for example. Same, what happen if we want to add a new line of text ?
I'm not sure what would happen if you add music or images, I guess nothing.

But if you add some line of text, or modify one of the existent ones, even by adding a comma, here's what you have to do:

- Regenerate the translation files
- Look for the new entry in the translation file (it should be at the end)
- You can cut it and place it in a more appropiate part of the script (you can paste it over the previous one if it's a modification, renpy doesn't delete the old ones)
. If english is the translated language and you don't have yet a working common.rpy file in tl/english/ you'll need to either delete it o get a working one and place it there, otherwise, all the default messages in english would be gone.

One way of getting a working common file in english is to start a new project in english, go to tl/None and copy the common.rpym file there and paste in your tl/english/ folder, rename the file to common.rpy and edit the first line of the script so it says:

Code: Select all

 translate english strings:
Bottom line: you need to have your script as finished and tested as possible, otherwise you'll be in a lot of trouble. There's always some minor corrections, a typo or something, for which you'll have to do what is explained above, but you should avoid, if possible, to translate the game while you're developing it. It's a bad idea.
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
Ayael
Regular
Posts: 57
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#21 Post by Ayael » Fri Jun 07, 2019 2:22 pm

Matalla wrote:
Fri Jun 07, 2019 11:57 am
Ayael wrote:
Fri Jun 07, 2019 10:54 am
I have a question, once the translation script are created, what happen if we modify the original one ? By adding music or image for example. Same, what happen if we want to add a new line of text ?
I'm not sure what would happen if you add music or images, I guess nothing.

But if you add some line of text, or modify one of the existent ones, even by adding a comma, here's what you have to do:

- Regenerate the translation files
- Look for the new entry in the translation file (it should be at the end)
- You can cut it and place it in a more appropiate part of the script (you can paste it over the previous one if it's a modification, renpy doesn't delete the old ones)
. If english is the translated language and you don't have yet a working common.rpy file in tl/english/ you'll need to either delete it o get a working one and place it there, otherwise, all the default messages in english would be gone.

One way of getting a working common file in english is to start a new project in english, go to tl/None and copy the common.rpym file there and paste in your tl/english/ folder, rename the file to common.rpy and edit the first line of the script so it says:

Code: Select all

 translate english strings:
Bottom line: you need to have your script as finished and tested as possible, otherwise you'll be in a lot of trouble. There's always some minor corrections, a typo or something, for which you'll have to do what is explained above, but you should avoid, if possible, to translate the game while you're developing it. It's a bad idea.
Thank you for this answer, it will be helpful ! I agreed, it's best to have the most polish version before translating, but I asked about modification because I need the sound designer to understand the text to add the music, and I can't find one in french so....

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#22 Post by Matalla » Fri Jun 07, 2019 4:49 pm

Ayael wrote:
Fri Jun 07, 2019 2:22 pm
Thank you for this answer, it will be helpful ! I agreed, it's best to have the most polish version before translating, but I asked about modification because I need the sound designer to understand the text to add the music, and I can't find one in french so....
You can make a test project with a couple of lines, generate the translation files, translate them and then, add some music or images, regenerate the translations and see if something's broken.

I think nothing would happen with the translation files as long as you don't touch/add anything that's translatable.
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
Ayael
Regular
Posts: 57
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#23 Post by Ayael » Sun Jun 09, 2019 3:42 pm

I thought the way to choose the language was a bit complicated, and I did not want to use the in game look, so I did it in a different way.

If you want to know :

♦ Choose the langage as splashscreen (even before going to the game menu), ONCE.
♦ Change possible in preference screen.

The splashscreen :

Code: Select all

default persistent.language = None #By default, the language is not selected 

label splashscreen: #The label to set a splashscreen before main menu
    if persistent.language: #If we select at least once, we didn't need to set the screen, otherwise we do. 
        return()
    else:
        call screen choose_language()



screen choose_language:

    frame:

        hbox:
            vbox:
                null height 10

                textbutton "Langue française":
                     action Language(None) #None = default language 

                textbutton "English language":
                     action Language("english") #It must be EXACTLY the name of the translation file. So without caps. 
        hbox:
            textbutton _("Ok"): #Some button to validate the whole stuff and set the persistent on true. 
                 style "languagevalid"
                 action SetField(persistent, "language", True),  Return()

And then in my preference screen, I just add :

Code: Select all

textbutton "Langue française" action Language(None)
                    textbutton "English language" action Language("english")

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#24 Post by Matalla » Sun Jun 09, 2019 7:05 pm

I can see some minor problems there. It would probably still work but there are some things that are not really needed or contradictory.

First, there's no need to assign a default value to a persistent variable, by default they don't exist, so assign a None value to it it's superfluous.

Second, the default variables are the last to be assigned and (I think) it's just at the start of the game. So the splashscreen wouldn't have access to that.

Third, even if the previous thing work, the point of assigning a default value to a persistent variable is wrong, because it would be updated to None every time the game is run, and you don't use a persistent variable for that

And last, and this is just a personal thing. If you have two buttons for language, I'd get rid of the button to validate it.

This is the code I use for this stuff:

Code: Select all

label before_main_menu:

    # Show the language screen selection the first time the game runs
    if persistent.language_selected is None:
        call screen choose_language # Although it's good to define the screen with (), you don't need to call it like that, unless you're actually passing some arguments to it

    return

Code: Select all

screen choose_language():
    
    frame:
        vbox:
            text "El juego está disponible en español e inglés / The game is available in english and spanish" 
            text "Escoge tu idioma / Choose your language"
            hbox:
                style_prefix "radio"
                # The buttons, first select the language, then mark the persistent variable as True, so the before_main_menu code won't run anymore
                imagebutton auto "gui/button/prefs/bt_spanish_%s.png":
                    action [Language(None), SetVariable("persistent.language_selected", True), Return(True)]
                imagebutton auto "gui/button/prefs/bt_english_%s.png":
                    action [Language("english"), SetVariable("persistent.language_selected", True), Return(True)]
The preferences screen options I do as you did, so no need to put the code
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
Ayael
Regular
Posts: 57
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#25 Post by Ayael » Mon Jun 10, 2019 3:16 am

Hello, thank you for sharing your own code and your wisedom !

I know I don't have to use a validation button, but I think it's better to have one, on this particular screen, it works as a "security". (If you missclick, or since he changes according the language you can check immediatly if it works).

For the default persistent, I am note sure 100% but I think the "default" is meant exactly for that. Because if I just declare the variable without it, I have the problem you mention (the variable always getting back to "None"), but if I use it, it work perfectly, (even if I close the game AND renpy, and restart my computer, the variable is still set properly).

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#26 Post by Matalla » Mon Jun 10, 2019 7:51 am

Ayael wrote:
Mon Jun 10, 2019 3:16 am
Hello, thank you for sharing your own code and your wisedom !

I know I don't have to use a validation button, but I think it's better to have one, on this particular screen, it works as a "security". (If you missclick, or since he changes according the language you can check immediatly if it works).

For the default persistent, I am note sure 100% but I think the "default" is meant exactly for that. Because if I just declare the variable without it, I have the problem you mention (the variable always getting back to "None"), but if I use it, it work perfectly, (even if I close the game AND renpy, and restart my computer, the variable is still set properly).
I'm not sure why it works for you. But declaring a persistent variable is just not right. They're meant to... persist. Not to be updated to the same value every time the game is run.

I think your code could work because it's one of those rare cases where several mistakes somehow manage to work together to make it right. But the premises are wrong.
Comunidad Ren'Py en español (Discord)
Honest Critique

User avatar
Ayael
Regular
Posts: 57
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#27 Post by Ayael » Mon Jun 10, 2019 9:08 am

Matalla wrote:
Mon Jun 10, 2019 7:51 am
Ayael wrote:
Mon Jun 10, 2019 3:16 am
Hello, thank you for sharing your own code and your wisedom !

I know I don't have to use a validation button, but I think it's better to have one, on this particular screen, it works as a "security". (If you missclick, or since he changes according the language you can check immediatly if it works).

For the default persistent, I am note sure 100% but I think the "default" is meant exactly for that. Because if I just declare the variable without it, I have the problem you mention (the variable always getting back to "None"), but if I use it, it work perfectly, (even if I close the game AND renpy, and restart my computer, the variable is still set properly).
I'm not sure why it works for you. But declaring a persistent variable is just not right. They're meant to... persist. Not to be updated to the same value every time the game is run.

I think your code could work because it's one of those rare cases where several mistakes somehow manage to work together to make it right. But the premises are wrong.
It's slightly offtopic, but how you do then, when you want a permanent to be, by default on true, and then the player can take it to none, if he wish ? (Like, I have that with my +18 content, by default it's on true, but in the setting menu, we can desable it. It use permanent too, and you can't just... not declare it, if you want it on true by default.)

Plus, I learn that from that topic : viewtopic.php?t=45621
Where several veteran speak about setting them at default. (Quite old though, but I didn't see another subject like this more recent)

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#28 Post by Matalla » Mon Jun 10, 2019 10:47 am

Ayael wrote:
Mon Jun 10, 2019 9:08 am
It's slightly offtopic, but how you do then, when you want a permanent to be, by default on true, and then the player can take it to none, if he wish ? (Like, I have that with my +18 content, by default it's on true, but in the setting menu, we can desable it. It use permanent too, and you can't just... not declare it, if you want it on true by default.)

Plus, I learn that from that topic : viewtopic.php?t=45621
Where several veteran speak about setting them at default. (Quite old though, but I didn't see another subject like this more recent)
Maybe there could be situations where declaring them makes sense. I can't think of one.

For the situation you say, I'd use the splashscreen or the before_main_menu labels to set it to true, like the language thing:

Code: Select all

label before_main_menu:

    if persistent.naughtythings is None:
        $ persistent.naughtythings = True
After the first run, it would be set to True, then you can set it in preferences to True or False (not to None, as that would imply that at the next run would be set to True again). Think of it this way: True means that the player wants it; False, that they don't want it; None would mean that the question has no answer.
Comunidad Ren'Py en español (Discord)
Honest Critique

SkyDelete
Newbie
Posts: 1
Joined: Thu Aug 15, 2019 8:14 am
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#29 Post by SkyDelete » Thu Aug 15, 2019 8:25 am

Maybe you can help me with the font swap at Ren'P?
If I'm doing a font swapping as specified in the training game, then part of the text is displayed in blank squares. (Screenshots 01-02)

Code: Select all

## Russian fonts swap
translate russian python:
    gui.text_font = "Oswald-Light.ttf"
    gui.name_text_font = "Gismonda CYR.otf"
    gui.interface_text_font = "Gismonda CYR.otf"
01 Fonts_swap_prferences.jpg
02 Fonts_swap_game.jpg
If I just replace fonts, everything is displayed correctly. (Screenshots 03-04)

Code: Select all

## Russian fonts swap workaround
define gui.text_font = "Oswald-Light.ttf"
define gui.name_text_font = "Gismonda CYR.otf"
define gui.interface_text_font = "Gismonda CYR.otf"
03 Fonts_replace_prferences.jpg
04 Fonts_replace_game.jpg
Any idea why this is happening, how to fix it?

User avatar
Matalla
Regular
Posts: 198
Joined: Wed Mar 06, 2019 6:22 pm
Completed: The Lost Smile
itch: matalla-interactive
Location: Spain
Contact:

Re: Implementing Translations in Ren'Py [Updated Tutorial]

#30 Post by Matalla » Fri Aug 16, 2019 7:39 pm

SkyDelete wrote:
Thu Aug 15, 2019 8:25 am
Maybe you can help me with the font swap at Ren'P?
If I'm doing a font swapping as specified in the training game, then part of the text is displayed in blank squares. (Screenshots 01-02)

Code: Select all

## Russian fonts swap
translate russian python:
    gui.text_font = "Oswald-Light.ttf"
    gui.name_text_font = "Gismonda CYR.otf"
    gui.interface_text_font = "Gismonda CYR.otf"
01 Fonts_swap_prferences.jpg02 Fonts_swap_game.jpg

If I just replace fonts, everything is displayed correctly. (Screenshots 03-04)

Code: Select all

## Russian fonts swap workaround
define gui.text_font = "Oswald-Light.ttf"
define gui.name_text_font = "Gismonda CYR.otf"
define gui.interface_text_font = "Gismonda CYR.otf"
03 Fonts_replace_prferences.jpg04 Fonts_replace_game.jpg

Any idea why this is happening, how to fix it?
I've never done a font swap for a translation, but the method you are using doesn't seem the right one, perhaps is an obsolete one that worked some time ago. Now the proper way to do it is with styles. Take a look at this: https://www.renpy.org/doc/html/translat ... anslations
Comunidad Ren'Py en español (Discord)
Honest Critique

Post Reply

Who is online

Users browsing this forum: No registered users