Vendetta/babel/hybrid example

From Quasar-RCE
Jump to navigationJump to search

This is an example of how a hybrid plugin can be designed to optionally support the Babel library.

--[[
[modreg]
id=bhybridtest
version=1.0.0
name=Babel hybrid example 
author=Luxen
]]--

--public class table to contain entry points to other LME plugins
local public = {}

--function called to edit functions and strings, and push table to LME
local update_public

--babel values for plugin
local babel, lang_key
local bstr = function(id, val)
	return val
end

--LME check and execution
if type(lib) == "table" and lib[0] == "LME" then
	--register plugin to LME
	local plugin_path = "plugins/test/"
	local plugin_main = plugin_path .. "main.lua"
	
	lib.register(plugin_main)
	
	--halt execution if plugin is disabled in LME
	if not lib.is_ready(plugin_main) then
		return --the plugin is disabled, stop here
	end
	
	--create and/or update plugin's common content description
	update_class = function()
		local class = {
			CCD1 = true,
			smart_config = {
				title = "Babel hybrid example",
				action = function() end,
				description_label = {
					type = "text",
					display = bstr(1, "This is an example hybrid plugin demonstrating Babel usage"),
				},
				"description_label",
			},
			description = bstr(1, "This is an example hybrid plugin demonstrating Babel usage"),
			commands = {},
			manifest = {
				"plugins/dummy/main.lua",
				"plugins/dummy/lang/en.ini",
				"plugins/dummy/lang/es.ini",
				"plugins/dummy/lang/fr.ini",
				"plugins/dummy/lang/pt.ini",
			},
		}
		
		for k, v in pairs(class) do
			public[k] = v
		end
		
		lib.set_class("bhybridtest", "1.0.0")
	end
	
	--update Babel functionality now
	local babel_func = function()
		babel = lib.get_class("babel", "0")
		lang_key = babel.register(plugin_path .. "lang/", {'en', 'es', 'fr', 'pt'})
		
		bstr = function(id, val)
			return babel.fetch(lang_key, id, val)
		end
		
		public.add_translation = function(path, lang_code)
			babel.add_new_lang(lang_key, path, lang_code)
		end
		
		update_public()
	end
	
	lib.require({{name = "babel", version = "0"}}, babel_func)
	
	--[[
		Any LME-specific code here
	]]--
	
	--OR call this at the end of the plugin inside its own LME check
	lib.set_class("bhybridtest", "1.0.0")
end

--[[
	Your plugin here
]]--