ios & android: detect current language [SOLVED]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

ios & android: detect current language [SOLVED]

#1 Post by Jibus »

Is there a way to detect the current language on mobile device ?

My aim is to adapt the language of the visual novel according to the language of the device
Last edited by Jibus on Wed Jul 22, 2015 6:17 pm, edited 1 time in total.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Mobile devlopment : detect current language

#2 Post by PyTom »

I don't know of any. If you can find an android or iOS API that can do this, it's something I can add to Ren'Py.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#3 Post by Jibus »

In Python, I suppose ?

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#4 Post by Jibus »

ios :

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Can we mimic this with Pyobjus ?

android :

Locale.getDefault().getDisplayLanguage()

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#5 Post by Jibus »

I am trying to detect the current local with Pyobjus, but i am bit stuck.

Code: Select all

init python:
    if renpy.ios:
        import pyobjus
        NSLocale = pyobjus.autoclass("NSLocale")
        languages = NSLocale.preferredLanguages()
        current_language = languages[0]
preferredLanguages should return the user's language preference order as an array of strings.

But current_language seems to be empty

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Mobile devlopment : detect current language

#6 Post by PyTom »

Have you tried languages.objectAtIndex_(0) ?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#7 Post by Jibus »

Code: Select all

    if renpy.ios:
        import pyobjus
        NSLocale = pyobjus.autoclass("NSLocale")
        languages = NSLocale.preferredLanguages()
        current_language = str(languages.objectAtIndex_(0))
I got :

Code: Select all

___main___-____NSCFString object at 0x121ac4f50

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Mobile devlopment : detect current language

#8 Post by PyTom »

Maybe,

current_language = languages.objectAtIndex_(0).UTF8String().decode("utf-8")

will turn it into a unicode string properly.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#9 Post by Jibus »

Yes ! It's working ! Thanks you PyTom

Here is the full code to get the current language in ios

Code: Select all

    if renpy.ios:
        import pyobjus
        NSLocale = pyobjus.autoclass("NSLocale")
        languages = NSLocale.preferredLanguages()
        current_language = languages.objectAtIndex_(0).UTF8String().decode("utf-8")
I will now look for android and post the result in this thread

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: Mobile devlopment : detect current language

#10 Post by Jibus »

And here is the working code for android device :

Code: Select all

    if renpy.android:
        from jnius import autoclass
        Locale = autoclass('java.util.Locale')
        current_language = str(Locale.getDefault().getLanguage())

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: ios & android: detect current language [SOLVED]

#11 Post by PyTom »

Can I include this code in a future version of Ren'Py?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: ios & android: detect current language [SOLVED]

#12 Post by Jibus »

No need to ask :) Of course !

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: ios & android: detect current language [SOLVED]

#13 Post by Jibus »

Bump this thread. I notice that the code for ios doesn't work anymore in the latest release.

I was wondering if you have change something that may have impacted this ?

Thanks in advance !

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: ios & android: detect current language [SOLVED]

#14 Post by PyTom »

What's broken about it?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: ios & android: detect current language [SOLVED]

#15 Post by Jibus »

Found it!

It seems that iOS returns now the language and the region, ie: en-US.

Updated code:

Code: Select all

init python:
    if renpy.ios:
        import pyobjus
        NSLocale = pyobjus.autoclass("NSLocale")
        languages = NSLocale.preferredLanguages()
        current_language_region = languages.objectAtIndex_(0).UTF8String().decode("utf-8")
        current_language = current_language_region.split("-")
current_language[0] contains the language code and current_language[1] the region code.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], GetOutOfMyLab