Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Easy text translation — Gideros Forum

Easy text translation

SinisterSoftSinisterSoft Maintainer
edited August 2017 in Code snippets
Whilst making RetroStar I decided to improve my text translation routines. I think some people might like to use them for their own games...

First I made a player with the Sqlite plugin.

I then downloaded DB Browser for SQLite (free, http://sqlitebrowser.org/ )

With that program I created a database table that was called 'languages'. I had multiple TEXT fields based on the language code with the first field called 'en' (my native language) with the attributes TEXT NOT NULL UNIQUE. I also made it the primary key.

I added the all the text from my game to new records with the English (en) column filled in. Various friends gave me text for the other columns. To add a new language I just add another field to the table.

I've attached 2 screenshots from the database to help.

Here is a snip from my game code:
local translated={}
 
pcall(function() require "lsqlite3" end)
src_db="retrostar.db"
dst_db="|T|retrostar.db"
if file_exists(dst_db) then
	file_delete(dst_db)
end
file_copy(src_db,dst_db)
 
if sqlite3 then
	local db=sqlite3.open(dst_db,sqlite3.OPEN_READONLY)
	if db then
		local sql=db:execute("PRAGMA temp_store=2;")
 
		osLanguage=lower(application:getLanguage())
		--osLanguage="se"
 
		if osLanguage~="en" then
			local found=false
			local sql=db:prepare("PRAGMA table_info(languages);")
			if sql then
				for n,l in sql:urows() do
					if l==osLanguage then
						found=true
						break
					end
				end
			end
 
			if found then
				local sql=db:prepare("SELECT en,"..osLanguage.." FROM languages;")
				if sql then
					for w,t in sql:urows() do
						if t and t~="" then
							t=gsub(t,"ę","e")	-- replace letters our font doesn't have
							t=gsub(t,"’","'")
							t=gsub(t,"…","...")
						end
						translated[w]=t
					end
				end
			end
		end
 
		db:close()
	end
end
 
local function translate(t)
	return translated[t] or t
end
File functions:
function file_copy(src,dst)
	local srcf=io.open(src,"rb")
	if srcf then
		local dstf=io.open(dst,"wb")
		if dstf then
			local size=8*1024
			while true do
				local block=srcf:read(size)
				if not block then break end
				dstf:write(block)
			end
			dstf:close()
		end
		srcf:close()
	end
end
 
--to check if file exists
function file_exists(name)
   local f=io.open(name,"rb")
   if f==nil then return false end
   f:close()
   return true
end
 
--to delete a file
function file_delete(name)
	if file_exists(name) then
		os.remove(name)
	end
end
The routine gets the native language of the player, and if not English (you can change this to your native language) then it loads the field (if any) that matches the language and loads in all the text. Whilst loading it check the text to make sure that I'm not using any characters not in my font.

The translate function is handy function to automatically translate to text in the other language. eg:
txt=TextField(myFont,translate("Start Game")
You can test a language out by uncommenting:
--osLanguage="se"
and replacing "se" with the language code you want to test.


database1.png
1166 x 276 - 22K
database2.png
802 x 376 - 23K
Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+3 / -0 )Share on Facebook
Sign In or Register to comment.