Guest User

transplate.lua 1.,1

a guest
Feb 18th, 2023
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.76 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.     local loadGraphics = true
  11.    
  12.     -- littleDialogue
  13.     local littleDialogue_loadFonts = true
  14.     local littleDialogue_loadAnswers = true
  15.     local littleDialogue_loadSpeakerNames = true
  16.    
  17.     -- pauseMenu by Marioman2007
  18.     local pauseMenu_loadFonts = true
  19.  
  20.     -- local restmenu_loadFont = true
  21. --]]
  22.  
  23. local currentLanguage = nil
  24. local langs = {}
  25. local graphicsCache = {}
  26.  
  27. local fonts = {}
  28. local fontsCache = {}
  29.  
  30. local pauseMenuFonts = {}
  31. local pauseMenuFontCache
  32. local pauseMenuOptionsCache = {}
  33.  
  34. local textplus = require("textplus")
  35.  
  36. local littleDialogue
  37. local littleDialogueAnswers = {}
  38. local littleDialogueSpeakerNames = {}
  39.  
  40. pcall(function() littleDialogue = require("littleDialogue") end)
  41.  
  42. local pauseMenu
  43. pcall(function() pauseMenu = require("pauseMenu") end)
  44.  
  45. -- local restmenuGUI
  46. -- pcall(function() restmenuGUI = require("restmenuGUI") end)
  47.  
  48. do
  49.     local function unpackTabledStrings(langFile)
  50.         for original, new in pairs(langFile) do
  51.             if type(original) == 'table' then
  52.                 for _, original in ipairs(original) do
  53.                     langFile[original] = new
  54.                 end
  55.                
  56.                 langFile[original] = nil
  57.             end
  58.         end
  59.     end
  60.    
  61.     local littleDialogueFonts
  62.     local pauseMenuFontsChange
  63.  
  64.     do
  65.         pauseMenuFontsChange = function(langName)
  66.             if not pauseMenu or not pauseMenu_loadFonts then return end
  67.  
  68.             pauseMenuFonts[langName] = {}
  69.  
  70.             local _dir = path .. [[/]] .. langName .. [[/pauseMenu]]
  71.             local _path = Misc.resolveDirectory(_dir)
  72.  
  73.             if not _path then return end
  74.  
  75.             pauseMenuFonts[langName] = textplus.loadFont(_dir .. [[/font.ini]])
  76.         end
  77.     end
  78.  
  79.     do
  80.         local function loadFont(langName, _dir, style)
  81.             fonts[langName][style] = textplus.loadFont(_dir)
  82.         end
  83.        
  84.         littleDialogueFonts = function(langName)
  85.             if not littleDialogue or not littleDialogue_loadFonts then return end
  86.            
  87.             fonts[langName] = {}
  88.            
  89.             local _dir = path .. [[/]] .. langName .. [[/littleDialogue]]
  90.             local _path = Misc.resolveDirectory(_dir)
  91.             -- Misc.dialog(_path)
  92.            
  93.             if not _path then return end
  94.            
  95.             local dirs = Misc.listDirectories(_path)
  96.            
  97.             for _, style in ipairs(dirs) do
  98.                 fonts[langName][style] = textplus.loadFont(_dir .. [[/]] .. style .. [[/font.ini]])
  99.             end
  100.            
  101.             local mainFontPath = _dir .. [[/font.ini]]
  102.            
  103.             if Misc.resolveFile(mainFontPath) then
  104.                 fonts[langName][1] = textplus.loadFont(mainFontPath)
  105.             end
  106.         end
  107.     end
  108.    
  109.     function transplate.loadLanguage(langName)
  110.         langs[langName] = {}
  111.        
  112.         local langFile = require(path .. [[/]] .. langName .. [[/lang]])
  113.         unpackTabledStrings(langFile)
  114.  
  115.         littleDialogueFonts(langName)
  116.         pauseMenuFontsChange(langName)
  117.  
  118.         langs[langName].strings = langFile
  119.  
  120.         if preCache then
  121.             SaveData._transplateCache = langs
  122.         end
  123.     end
  124. end
  125.  
  126. function transplate.getLanguage()
  127.     return currentLanguage
  128. end
  129.  
  130. do
  131.     local function littleDialogue_changeFonts(new)
  132.         if not littleDialogue or not littleDialogue_loadFonts then return end
  133.        
  134.         if currentLanguage and fonts[currentLanguage] then -- updating ld style fonts
  135.             for name, settings in pairs(littleDialogue.styles) do
  136.                 local font = fonts[currentLanguage][name]
  137.                
  138.                 if not fontsCache[name] then
  139.                     fontsCache[name] = settings.font
  140.                 end
  141.                
  142.                 if font then
  143.                     settings.font = font
  144.                 else
  145.                     settings.font = fonts[currentLanguage][1] or settings.font
  146.                 end
  147.             end
  148.         elseif not currentLanguage then
  149.             for name, settings in pairs(littleDialogue.styles) do
  150.                 settings.font = fontsCache[name] or settings.font
  151.             end
  152.         end
  153.     end
  154.    
  155.     local function pauseMenu_changeFont(new)
  156.         if not pauseMenu or not pauseMenu_loadFonts then return end
  157.  
  158.         if currentLanguage and pauseMenuFonts[currentLanguage] then
  159.             if not pauseMenuFontCache then
  160.                 pauseMenuFontCache = pauseMenu.pauseFont
  161.             end
  162.  
  163.             pauseMenu.pauseFont = pauseMenuFonts[currentLanguage]
  164.  
  165.             -- change names of options
  166.             for id, config in ipairs(pauseMenu.options) do
  167.                 if not pauseMenuOptionsCache[config] then
  168.                     pauseMenuOptionsCache[config] = config.name
  169.                 end
  170.  
  171.                 config.name = transplate.getTranslation(pauseMenuOptionsCache[config])
  172.             end
  173.         elseif not currentLanguage then
  174.             pauseMenu.pauseFont = pauseMenuFontCache or pauseMenu.pauseFont
  175.  
  176.             -- change names of options back
  177.             for id, config in ipairs(pauseMenu.options) do
  178.                 config.name = pauseMenuOptionsCache[config] or config.name
  179.             end
  180.         end
  181.     end
  182.  
  183.     local function littleDialogue_changeAnswers(new)
  184.         if not littleDialogue or not littleDialogue_loadAnswers then return end
  185.        
  186.         if currentLanguage then
  187.             for answer, origData in pairs(littleDialogueAnswers) do
  188.                 answer.text = transplate.getTranslation(origData.text)
  189.                 answer.addText = transplate.getTranslation(origData.addText)
  190.             end
  191.            
  192.             return
  193.         end
  194.        
  195.         for answer, origData in pairs(littleDialogueAnswers) do
  196.             answer.text = origData.text
  197.             answer.addText = origData.addText
  198.         end
  199.     end
  200.  
  201.     local function graphics(new)
  202.         if not loadGraphics or new == nil then
  203.             for graphicsType, v in pairs(graphicsCache) do
  204.                 for graphicsId, img in pairs(v) do
  205.                     Graphics.sprites[graphicsType][graphicsId].img = img
  206.                 end
  207.             end
  208.            
  209.             return
  210.         end
  211.        
  212.         local graphicsFolder = Misc.resolveDirectory(transplate.getLanguagePath() .. "/graphics")
  213.        
  214.         if not graphicsFolder then return end
  215.        
  216.         local types = Misc.listDirectories(graphicsFolder)
  217.        
  218.         for _,type in ipairs(types) do
  219.             local fullPath = graphicsFolder .. "/" .. type
  220.            
  221.             for k,graphicPath in ipairs(Misc.listFiles(fullPath)) do
  222.                 local img = Graphics.loadImage(fullPath .. "/" .. graphicPath)
  223.                 local name = graphicPath:match("(.+)%..+")
  224.                
  225.                 local graphicType, graphicId = name:match("(%w+)-(.+)")
  226.                
  227.                 if tonumber(graphicId) then graphicId = tonumber(graphicId) end
  228.                
  229.                 graphicsCache[graphicType] = graphicsCache[graphicType] or {}
  230.                 graphicsCache[graphicType][graphicId] = graphicsCache[graphicType][graphicId] or Graphics.sprites[graphicType][graphicId].img
  231.                
  232.                 Graphics.sprites[graphicType][graphicId].img = img
  233.             end
  234.         end
  235.     end
  236.    
  237.     function transplate.setLanguage(new)
  238.         currentLanguage = new
  239.        
  240.         graphics(new)
  241.         littleDialogue_changeFonts(new)
  242.         littleDialogue_changeAnswers(new)
  243.         pauseMenu_changeFont(new)
  244.    
  245.         if saveLanguage then
  246.             SaveData._transplateLang = currentLanguage
  247.         end
  248.     end
  249. end
  250.  
  251. function transplate.getLanguagePath()
  252.     if currentLanguage == nil then return "" end
  253.    
  254.     return (path .. "/" .. currentLanguage)
  255. end
  256.  
  257. function transplate.resolveFile(path)
  258.     local ret = Misc.resolveFile(transplate.getLanguagePath() .. "/" .. path)
  259.    
  260.     if ret == nil then
  261.         return Misc.resolveFile(path)
  262.     end
  263.    
  264.     return ret
  265. end
  266.  
  267. function transplate.getTranslation(text, lang)
  268.     if not currentLanguage then return text end
  269.     local lang = langs[lang or currentLanguage]
  270.    
  271.     if not lang then return text end
  272.    
  273.     return lang.strings[text] or text
  274. end
  275.  
  276. function transplate.setTranslation(text, new, lang)
  277.     if not currentLanguage then return end
  278.  
  279.     langs[lang or currentLanguage].strings[text] = new
  280. end
  281.  
  282. function transplate.onInitAPI()
  283.     registerEvent(transplate, 'onStart')
  284. end
  285.  
  286. function transplate.onStart()
  287.     if saveLanguage then
  288.         transplate.setLanguage(SaveData._transplateLang)
  289.     end
  290.    
  291.     if preCache and SaveData._transplateCache then
  292.         langs = SaveData._transplateCache
  293.         return
  294.     end
  295.    
  296.     if not preLoad then return end
  297.    
  298.     local _path = Misc.resolveDirectory(path)
  299.    
  300.     if not _path then return end
  301.    
  302.     local dirs = Misc.listDirectories(_path)
  303.    
  304.     for _, langName in ipairs(dirs) do
  305.         transplate.loadLanguage(langName)
  306.     end
  307. end
  308.  
  309. -- littleDialogue implementation
  310. if littleDialogue then
  311.     local onMessageBox = littleDialogue.onMessageBox
  312.    
  313.     littleDialogue.onMessageBox = function(e, msg, p, v)
  314.         return onMessageBox(e, transplate.getTranslation(msg), p, v)
  315.     end
  316.    
  317.     local registerAnswer = littleDialogue.registerAnswer
  318.    
  319.     littleDialogue.registerAnswer = function(name,  answer)
  320.         littleDialogueAnswers[answer] = {text = answer.text, addText = answer.addText}
  321.        
  322.         return registerAnswer(name, answer)
  323.     end
  324.    
  325.     local getPortraitData = littleDialogue.getPortraitData
  326.    
  327.     littleDialogue.getPortraitData = function(name)
  328.         local data = getPortraitData(name)
  329.        
  330.         if not littleDialogueSpeakerNames[data] then
  331.             littleDialogueSpeakerNames[data] = data.name
  332.         end
  333.        
  334.         data.name = transplate.getTranslation(littleDialogueSpeakerNames[data])
  335.        
  336.         return data
  337.     end
  338.    
  339.     local customTags = littleDialogue.customTags
  340.     local speakerName = customTags.speakerName
  341.    
  342.     customTags.speakerName = function(fmt,out,args)
  343.         local args = args
  344.         args[1] = transplate.getTranslation(args[1])
  345.        
  346.         return speakerName(fmt, out, args)
  347.     end
  348. end
  349.  
  350. return transplate
Advertisement
Add Comment
Please, Sign In to add comment