Struggling with something that used to be easy

Hey all,

I’ve been away from my Corona workflows for so long, I’ve pretty much forgotten the most simple stuff. - I’m working with an inbuilt translation table for localising my game, but I’ve been trying to swap text objects for image objects… With no joy.

I made this work years ago, but I’ve completely forgotten how I did it. Just wondered if there’s anyone out there who can put me on track.

Here’s the basic code for the function.

An external file called translations, it looks lime this:

-- translations.lua local translations = { ["Hello"] = { ["en"] = "Hello", ["fr"] = "Salut", ["de"] = "Hallo", ["es"] = "Hola", ["it"] = "Ciao" } } return translations

I then require it into my ‘active’ code:

local translations = require("translations") local language = userDefinedLanguage or system.getPreference("ui", "language")  

 Then, as a text object, it’s implemented like this:

local myText = display.newText(translations["Hello"][language], 0, 0, native.systemFont, 16)

This is what I’m trying to achieve - - Table:

-- translations.lua local translations = { ["Hello"] = { ["en"] = "assets/Languages/en/startBt.png", ["fr"] = "assets/Languages/fr/startBt.png", ["de"] = "assets/Languages/de/startBt.png", ["es"] = "assets/Languages/es/startBt.png", ["it"] = "assets/Languages/it/startBt.png" } } return translations

The require code is obviously the same, but here’s my attempted implementation:

begin = display.newImageRect(translations["StartButton"][language]) begin .x = 512 begin .y = 300 begin .alpha = 1 begin:addEventListener( "tap", advance )

I’ve introduced the ‘begin’ asset as ‘local’ and included it into a display group… It doesn’t work.

The console error reads: /Users/Home/Desktop/GAME CODE/MyGame/menu.lua:103: display.newImageRect() bad argument #1: filename or image sheet expected, but got nil

Jun 09 11:42:21.856 ERROR: Runtime error
/Users/Home/Desktop/GAME CODE/MyGame/menu.lua:104: attempt to index upvalue ‘begin’ (a nil value)
stack traceback:

 

 

If anyone can help, I’d be grateful.

Your translations table says hello and not startbutton at the beginning so no source image file found? Or so it seems to me at first glance late at night :slight_smile:

Upvalue errors is often about scope butnwithoutnfull lua code with line numbers i cant tell… i see no reference to begin in the code.

Good spot Anaqim, but that’s not it, I just compiled it incorrectly in my example, it’s actually correct within my project.

Thanks for taking the time though.

Can i see the full code with line numbers?

Did you try to use ‘local begin=’ ? Im quite new myself but looks like you are starting to use begin as a global without declaring it first with like “global=nil” or something

I ment begin=nil Man typing on an ipad is not something i do often :slight_smile: Globals need to be declared with an initial value, even nil, before being used afaik. Besides that from what i understand, thesedays in corona it is all about using local variables and no globals. Let me know if it works:)

Hi. Yes, I declared it at the top of my script as a local, like:

local begin

Then i need to see the whole script with line numbers and not bits and pieces mate

If you want help then post your entire code.  Do compress translations{} into a single instance first. 

this is pointless code

local translations = {     ["Hello"] = { ["en"] = "assets/Languages/en/startBt.png", ["fr"] = "assets/Languages/fr/startBt.png", ["de"] = "assets/Languages/de/startBt.png", ["es"] = "assets/Languages/es/startBt.png", ["it"] = "assets/Languages/it/startBt.png" } } return translations

when you can easily use a language variable for image declarations like this

begin = display.newImageRect(group, "assets/Languages/"..language.."/startBt.png", 100, 100)

I’ll second the call for more code, as what you’ve provided isn’t enough to make a solid recommendation on. But I’d also check the value of ‘language’ that you’re supplying. For example, if the language is being returned by the call to system.getPreference(), then it could be returning “en-US”, which isn’t accounted for in your translations table, and therefore calling translations["StartButton"][language] would return nil.

I suspect that there’s either a typo at play, or that your language variable isn’t matching one of your pre-defined options. But again, without more code it’s impossible to say for sure.

You could also set up a default language (I’d assume English) by modifying your setting of the language variable thusly:

local language = userDefinedLanguage or system.getPreference("ui", "language") if not translations["Hello"][language] then language = "en" end

Or perhaps more appropriately, define your available languages within your translations module:

translations.languages = {"en", "fr", "de", "es", "it"}

And then poll against that master list to determine if a default should be applied:

local language = userDefinedLanguage or system.getPreference("ui", "language") if not translations.languages[language] then language = "en" end

Good luck - and share more code so we can help you truly get to the bottom of this! :slight_smile:

According to post system.getPreference( “locale”, “country” ) - What exactly does this return?

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

in Lua was designed to return a language code on all platforms, not a localized language name. I believe what you use translations table as keys for example ‘en’ or ‘fr’ (two character country codes).

Note that some language codes have two parts with a separator (ZH-HANT = Traditional Chinese) . On Android this separator is the _, on iOS the separator will be a -.

Locale will be set to a language code that is consistent between platforms. Japanese will be JA for example. For languages with dialects you will get two part language codes like “ZH-HANS” (iOS), android will be “ZH_HANS”. I recommend testing this out in the Android & iOS XCode Simulator to be sure (Make sure to test languages like Chinese).

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

in Lua is supposed to return a localized language name. The idea is it is something you can display in your UI to the end-user.

let’s take it one step at a time, you could have OTHER problems complicating this before you even get to your language table issue…

going back to your original posted code, does even just THIS work with a hardcoded path?

local test = display.newImageRect("assets/Languages/en/startBt.png") print("test = ", test) -- hoping for non-nil

my guess is not, because the width/height parameters are required.  so how about THIS as a second test:

local test = display.newImageRect("assets/Languages/en/startBt.png", 100, 100) print("test = ", test) -- hoping for non-nil

did that work?  if not, then perhaps case-sensitive path or filename is at fault.

at any rate, once you’ve got it working with hardcoded values, THEN you can start applying any/all of the previous good suggestions re “validating” your language lookup and such.

Your translations table says hello and not startbutton at the beginning so no source image file found? Or so it seems to me at first glance late at night :slight_smile:

Upvalue errors is often about scope butnwithoutnfull lua code with line numbers i cant tell… i see no reference to begin in the code.

Good spot Anaqim, but that’s not it, I just compiled it incorrectly in my example, it’s actually correct within my project.

Thanks for taking the time though.

Can i see the full code with line numbers?

Did you try to use ‘local begin=’ ? Im quite new myself but looks like you are starting to use begin as a global without declaring it first with like “global=nil” or something

I ment begin=nil Man typing on an ipad is not something i do often :slight_smile: Globals need to be declared with an initial value, even nil, before being used afaik. Besides that from what i understand, thesedays in corona it is all about using local variables and no globals. Let me know if it works:)

Hi. Yes, I declared it at the top of my script as a local, like:

local begin

Then i need to see the whole script with line numbers and not bits and pieces mate

If you want help then post your entire code.  Do compress translations{} into a single instance first. 

this is pointless code

local translations = {     ["Hello"] = { ["en"] = "assets/Languages/en/startBt.png", ["fr"] = "assets/Languages/fr/startBt.png", ["de"] = "assets/Languages/de/startBt.png", ["es"] = "assets/Languages/es/startBt.png", ["it"] = "assets/Languages/it/startBt.png" } } return translations

when you can easily use a language variable for image declarations like this

begin = display.newImageRect(group, "assets/Languages/"..language.."/startBt.png", 100, 100)