Guest User

transplate.lua

a guest
Nov 17th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1. local transplate = {}
  2.  
  3. local path = "transplate"
  4.  
  5. --[[settings]]
  6.     -- main
  7.     local preCache = false
  8.     local preLoad = true
  9.     local saveLanguage = false
  10.  
  11.     -- littleDialogue
  12.     local littleDialogue_loadFonts = true
  13.    
  14.     -- restmenu
  15.     -- local restmenu_loadFont = true
  16. --]]
  17.  
  18. local currentLanguage = nil
  19. local langs = {}
  20. local fonts = {}
  21. local fontsCache = {}
  22.  
  23. local textplus = require("textplus")
  24.  
  25. local littleDialogue
  26. pcall(function() littleDialogue = require("littleDialogue") end)
  27.  
  28. -- local restmenuGUI
  29. -- pcall(function() restmenuGUI = require("restmenuGUI") end)
  30.  
  31. do
  32.     local function unpackTabledStrings(langFile)
  33.         for original, new in pairs(langFile) do
  34.             if type(original) == 'table' then
  35.                 for _, original in ipairs(original) do
  36.                     langFile[original] = new
  37.                 end
  38.                
  39.                 langFile[original] = nil
  40.             end
  41.         end
  42.     end
  43.    
  44.     local littleDialogueFonts
  45.    
  46.     do
  47.         local function loadFont(langName, _dir, style)
  48.             fonts[langName][style] = textplus.loadFont(_dir)
  49.         end
  50.        
  51.         littleDialogueFonts = function(langName)
  52.             if not littleDialogue or not littleDialogue_loadFonts then return end
  53.            
  54.             fonts[langName] = {}
  55.            
  56.             local _dir = path .. [[/]] .. langName .. [[/littleDialogue]]
  57.             local _path = Misc.resolveDirectory(_dir)
  58.             -- Misc.dialog(_path)
  59.            
  60.             if not _path then return end
  61.            
  62.             local dirs = Misc.listDirectories(_path)
  63.            
  64.             for _, style in ipairs(dirs) do
  65.                 fonts[langName][style] = textplus.loadFont(_dir .. [[/]] .. style .. [[/font.ini]])
  66.             end
  67.            
  68.             local mainFontPath = _dir .. [[/font.ini]]
  69.            
  70.             if Misc.resolveFile(mainFontPath) then
  71.                 fonts[langName][1] = textplus.loadFont(mainFontPath)
  72.             end
  73.         end
  74.     end
  75.    
  76.     function transplate.loadLanguage(langName)
  77.         langs[langName] = {}
  78.        
  79.         local langFile = require(path .. [[/]] .. langName .. [[/lang]])
  80.         unpackTabledStrings(langFile)
  81.         littleDialogueFonts(langName)
  82.        
  83.         langs[langName].strings = langFile
  84.  
  85.         if preCache then
  86.             SaveData._transplateCache = langs
  87.         end
  88.     end
  89. end
  90.  
  91. function transplate.getLanguage()
  92.     return currentLanguage
  93. end
  94.  
  95. do
  96.     local function littleDialogue_changeFonts(new)
  97.         if not littleDialogue_loadFonts then return end
  98.        
  99.         if littleDialogue and currentLanguage and fonts[currentLanguage] then -- updating ld style fonts
  100.             for name, settings in pairs(littleDialogue.styles) do
  101.                 local font = fonts[currentLanguage][name]
  102.                
  103.                 if not fontsCache[name] then
  104.                     fontsCache[name] = settings.font
  105.                 end
  106.                
  107.                 if font then
  108.                     settings.font = font
  109.                 else
  110.                     settings.font = fonts[currentLanguage][1] or settings.font
  111.                 end
  112.             end
  113.         elseif littleDialogue and not currentLanguage then
  114.             for name, settings in pairs(littleDialogue.styles) do
  115.                 settings.font = fontsCache[name] or settings.font
  116.             end
  117.         end
  118.     end
  119.    
  120.     function transplate.setLanguage(new)
  121.         currentLanguage = new
  122.        
  123.         littleDialogue_changeFonts(new)
  124.        
  125.         if saveLanguage then
  126.             SaveData._transplateLang = currentLanguage
  127.         end
  128.     end
  129. end
  130.  
  131. function transplate.getTranslation(text, lang)
  132.     if not currentLanguage then return text end
  133.  
  134.     return langs[lang or currentLanguage].strings[text] or text
  135. end
  136.  
  137. function transplate.setTranslation(text, new, lang)
  138.     if not currentLanguage then return end
  139.  
  140.     langs[lang or currentLanguage].strings[text] = new
  141. end
  142.  
  143. function transplate.onInitAPI()
  144.     if saveLanguage then
  145.         transplate.setLanguage(SaveData._transplateLang)
  146.     end
  147.    
  148.     if preCache and SaveData._transplateCache then
  149.         langs = SaveData._transplateCache
  150.         return
  151.     end
  152.    
  153.     if not preLoad then return end
  154.    
  155.     local _path = Misc.resolveDirectory(path)
  156.    
  157.     if not _path then return end
  158.    
  159.     local dirs = Misc.listDirectories(_path)
  160.    
  161.     for _, langName in ipairs(dirs) do
  162.         transplate.loadLanguage(langName)
  163.     end
  164. end
  165.  
  166. -- littleDialogue implementation
  167. if littleDialogue then
  168.     local onMessageBox = littleDialogue.onMessageBox
  169.    
  170.     littleDialogue.onMessageBox = function(e, msg, p, v)
  171.         return onMessageBox(e, transplate.getTranslation(msg), p, v)
  172.     end
  173. end
  174.  
  175. return transplate
Advertisement
Add Comment
Please, Sign In to add comment