Function to filter character works in simulator but not in android device

Hi everyone, I’m programming a multilingual game and I decided to make different txt files for each language. when the game start after choose one language will be loaded the specific txt file, in this file every lines is separated by a “#” that is filtered by a function. My problem is that in the simulator all works great but when I make the apk and I install the game in my tablet the “#” is not filtered and appear in the main text like a line of text. Here my code, I hope someone can help me thanks!

string = composer.getVariable( "stringa" )
	
local path = system.pathForFile( string )

	-- Open the file handle
local file, errorString = io.open( path, "r" )

	
if not file then
-- Error occurred; output the cause
	print( "File error: " .. errorString )
else
	-- Read data from file

	lines = {}
	-- io.lines returns an iterator, so we need to manually unpack it into an array
	for line in io.lines(path) do
		lines[#lines+1] = line
	end	
	for i = 1, #lines do
		if lines[i] == "#" then
		  table.remove(lines, i)
		 end
	end
		
	contents = lines[1]

	print( "Contents of " .. path .. "\n" .. contents)
	for i,v in ipairs(lines) do print(i,v) end
		-- Close the file handle
	io.close( file )
end

txt file structure:

some text in first line
-#
some other text in second line
-#
some other text in third line

You should not have a variable that has the same name as the API. Essentially you’ve wiped out the string library.

You can store your translations in a table in lua and that would mean you don’t need txt files.

1 Like

Hi @anon63346430 thank you for your reply.
You are right about variable name, now I’ve changed the name in “stringPath” but unfortunately
i still have the same problem. I don’t understand why in the simulator it’s all ok and in the device I still see # in the text

When removing items from a table you must iterate the table backwards.

for i = #lines, 1, -1 do
    if lines[i] == "#" then table.remove(lines, i) end
end

thanks, I tried but same story… I guess that is a problem of character, looks like that for some reason the “#” is not recognized by android system and so it can’t remove from table

I would just recommend that you follow SGS’s original advice and write your translations inside Lua (or JSON) tables. That way you can just use a single require or json.decode call to get all of your translations without needing to play around with strings at all.

A similar question was also asked in Discord a few weeks ago and here’s my response from there:


I don’t think there’s a best practice, but translations can be handled quite easily. You could, for instance, create your own simple module like this:

local translations = {}

local activeLanguage

function translations.setLanguage( language )
    activeLanguage = {}

    -- Get all strings from the translations configuration file.
    local data = require( "configs.translations" )
    for string, languages in pairs( data ) do
        activeLanguage[string] = languages[language] or "[" .. string .. " not translated in " .. language .. "]"
    end
    -- Free up the module from memory.
    _G.package.loaded["configs.translations"] = nil
    data = nil
end

function translations.get( string )
    return activeLanguage[string] or "[" .. string .. " does not exist in the translations table]"
end

return translations

On the good practices side, that would have built-in error handling and clear error messages. On start, you’d just require the module and call translations.setLanguage( language ) with your desired language, like “en” and then you’d get the strings using translations.get( string ) . The actual translations config file could just be a lua file that’s written like:

return {

    ["hello"] = {
        ["en"] = "Hello!",
        ["fr"] = "Bonjour!",
    },

}

You could further expand the module by making it only load certain predetermined languages, or you could make it so that if a requested language doesn’t exist or a string doesn’t exist in a specific language then it defaults to english, etc.


With that approach, it assumes that you have a single translations.lua file placed inside a “configs” folder. If you wanted to put each language in their own file, then you wouldn’t necessarily require the other module. It’d still be a good idea to use something like the translations.get() as it would prevent you from getting crashes caused by undefined translations.

1 Like

thank you @XeduR , I’ll try to do like you said :wink:
however I’m still curious to understand why there are these problems when I try the app in a device

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.