Page 1 of 1

ios & android: detect current language [SOLVED]

Posted: Wed Jul 15, 2015 5:12 pm
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

Re: Mobile devlopment : detect current language

Posted: Wed Jul 15, 2015 11:17 pm
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.

Re: Mobile devlopment : detect current language

Posted: Thu Jul 16, 2015 9:53 am
by Jibus
In Python, I suppose ?

Re: Mobile devlopment : detect current language

Posted: Sat Jul 18, 2015 8:48 pm
by Jibus
ios :

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

android :

Locale.getDefault().getDisplayLanguage()

Re: Mobile devlopment : detect current language

Posted: Mon Jul 20, 2015 4:32 pm
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

Re: Mobile devlopment : detect current language

Posted: Mon Jul 20, 2015 4:35 pm
by PyTom
Have you tried languages.objectAtIndex_(0) ?

Re: Mobile devlopment : detect current language

Posted: Mon Jul 20, 2015 5:12 pm
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

Re: Mobile devlopment : detect current language

Posted: Mon Jul 20, 2015 6:34 pm
by PyTom
Maybe,

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

will turn it into a unicode string properly.

Re: Mobile devlopment : detect current language

Posted: Tue Jul 21, 2015 5:45 pm
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

Re: Mobile devlopment : detect current language

Posted: Wed Jul 22, 2015 6:17 pm
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())

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

Posted: Wed Jul 22, 2015 6:59 pm
by PyTom
Can I include this code in a future version of Ren'Py?

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

Posted: Thu Jul 23, 2015 9:21 am
by Jibus
No need to ask :) Of course !

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

Posted: Fri Sep 25, 2015 5:07 am
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 !

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

Posted: Sat Sep 26, 2015 1:29 am
by PyTom
What's broken about it?

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

Posted: Mon Sep 28, 2015 4:03 am
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.