Jump to content

Recommended Posts

-- in modinfo.lua, make this configuration or some equivalent
	{
		name = "LANGUAGE",
		label = "Language/语言",
		hover = "",
		options = {
			{ description =    "Default", data = false },
			{ description =  "English", data = 1 },
			{ description = "中文", data = 2 },
		},
		default = false,
	},

My secret way of making config easier

Spoiler
-- Setting up global constants (They're all stored in TUNING)
local modinfo = KnownModIndex:GetModInfo(modname)
local MODNAME = modinfo.name or "UNKNOWN MOD"
TUNING[modname] =
{
	CONFIG = {},
	NAME = MODNAME,
	NAMESTAMP = "["..MODNAME.."] ",
}
if not modinfo.configuration_options then return end
for _,option in pairs(modinfo.configuration_options) do
	if option.name:sub(1,5) ~= "Label" then
		local setting = GetModConfigData(option.name)
		if type(setting) == "string" and setting:sub(1,4) == "KEY_" then
			setting = _G[setting]
		end
		TUNING[modname].CONFIG[option.name] = setting
	end
end

 

-- in modmain.lua or whenever your language starts mattering (like strings.lua)
local CONFIG = TUNING[modname].CONFIG or {}

local CONFIG.LANGUAGE = CONFIG.LANGUAGE or GetModConfigData"LANGUAGE"
local LANGUAGE = Profile:GetLanguageID()
local _mod = function(...) return KnownModIndex:IsModEnabled(...) end
if not CONFIG.LANGUAGE then	
	if _mod"workshop-367546858" then -- Check whether a translation workshop mod is enabled. you need its ID. (commonly used language mods are in the settings)
		-- set language to that mod (in this case chinese)
	elseif LANGUAGE == 0 then -- Check what supported language is enabled
		-- English
	elseif LANGUAGE == 22 then
		-- Simplified chinese
	elseif LANGUAGE == 21 then
		-- Traditional chinese
	else
		-- No language found? Make language English (or whatever makes the most sense for your mod).
	end
elseif CONFIG.LANGUAGE == 1 then
	-- set language to english
elseif CONFIG.LANGUAGE == 2 then
	-- set language to other langauge
end

you can use a table structure and go through the data in pairs if you have a lot of data to check,but for shorter sets of data elseif chains are more readable imo. and haven't tested this code though, so if it crashes for some unforeseen reason it's that. but this is supposed to work.

Also have seen mods use LanguageTranslator.defaultlang for language detection.

If you want to change the Mod Configuration strings, the only way I know of currently is would look something like this. You may need to put restart_required in modinfo.Client mods and server mods are handled differently in config, but I don't know if this method effects that. The mod has to be enabled in order for the changes to be made, otherwise the strings will be whatever you put in modinfo.

Spoiler
local new_config =
{
	{
		label = "New Label",
	},
	{
		label = "New Label",
		hover = "New hover",
		options =
		{
			{description = "New description", hover = "newhover"},
			{description = "New da", hover = "newhover"},
		},
	},
	{
		label = "New Label",
		hover = "New hover",
		options =
		{
			{description = "New description", hover = "newhover"},
			{description = "New da", hover = "newhover"},
		},
	},
}

local LoadConfig = GLOBAL.KnownModIndex.LoadModConfigurationOptions
local ourmodname = modname
GLOBAL.KnownModIndex.LoadModConfigurationOptions = function(self, modname, ...)
	local result = LoadConfig(self, modname, ...)
	if modname == ourmodname and type(result) == "table" then
		return GLOBAL.MergeMapsDeep(result, new_config)
	end
	return result
end

 

Edited by oregu
  • Like 1
1 hour ago, Rickzzs said:

use 'locale'

type:string

value: '','zh','ru','jp',...

source:loc.lua

I had no clue a function like ChooseTranslationTable existed in modinfo! And LOC.GetLocaleCode() always returns something, so it might be the intended way to get language. Nice

Spoiler
-- modinfo.lua
configuration_options = {}
local _ctt = ChooseTranslationTable
local function Option(desc, val, hov, notranslate)
	local t
  	if not notranslate then
    	t = function(tbl, hov) 
      		if not hov then
        		return _ctt(tbl["DESC"])
        	else
        		return _ctt(tbl["HOV"]) 
      		end
      	end
    else
    	t = function(str) return str end
    end
  	return 
	{
		description = t(desc) or "",
		data = val or false,
		hover = t(hov, true) or "",
	}
end


local labelcount = 0
local function Config(name, label, options, default, desc)
	local is_header
	if not options then 
		is_header = true
		labelcount = labelcount + 1 
	end
	configuration_options[#configuration_options + 1] =
	{
		name = label and name or "Label"..labelcount.."_"..(name or "Unnamed"),
		label = _ctt(label) or name or "Label"..labelcount.."_"..(name or "Untitled"),
		hover = _ctt(desc) or "",
		options = options or {Option()},
		default = default or false,
		is_header = is_header,
	}
end

local CODENAME =
{
  NAME = 
  	{
      "English", -- English/default language is [1]
      ["zh"] = "Yeah",
      ["zhr"] = "Yah",
    },
  DESC =
  	{
    	"I'm a desc",
    	["zh"] = "Thing",
    	["zhr"] = "Th",
    },
  OPT_1 =
  {
    DESC =
    {
      "I'm a desc",
      ["zh"] = "Thing",
      ["zhr"] = "Th",
    },
    HOV = 
    {
      "I'm a desc",
      ["zh"] = "Thing",
      ["zhr"] = "Th",
    },
   },
}
Config("CODENAME", CODENAME.NAME, {Option(CODENAME.OPT_1, true, CODENAME.OPT_1), Option()}, CODENAME.DESC)

-- Then add the next translation table and Config function, and so on. You can probably automate this more but that's up to you.

 

This is probably how I would handle using ChooseTranslationTable

How the function works is that it takes a table as an input and returns an index at LOC.GetLocaleCode()

 

If you want an example of a mod that uses this, this is the only one I had that does it.

Edited by oregu
  • Like 1

Thank you very much for helping!
I used ChooseTranslationTable in modinfo.lua and that get the job done.

I hope the following example will make sence for others who might visit this thread

ChooseTranslationTable(Table): 
Return a value in Table using current language code as key, return Table[1] as default

Example 1:

-- Current language code: "zh"
local tbl = {
  "123",
  zh = "456"
}

>>> ChooseTranslationTable(tbl)
>>> "456"

Example 2:

-- Current language code: "ru"

>>> ChooseTranslationTable({
    "English",
    zh = "Chinese"
  })
>>> "English"
Edited by HuangET

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...