Jump to content
El_dorado

How do I write Multiple languages scripts?

Recommended Posts

How do I write scripts that change languages for users using internet connection? Cos it's not possible to write a script with many languages, it'll be so stressful 

Share this post


Link to post
Share on other sites

No one to answer this? 😥

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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', 'Привет'))

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...