Formatting Number Based on Language

Here is my spreadsheet:

afaf

I have removed the Context column. Does not seem to have caused any adverse effects.

Here is the code located in player scripts:

local translator =     game:GetService("LocalizationService"):GetTranslatorForPlayer(game.Players.LocalPlayer)

function translate(key,...)
	local args = {...}
	local success, result = pcall(function() 
		return translator:FormatByKey(key,args[1],args[2],args[3])
	end)
	if success then
		return result
	else
		spawn(function()
			error(result)
		end)
		return key
	end
end

print(translate("Number",1000))

The code results in this error when running in either Spanish or English language mode:
dea

Should print “1,000” when running in English mode and “1.000” when running in Spanish

1 Like

Are you sure it imported correctly? If so, try calling LocalizationTable:GetEntries() and print the results?

Also think the arguments for the FormatByKey method have to be passed as a table, but not entirely sure. Should look like this instead:

return translator:FormatByKey(key,args)
4 Likes

You were right. FormatByKey accepts tables as the second argument.

Looks as though a decimal value is automatically appended to the formatted number. Does this mean I have to use string.sub on a per language basis to remove it?

1 Like

Yeah the Context column is just for the automatic text replacement system so you can drop that safely.

You do have an error in your script, but the error message is very wrong. Just tested this and reproduced it. I’ll make sure this is fixed soon.

The second argument to FormatByKey should be a table, so it should be translator:FormatByKey(key, args). If you change that it should work.

1 Like

Looks as though a decimal value is automatically appended to the formatted number. Does this mean I have to use string.sub on a per language basis to remove it?

This seems like a question better suited for you to answer.

Also for now unfortunately yeah. Precision specifiers were cut from the original spec. Hopefully we can add them soon.

For all languages we support now the decimal point can be either . or , depending on the target language. The precision for num is exactly 2. Should be safe to just slice off the last three characters for now. Pains me to say it, but I don’t think there’s a better way at the moment.

3 Likes

Thanks, slicing off the decimals won’t be a problem