Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if shell then
- local tArgs = {...}
- local apiName = fs.getName(shell.getRunningProgram())
- assert(#tArgs > 0, "Usage:\n"..apiName.."<function> [arguments]")
- getfenv()[apiName][tArgs[1]](select(2, unpack(tArgs)))
- else
- local translations = {}
- local curLang = "en_US"
- local path = "lang:rom/lang" -- just in case there's a resource pack with lang files
- local function checkString(s)
- if type(s) ~= "string" then
- error("Expected string", 3) -- level three to return to user source; only called from within this API
- end
- end
- function setString(langType, key, value)
- checkString(langType)
- checkString(key)
- checkString(value)
- if not translations[langType] then
- translations[langType] = {}
- end
- translations[langType][key] = value
- end
- function removeString(langType, key)
- checkString(langType)
- checkString(key)
- if translations[langType] then
- translations[langType][key] = nil
- end
- end
- function removeStringForAll(key)
- checkString(key)
- for k,v in pairs(translations) do
- removeString(k, key)
- end
- end
- function getString(key)
- if not translations[curLang] or not translations[curLang][key] then
- return key
- end
- return translations[curLang][key]
- end
- function setPath(sPath)
- checkString(sPath)
- path = sPath
- end
- function appendPath(sPath)
- checkString(sPath)
- end
- function getPath()
- return path
- end
- function getCurrentLanguage()
- return curLang
- end
- function setCurrentLanguage(sCurLang)
- checkString(sCurLang)
- curLang = sCurLang
- end
- function reloadLanguageFiles()
- for sPath in string.gmatch(path, "[^:]+") do
- if fs.isDir(sPath) then
- for i,v in ipairs(fs.list(sPath)) do
- local filePath = fs.combine(sPath, v)
- local langName = (v:sub(-5) == ".lang") and v:sub(1, -6) or v
- if fs.exists(filePath) and not fs.isDir(filePath) then
- local fileHandle = assert(fs.open(filePath, "r"), "Error reading language file: " .. filePath)
- local fileContents = fileHandle.readAll()
- fileHandle.close()
- for key,val in fileContents:gmatch("([^%s=]+)%s*=%s*([^\n]+)\n?") do
- setString(langName, key, val)
- end
- end
- end
- end
- end
- end
- reloadLanguageFiles()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement