New line doesn't work from database - bug or not?

Hi,

New line function doesn’t seems to work when I try to read my localization data from the database?

local function translate(key, code)  
 for row in dblogic:nrows("SELECT eng, sv from translation where key='" .. key .."'") do  
  
 if code == "sv" then  
 return row.sv  
 elseif code == "en" then  
 return row.eng  
 end  
 end  
end  
  
local txtInfo = translate("Hello world","sv")  
--txtInfo = "This is a line\n with linefeed"  
  
local txtInfo = display.newRetinaText( txtInfo, 135, 430, 352, 400, "Gill Sans", 18 )  

Hardcoded within the page it works as it should? Is this a bug or did I missed something?

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 322447[/import]

have you tried printing row.sv or row.eng to see whether it is a database problem or not? [import]uid: 24641 topic_id: 22447 reply_id: 89522[/import]

HI, it looks exactly like the hard printed code? Is this a bug or is there anything else I can do?

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 89652[/import]

Ansca staff!

What should I do now, we are in the end of development and the game is soon to be released. Localization from a database doesn’t work when i have \n \n as row breaks.

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111498[/import]

Hi Joakim,
I believe that “NewRetinaText” has been totally depreciated and should no longer be used. Of course, using the core “NewText” might produce the same result you’re seeing. Still, worth a try…

Brent [import]uid: 9747 topic_id: 22447 reply_id: 111504[/import]

Yepp, I am using newText and it doesn’t work.

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111505[/import]

Hmmm, I know this might seem “desperate”, but if you’re ready to market soon, maybe worth a shot…

Could you possibly “re-process” the database string into another string, instead of using it directly? For example, take the entire string, then do “string.gmatch” operations on it (looking for lines), and then add/append those lines to a new Lua string. I’ve had some issues in the past where new lines “\n” *especially* don’t play well when they come over from various sources, including FTP (that was my latest hurdle). It’s not necessarily a “bug” but rather, I suspect, some kind of encoding issue or the database/FTP not translating the \n operator correctly.

Brent

[import]uid: 9747 topic_id: 22447 reply_id: 111508[/import]

Hmmm, I am terrible bad on patterns and match. You don’t have and patterns laying around for no purpose :wink:

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111528[/import]

Hi Joakim,
In your code you have this line…

local txtInfo = translate("Hello world","sv")

Can you quickly show me one example of the “print( txtInfo )” value (meaning, the translated value that is coming from the database) so I can see what is being passed to the newText API? I imagine it looks something like “Line1\nLine2” but perhaps not. If I see the result that it’s pulling from the database, maybe we can figure out a workaround solution. :slight_smile:

Brent [import]uid: 9747 topic_id: 22447 reply_id: 111531[/import]

Great Brent,

Here is the function that queries the database.

  
require "sqlite3";  
local path = system.pathForFile("gameLogic.db",nil);  
dblogic = sqlite3.open( path );  
  
local function translate(key, code)  
 for row in dblogic:nrows("SELECT eng, sv from translation where key='" .. key .."'") do  
  
 if code == "sv" then  
 return row.sv  
 elseif code == "en" then  
 return row.eng  
 end  
 end  
end  
  

and here is one row that is fetched:

Unlock or download more animals and fun Game. \n \nYour support helps us make more fun things for your child to play with. \n \nEnjoy!
Thanks, Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111542[/import]

Hi Joakim,

Out of curiosity, would you be willing to split the “lines” into separate newText elements, using string search functions, with each line spaced downward by a Y parameter you specify? Or would that be too much trouble or memory usage for your app?

I’m going to experiment real quick with re-processing the string to see what happens, but if that doesn’t work, the above method would be a relatively simple alternative, code-wise.

Brent
[import]uid: 9747 topic_id: 22447 reply_id: 111544[/import]

Hmm…if its possible to split them out by the \n character - wouldn’t it be easier to rebuild a lua string? One solution could maybe be to use another sign for row break in the database.

For example: This line needs § a rowbreak?

Its maybe easier to search for the § sign and replace it by pure lua code, to a:

This line needs \n a row break?

The strange thing is that I tried to find the \n character but I just got nil as a result? There is something real strange going on here from sqLite…

Joakim
[import]uid: 81188 topic_id: 22447 reply_id: 111547[/import]

Did you mention (initially) that if you hard-code the string (not necessarily pulled from the database), you are getting properly line-breaked text using newText()? I just tried a quick hard-coded line, and I’m not getting any line breaks at all. :frowning:

Brent
[import]uid: 9747 topic_id: 22447 reply_id: 111549[/import]

Ahhh, I think that you have to put in a fixed with and height - to get it to work:

  
-- Display row breaks  
  
display.newText( "Unlock or download more animals and fun Game. \n \nYour support helps us make more fun things for your child to play with.\n \nEnjoy!", 100,100,200,500,18)  
  

Note from API documentation:

Build 2011.640, added multiple lines support to display.newText and display.newRetinaText. This is enabled by specifying the width and height when the object is created.

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111550[/import]

OK, I managed to get it to work with a quick fix. Instead of using \n as row break in the database content, I used § as code for row breaks.

I just added this line to my function:

  
string.gsub(myText, "§", "\n \n");   
  

and then I got my rowbreaks! Thanks for pointing me out and supporting my tired brain :wink:

Joakim [import]uid: 81188 topic_id: 22447 reply_id: 111553[/import]

Joakim,

This is a SQLite limitation and not a bug in Corona or SQLite. If you search the Internet for “SQLite new line character”, then you’ll find that the rest of the world has this problem too. I agree that it’s really inconvenient because no other database I’ve used (SQL Server, MySQL, Sybase, etc.) has this limitation. I suspect sqlite has this problem because its database file is really a text file, so it has to replace all new line and carriage return characters with “\n” and “\r” string literals before saving to the database file… and it doesn’t bother to convert back that new lines and carriage returns when you query the strings. The nerve, right?!

So, a quick solution would be to do the string replacement yourself when you receive strings from the database. You can do this via the standard Lua fucntion string.gsub(). It would look something like this…
[lua]myString = string.gsub(myString, “\n”, “\n”)
myString = string.gsub(myString, “\r”, “\r”)[/lua]

So, the above will handle the “\n” and the “\r” that SQLite is converting your characters too, but realize that this isn’t a perfect solution. Because what if the end-user of your app types in “\n” somewhere in your text box? Something like “yes\no” for some legitimate reason. In this case, the above solution would wrongly replace the “\n” part of “yes\no” with a new line character. In this case, you would want to replace the new line character with your own “character code”, for example “{0x0A}”, before you INSERT/UPDATE it in SQLite. That is, with a string code that is reasonably unique that you would never expect the end-user to ever type in. It’s not a bullet proof solution because the end-user could type in those exact character code strings to circumvent your control character replacement system, but unless your concerned about system security this solution is probably good enough. You may not like the full bullet proof solution that I’m thinking of. :slight_smile: [import]uid: 32256 topic_id: 22447 reply_id: 111554[/import]

Awesome! Thanks for clearing it up Joshua. And great to hear it’s finally working for you Joakim. :slight_smile:

Brent [import]uid: 9747 topic_id: 22447 reply_id: 111555[/import]

Thanks Joshua,

Glad to hear that it isn’t a Corona bug. For my case I am only using the database for localization so the user never enters any data to my db so I am safe with this solution.

Thank you guys!

Joakim - almost there!
[import]uid: 81188 topic_id: 22447 reply_id: 111561[/import]