Page 1 of 3

Implementing Translations in Ren'Py [Updated Tutorial]

Posted: Sat Apr 01, 2017 4:50 am
by Morhighan
Image
Tutorial on Itch.io
Note: As of May 29,2019 I will focus any future updates on the itch.io page rather than this post.
Introduction
In this tutorial I'll be discussing how to add a language option to your Ren'Py game. This is solely for the implementation of a translation, NOT to generate a translation from the script. There is no automated translation button built into Ren'Py, and I highly recommend against using machine translation for official game releases, as the practice is highly frowned upon in.
When working for Afterthought Studios, I worked with a team of translators to make the Russian translations of Afterthoughi's games. I then implemented the translations into the scripts. However, the process can be confusing when you only do it once every six-to-twelve months or so. Thus I made a guide for myself some time back to avoid confusion. I've decided to share and update the tutorial for anyone who might find it useful.

Step One

To get a basic understanding of the translation framework built into Ren'Py, I recommend reading this Documentation page.

Once you have your game's script completed in the original language (I'll be using English in the example), you will want to generate a blank script for translation into another language (Russian, in this example.) To do this, you will select the project in The Ren'py Launcher and go to the Actions menu, where you will click "Generate Translations."

Image

This will take you to a second menu, in which you will input the name of the language you are generating scripts for. Be sure to use all lowercase letters.


Image

Now press "Generate Translation," making sure to have the box next to 'generate empty strings for translations' checked.

Image

Ren'Py will generate blank files for your project, which can take a few moments depending on how large your project is. Once it's done you'll find the blank scripts for translation in your project's "game" folder, like so:

Code: Select all

[Your Game]/game/tl/russian
Image

Your files will be ready for the translation to be input. This will be done manually. The team we worked with input the translation into the script as they worked, so everything could be matched up and double checked. Some programmers receive translations via spreadsheets, some scripters input the translations themselves. It all depends on what you/your team and the translator's preferences are.

Here's what one of the scripts looks like before the translation is filled in.

Image

You can see that the contents of original script (in this case, the English text) is available in comments. The lines above the comments are what allows Ren'py to replace the English line with the Russian line, when the game is played in Russian. Below the comment is an empty dialogue line with the appropriate character label defined, which will eventually be filled with the translation.

Code: Select all

# game/script.rpy <--file from which the lines are taken
translate language script_#######:  #<--string for replacing original script with translation
# n "Commented version of line from original script."
n "Translated version of line."
Here is what the same script looks like after the translation is implemented:

Image

!! Important !!

Be sure that any strings like the following

Code: Select all

translate russian script1_f978b7f5:
match the capitalization of your game folders. If the folder is "/game/tl/russian" make sure "russian" is always lowercase, or you might get weird issues. At one point I had an error in which only the menus appeared translated, but all of the text was still in English. It was because of a mismatch in capitalization.

Another issue that sometimes crops up has to do with how characters are defined. When Ren'Py converts a script into an empty script to be filled with translations, it will automatically convert some lines that won't be translated in the same manner as the rest of the script.

For example, the script we generated for Forgotten, Not Lost included the character definitions, which our translators naturally translated.

Code: Select all

# game/script.rpy:79
translate russian start_3989d14d:
    #define mad1 = Character("Madalene")
    define mad1 = Character("Мадалена")
But this caused the translated names to override the names in English, no matter which language you played the game in, due to how Ren'Py works. To prevent this, you must do the following:

Code: Select all

define e = Character(_('Eileen'), color="#c8ffc8")
You will find this in the scripts of the Russian translation:

Code: Select all

translate russian strings: 
    old "Eileen" 
    new "Эйлин"
In the end, our Russian Strings looked like this:

Image

and the error was resolved.

Now that the above is done, you should have a functioning translated script. Be sure that your UI is also translated. If you use imagemaps for your GUI, you'll want to have translated versions of any UI images and add them to your translated file folder, like so:

Code: Select all

[Your Game]/game/tl/russian/images
Now, you'll need a way to allow your players to select the language they want to play in. For Afterthought Studio's games, I had the option pop up in a splash screen upon opening the game. If the player has already made their selection, the splash screen won't pop up again.

To do this, I used the following code in options.rpy:

Code: Select all

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
Please note that not all fonts will be compatible with a given language, so you may need to change the font depending on the language selection.

If you want players to be able to choose the language upon opening the game, put the following in script.rpy:

Code: Select all

label language_chooser: 
    scene black 
    menu: 
        "{font=Arimo-Regular.ttf}English{/font}": $ renpy.change_language(None) 
        "{font=Arimo-Regular.ttf}Русский{/font}": $ renpy.change_language("russian")
    $ renpy.utter_restart() #alternatively, use "return" if you don't want to go to the main menu.
And in case your players want to switch languages after starting the game, put the following in your imagemap for the Main Menu or Options Menu for screens.rpy:

Code: Select all

