text translations not working on Android

Hi everyone…hope you can help me.

I’m new text translations with external translations.lua.

On Mac everything is fine, it works fine either on simulator

and or on devices…it changes texts accordind to device language etc…

BUT If I make an apk for Android I get a non working app.

I moved the project on windows simulator and I get and error
localizing text. Translation return “nil” instead of the right string.

print(translations[“lento”][language]) returns nil.

Is there any set up file I ve to do on android?

I used the basic example I found here:

http://www.monkeybin.com/blog/archives/2011/09/18/corona-sdk-localization-made-easy/

and I get the same problem. It returns nil.

 I’m using on both machine build 2076.

Anyone can help?  Do I need a special confing to localize for Android?

Have you looked at  your console log and looked for any errors?

It’s likely the string you are getting back from the OS for the langauge might not be what you’re expecting or possibly nil and causing problems.  I would as in some prints around how you are determining the local language.

If you don’t know how to read your console log please see:  http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Rob

Thank Rob for your kind reply!

Actually I understood that my problem was in the different LCID code on Android and iOS producing a nil value. I solved like this. I’m new with lua and there might be a better solution but it seems to work. Thks Marco

local language =  system.getPreference(“ui”, “language”)

if translations[“hello”][language]==nil then

    language = string.sub(system.getPreference(“locale”, “language”),1,2):lower()

end

if not (language == “it” or language == “fr” or language == “es”) then

    language = “en”

end 

On testing with a GalaxyTab 3 with 4.2.2 I found this:

system.getPreference("locale", "language")

Will return " en", while

system.getPreference("ui", "language")

will return " English"

That’s on the actual device. In the simulator, they both return " en"

So on Android devices, it’s apparently better to go with “locale” to get an ISO language id, and add in the substring/lower parts to be safe:

string.sub(system.getPreference("locale", "language"),1,2):lower()

It seems to work fine with “locale” on iOS, too.

yes, this is why I used the “if” clause in the code above, but instead of looking at the kind of device I try to translate a word and in case of failure I use “locale”. I did that bcs I can’t be sure of behaviour on all devices…

@m_locatelli, are you getting a 2 character language code returned?  Perhaps that is why you are getting a nil error?  

I ran the following on the simulator (Windows simulator, build 2013.2108)

[lua]

print (system.getPreference(“ui”, “language”))

print (system.getPreference(“locale”, “language”))

[/lua]

Notice the output I received for the first statement.

[lua]

Windows simulator build date: Dec 18 2013 @ 11:04:32

Copyright © 2009-2013  C o r o n a   L a b s   I n c .

        Version: 3.0.0

        Build: 2013.2108

1033

English (United States)

[/lua]

Anyone else get “1033” instead of “en”?  Is there a problem, or something I am doing wrong?

yes you’re right! This is why i solved the issue in this way as you can see in the post before.

local language =  system.getPreference(“ui”, “language”)

if translations[“hello”][language]==nil then

    language = string.sub(system.getPreference(“locale”, “language”),1,2):lower()

end

The app is now working good on all devices but I inserted a safety control just that in case of wrong translation. In that case it switches to “en”.

if not (language == “it” or language == “fr” or language == “es”) then

    language = “en”

end

Strange behavior indeed, and it looks like the code you posted does work around the issue.

I would be interested in knowing if anyone else gets a return of “1033” instead of “en”.  It happens to me on the simulator.  I did not get a chance to try it on a device yet.  Maybe someone at CL can check to see if this is something that needs to be addressed in a future build?

thanks!

ehi wait wait…the code I posted does work! :slight_smile: It is the workaroud to the initial problem!

Have you looked at  your console log and looked for any errors?

It’s likely the string you are getting back from the OS for the langauge might not be what you’re expecting or possibly nil and causing problems.  I would as in some prints around how you are determining the local language.

If you don’t know how to read your console log please see:  http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Rob

Thank Rob for your kind reply!

Actually I understood that my problem was in the different LCID code on Android and iOS producing a nil value. I solved like this. I’m new with lua and there might be a better solution but it seems to work. Thks Marco

local language =  system.getPreference(“ui”, “language”)

if translations[“hello”][language]==nil then

    language = string.sub(system.getPreference(“locale”, “language”),1,2):lower()

end

if not (language == “it” or language == “fr” or language == “es”) then

    language = “en”

end 

On testing with a GalaxyTab 3 with 4.2.2 I found this:

system.getPreference("locale", "language")

Will return " en", while

system.getPreference("ui", "language")

will return " English"

That’s on the actual device. In the simulator, they both return " en"

So on Android devices, it’s apparently better to go with “locale” to get an ISO language id, and add in the substring/lower parts to be safe:

string.sub(system.getPreference("locale", "language"),1,2):lower()

It seems to work fine with “locale” on iOS, too.

yes, this is why I used the “if” clause in the code above, but instead of looking at the kind of device I try to translate a word and in case of failure I use “locale”. I did that bcs I can’t be sure of behaviour on all devices…

@m_locatelli, are you getting a 2 character language code returned?  Perhaps that is why you are getting a nil error?  

I ran the following on the simulator (Windows simulator, build 2013.2108)

[lua]

print (system.getPreference(“ui”, “language”))

print (system.getPreference(“locale”, “language”))

[/lua]

Notice the output I received for the first statement.

[lua]

Windows simulator build date: Dec 18 2013 @ 11:04:32

Copyright © 2009-2013  C o r o n a   L a b s   I n c .

        Version: 3.0.0

        Build: 2013.2108

1033

English (United States)

[/lua]

Anyone else get “1033” instead of “en”?  Is there a problem, or something I am doing wrong?

yes you’re right! This is why i solved the issue in this way as you can see in the post before.

local language =  system.getPreference(“ui”, “language”)

if translations[“hello”][language]==nil then

    language = string.sub(system.getPreference(“locale”, “language”),1,2):lower()

end

The app is now working good on all devices but I inserted a safety control just that in case of wrong translation. In that case it switches to “en”.

if not (language == “it” or language == “fr” or language == “es”) then

    language = “en”

end

Strange behavior indeed, and it looks like the code you posted does work around the issue.

I would be interested in knowing if anyone else gets a return of “1033” instead of “en”.  It happens to me on the simulator.  I did not get a chance to try it on a device yet.  Maybe someone at CL can check to see if this is something that needs to be addressed in a future build?

thanks!

ehi wait wait…the code I posted does work! :slight_smile: It is the workaroud to the initial problem!