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
pyPat
Regular
Posts: 30
Joined: Wed Sep 11, 2019 5:34 pm
Github: patlol
Location: France
Contact:

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

#31 Post by pyPat »

Hello,

Thank you for this tuto it's good thing for a newbie. :D
But there is something I don't understand about the common.rpy and screen.rpy files generated by the ren py translation function.

If you have an explanation it would be nice! See the post
viewtopic.php?f=8&t=56918#p519307
English is not my native language; please excuse typing errors.

User avatar
Shocking Alberto
Regular
Posts: 35
Joined: Sun Mar 29, 2020 4:58 am
Contact:

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

#32 Post by Shocking Alberto »

Morhighan wrote: Sat Apr 01, 2017 4:50 am 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'm actually stuck on how to implement an achievement for switching languages.

I have my game setup where you can toggle the languages from within preferences. So far so good.

screens.rpy

Code: Select all

            frame:
                label _("Languages")

                hbox:
                    textbutton "日本語" action Language(None)
                    textbutton "English" action Language("english")
                    textbutton "简体中文" action Language("simpch")
                    textbutton "繁体中文" action Language("chinese")
Originally, some decision was made to make separate builds for each language and the following code in script.rpy was used:

Code: Select all

    if persistent.first_boot:        
        persistent.first_boot = False     
    
        renpy.run(SetPreferencesDefault)
        _preferences.language = "english"
        persistent.last_lang = _preferences.language

    grant_achievement("game_start")
    
        if persistent.last_lang != _preferences.language:
        grant_achievement("change_language")
    persistent.last_lang = _preferences.language
Fairly cut and dry. If it's the first time booting up then it uses defaults for preferences and sets language to English (English isn't the primary language but I'd like it to be default which is why it's defined in the code) and the persistent.last_lang is initially set to English. Thus when starting up in another language, the last bit of the code would see that _preferences.language is set to a different language and thus achievement get.

Now that a language toggle is being used, this code does work but it only works after changing the language in game then restarting so I'm wondering what would be a better solution that doesn't require the user to restart the game. I thought maybe I could add an if statement in screens.rpy perhaps?

User avatar
Shocking Alberto
Regular
Posts: 35
Joined: Sun Mar 29, 2020 4:58 am
Contact:

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

#33 Post by Shocking Alberto »

So I thought this through a bit more and realizing I don't really need too much logic to check for the language being toggled, maybe I could just modify screens.rpy to trigger the achievement.

Code: Select all

label _("Languages")

                hbox:
                    textbutton "日本語": 
                        action [ Language(None),
                        Function(grant_achievement, "change_language")]
                    # English is the default language so clicking it won't trigger the achievement.
                    textbutton "English" action Language("english")
                    textbutton "简体中文": 
                        action [ Language("simpch"),
                        Function(grant_achievement, "change_language")]
                    textbutton "繁体中文":
                        action Language("chinese"),
                        Function(grant_achievement, "change_language")]
The achievement is designed to trigger if it's anything but English so I figured maybe this would work? Alas I get the following error:

Code: Select all

File "game/screens.rpy", line 616: u'return' is not a keyword argument or valid child for the vbox statement.
    return Text("This is English.")
I feel like I'm very close to the solution... maybe?

Edit: And this is why I never could pass my old college programming classes. I apparently left off an open bracket on the last text button. Put that in and it works like a charm. '-')v

Post Reply

Who is online

Users browsing this forum: No registered users