hotspot(#,#,#,#) action ShowMenu("language_chooser") #insert coordinates appropriately
This is how I've implemented translations in the past. As Ren'Py updates, I'll try to improve on this tutorial as I am able to. I will focus any updates on the itch.io page rather than this post.

This code is compatible with SteamAPI, so you can use it in conjunction with achievements and such. (ie. having an achievement unlock when players change the language.)

I hope this helps anyone who might need it. If you have any questions, please feel free to leave a comment and I will try to get back to you.

If you found this helpful at all, please consider leaving me a token of your appreciation, if you are able to.

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Sat Apr 01, 2017 7:53 am
by karorogunso
Also.. you can use this code with SteamAPI too. :D

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Sat Apr 08, 2017 3:45 pm
by Morhighan
karorogunso wrote:Also.. you can use this code with SteamAPI too. :D
Good to know/remember that! :D

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Wed Jun 14, 2017 6:15 am
by Ertal77
Morhighan wrote: Put the following in script.rpy:
(You can use this label before the game starts so players can choose the language immediately.)

Code: Select all


label language_chooser:
    scene black
    
    menu:
        "{font=Arimo-Regular.ttf}English{/font}":
            $ renpy.change_language(None)
        "{font=Arimo-Regular.ttf}Русский{/font}":
            $ renpy.change_language("russian")

    $ renpy.utter_restart()

Thank you so much for this tutorial, it's very clear and useful!
Just a question, though: how do I change the "$ renpy.utter_restart()" part so it goes to my "label start" directly and skips the splashscreen?

*EDIT Nevermind, I changed it for "return" and now it leads me directly back to the game instead of the main menu, but I guess that works fine, too.

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Tue Jul 11, 2017 10:56 pm
by karorogunso
Ertal77 wrote:
Morhighan wrote: Put the following in script.rpy:
(You can use this label before the game starts so players can choose the language immediately.)

Code: Select all


label language_chooser:
    scene black
    
    menu:
        "{font=Arimo-Regular.ttf}English{/font}":
            $ renpy.change_language(None)
        "{font=Arimo-Regular.ttf}Русский{/font}":
            $ renpy.change_language("russian")

    $ renpy.utter_restart()

Thank you so much for this tutorial, it's very clear and useful!
Just a question, though: how do I change the "$ renpy.utter_restart()" part so it goes to my "label start" directly and skips the splashscreen?

*EDIT Nevermind, I changed it for "return" and now it leads me directly back to the game instead of the main menu, but I guess that works fine, too.
Yes return is fine too.

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Sun Aug 06, 2017 2:02 am
by Kinross07
Unfortunately I don't speak any other language well enough to know myself, how good do the translations tend to be? Is it just utilising something like Google Translate where it makes sense 98% of the time but sometimes completely falls apart?

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Thu Oct 12, 2017 4:55 am
by Morhighan
Kinross07 wrote: Sun Aug 06, 2017 2:02 am Unfortunately I don't speak any other language well enough to know myself, how good do the translations tend to be? Is it just utilising something like Google Translate where it makes sense 98% of the time but sometimes completely falls apart?
So we hired a team to manually translate the script and then programmed it using the method above. I don't think Ren'Py actually translates anything itself. Though it would be nice.

Hope that helps! :)

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Thu Oct 12, 2017 7:34 am
by Zelan
This is absolutely fantastic, thank you so much, Morhigan! ^_^

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Fri Aug 03, 2018 4:46 am
by 017Bluefield
Having a bit of trouble employing the hotspot()? I don't really have an imagemap to use it with...

Code: Select all

    hotspot(500,500,400,200) action ShowMenu("language_chooser")

Code: Select all

While running game code:
  File "game/screens.rpy", line 391, in execute
    screen main_menu():
  File "game/screens.rpy", line 391, in execute
    screen main_menu():
IndexError: list index out of range

Re: [Tutorial] Creating Translations for Ren'Py Games

Posted: Sat Oct 27, 2018 9:41 am
by fearlessor
Hello
I want to do the translation. I've created files using Renpy. I even made the language menu on the opening screen. But how do I guide? I do not know. What's missing?

Code: Select all

            if _return_button:
                textbutton _("RETURN") action Return()

            if main_menu:
                textbutton _("START") action Start()
            if main_menu:
                textbutton _("Language") action ShowMenu()
            else:
                #textbutton _("History") action ShowMenu("history")
                textbutton _("SAVE") action ShowMenu("save")

            textbutton _("LOAD") action ShowMenu("load")

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

Posted: Wed May 29, 2019 7:46 pm
by Morhighan
Updated first post to include the improved tutorial and a link to itch.io version, which will be the location of any future updates.

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

Posted: Mon Jun 03, 2019 2:56 am
by Ayael
Thank you very much for this tutorial, it's really great ! I was thinking about it, and pop you update it, wonderful ♥

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

Posted: Mon Jun 03, 2019 5:37 pm
by richycapy
Hi, is there a way to change the lenguaje using a imagebutton ?

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

Posted: Mon Jun 03, 2019 8:25 pm
by Andredron
richycapy wrote: Mon Jun 03, 2019 5:37 pm Hi, is there a way to change the lenguaje using a imagebutton ?
imagebutton..... action.....

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

Posted: Mon Jun 03, 2019 8:40 pm
by richycapy
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 Language(None)
        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?