Jump to content
  • 0

How do I write Multiple languages scripts?


El_dorado

Question

6 answers to this question

Recommended Posts

  • Administrators

Several options are possible.
For example, a table or function.
Most of all I like the option with the function, because it allows you to keep lines with different translations side by side.
Depending on the approach, the code can be very different.

local lang = 'ru'
local strs = {
	hello = 'Hello',
	hello_ru = 'Привет',
}
local function lng(index) 
	local ret = strs[index..'_'..lang]
	if not ret then ret = strs[index] end
	return ret
end

print(lng('hello'))

lang = 'id'

print(lng('hello'))

If you not like mess with quotes every time you can use metatable:

local lang = 'ru'
local strs = {
	hello = 'Hello',
	hello_ru = 'Привет',
}
local lng = setmetatable({}, {__index = function (tbl, index)
	local ret = strs[index..'_'..lang]
	if not ret then ret = strs[index] end
	return ret
end})

print(lng.hello)

lang = 'id'

print(lng.hello)

 

Link to comment
Share on other sites

  • Administrators

You can also use functions with parameters to specify strings right where they are used:

local lang = 1 -- 1 for first lang, 2 for second and so on, first lang as default lang if no specified lang found
local function lng(...)
	local list = {...}
	local ret = list[lang]
	if not ret and lang ~= 1 then ret = list[1] end
	return ret
end

print(lng('Hello', 'Привет'))

lang = 2

print(lng('Hello', 'Привет'))

lang = 20

print(lng('Hello', 'Привет'))

 

Link to comment
Share on other sites

Like seriously I don't understand any of this 😢. Does this display multiple languages (like English, Spanish, Arabic etc.) for users of the script? I write my scripts only in English, and the user may understand only Spanish, how can he change the English to Spanish? 

[added 0 minutes later]
21 hours ago, Enyby said:

You can also use functions with parameters to specify strings right where they are used:


local lang = 1 -- 1 for first lang, 2 for second and so on, first lang as default lang if no specified lang found
local function lng(...)
	local list = {...}
	local ret = list[lang]
	if not ret and lang ~= 1 then ret = list[1] end
	return ret
end

print(lng('Hello', 'Привет'))

lang = 2

print(lng('Hello', 'Привет'))

lang = 20

print(lng('Hello', 'Привет'))

 

@Enyby sir

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.