Advertisement
ElvishJerricco

Language API

Oct 12th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. if shell then
  2.     local tArgs = {...}
  3.     local apiName = fs.getName(shell.getRunningProgram())
  4.     assert(#tArgs > 0, "Usage:\n"..apiName.."<function> [arguments]")
  5.     getfenv()[apiName][tArgs[1]](select(2, unpack(tArgs)))
  6. else
  7.     local translations = {}
  8.     local curLang = "en_US"
  9.     local path = "lang:rom/lang" -- just in case there's a resource pack with lang files
  10.  
  11.     local function checkString(s)
  12.         if type(s) ~= "string" then
  13.             error("Expected string", 3) -- level three to return to user source; only called from within this API
  14.         end
  15.     end
  16.     function setString(langType, key, value)
  17.         checkString(langType)
  18.         checkString(key)
  19.         checkString(value)
  20.         if not translations[langType] then
  21.             translations[langType] = {}
  22.         end
  23.  
  24.         translations[langType][key] = value
  25.     end
  26.  
  27.     function removeString(langType, key)
  28.         checkString(langType)
  29.         checkString(key)
  30.         if translations[langType] then
  31.             translations[langType][key] = nil
  32.         end
  33.     end
  34.  
  35.     function removeStringForAll(key)
  36.         checkString(key)
  37.         for k,v in pairs(translations) do
  38.             removeString(k, key)
  39.         end
  40.     end
  41.  
  42.     function getString(key)
  43.         if not translations[curLang] or not translations[curLang][key] then
  44.             return key
  45.         end
  46.         return translations[curLang][key]
  47.     end
  48.  
  49.     function setPath(sPath)
  50.         checkString(sPath)
  51.         path = sPath
  52.     end
  53.  
  54.     function appendPath(sPath)
  55.         checkString(sPath)
  56.     end
  57.  
  58.     function getPath()
  59.         return path
  60.     end
  61.  
  62.     function getCurrentLanguage()
  63.         return curLang
  64.     end
  65.  
  66.     function setCurrentLanguage(sCurLang)
  67.         checkString(sCurLang)
  68.         curLang = sCurLang
  69.     end
  70.  
  71.     function reloadLanguageFiles()
  72.         for sPath in string.gmatch(path, "[^:]+") do
  73.             if fs.isDir(sPath) then
  74.                 for i,v in ipairs(fs.list(sPath)) do
  75.                     local filePath = fs.combine(sPath, v)
  76.                     local langName = (v:sub(-5) == ".lang") and v:sub(1, -6) or v
  77.                     if fs.exists(filePath) and not fs.isDir(filePath) then
  78.                         local fileHandle = assert(fs.open(filePath, "r"), "Error reading language file: " .. filePath)
  79.                         local fileContents = fileHandle.readAll()
  80.                         fileHandle.close()
  81.                         for key,val in fileContents:gmatch("([^%s=]+)%s*=%s*([^\n]+)\n?") do
  82.                             setString(langName, key, val)
  83.                         end
  84.                     end
  85.                 end
  86.             end
  87.         end
  88.     end
  89.  
  90.     reloadLanguageFiles()
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement