How to translate strings generated dynamically by a script?

I’m starting the process of translating my game.
I already know how to manually launch translations via the website (Introduction to Localization).
But I didn’t understand how exactly I could do the translation of strings generated within my script.

For example, the script below will print 3 lines over 3 fruits (in Portuguese - the game’s original language):

local Fruits = {
	{Name = 'Batata', Amount = 1},
	{Name = 'Banana', Amount = 2},
	{Name = 'Tomate', Amount = 3},
}
for Id, Fruit in ipairs(Fruits) do
	local Plural = Fruit.Amount > 1 and 's' or ''
	print('Eu tenho ' .. Fruit.Amount .. ' ' .. Fruit.Name .. Plural)
end

It will be printed (in Portuguese):

Eu tenho 1 Batata
Eu tenho 2 Bananas
Eu tenho 3 Tomates

The English translation should be:

I have 1 Potato
I have 2 Bananas
I have 3 Tomatoes

So we have the first fixed part Eu tenho (“I have”) , then an amount and finally the name of the fruit, which can be followed by an ‘s’ at the end, to simulate the plural in Portuguese (which when translating into English would add es to the end).

I have dozens of parts inside my script with dynamically generated strings.

How then will translation be done in this case?

I am unsure if there’s a more efficient way to do it but consider reviewing this:
https://developer.roblox.com/en-us/api-reference/function/LocalizationService/GetCountryRegionForPlayerAsync

1 Like

Actually, my question is simpler (although the example was a little more complex):

How to translate strings generated dynamically by a script?

Could look into Google’s ‘Translate API’, new customers receive $300 worth of credits for free.

1 Like