These days I've been working on a translation of a Ren'Py game, and wondered if it would be possible to make the game set automatically the proper language for the user, as I knew this is a system setting.
Almost all computer systems have a language setting that uses a convention you've probably seen somewhere, like "en_US" or "fr_FR". That string contains the language preferred by the system and in wich country variation, if any: "en_US" means "english for the USA", "en_GB" means "english for Great Britain" and so on. These codes are specified by two ISO standards (look at the end of this post for more information).
Let's imagine I'm a german player who hardly knows english, and who has downloaded a game wich is available in english and german -amongst other languages maybe-. I can't set the language to german if the game speaks english by default. Well, this code should solve it:
Code: Select all
init -3 python:
import locale
# Keep in mind that the language is only set the first time the game is played!
if persistent.lang is None:
# First, obtain the full locale. getdefaultlocale() returns some other fields we don't need:
miau = locale.getdefaultlocale()[0]
# Now get the value for each field
iso_lang = miau[0:2]
iso_ctry = miau[3:5]
# You must modify these lines in order to make available only the languages implemented:
if iso_lang == "es":
persistent.lang = "spanish"
elif iso_lang == "de":
persistent.lang = "german"
else:
persistent.lang = "english"
lang = persistent.lang
For country variations:
Code: Select all
if iso_lang == "es":
if iso_ctry == "AR":
persistent.lang = "argentinian"
elif iso_ctry == "MX":
persistent.lang = "mexican"
else:
persistent.lang = "spanish"
elif iso_lang == "de":
persistent.lang = "german"
else:
persistent.lang = "english"
Maybe a faster (and a more technical) approach would be to use the locale in the game instead of translating it (i.e. using "es" instead of "spanish"), but there's still some work to be done if we have any country variation.
This code has been tested under Windows only, but it should also work under Linux and Mac. Any brave tester around?
Standards ISO-639-1 or ISO-639-2 details two letter codes for every language (values for iso_lang in the examples above), and ISO-3166-1 details two-letter codes for every country (values for iso_ctry); you can use your favourite search engine to get a full list of both of them.