Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local transplate = {}
- local path = "transplate"
- --[[settings]]
- -- main
- local preCache = false
- local preLoad = true
- local saveLanguage = false
- local loadGraphics = true
- -- littleDialogue
- local littleDialogue_loadFonts = true
- local littleDialogue_loadAnswers = true
- local littleDialogue_loadSpeakerNames = true
- -- pauseMenu by Marioman2007
- local pauseMenu_loadFonts = true
- -- local restmenu_loadFont = true
- --]]
- local currentLanguage = nil
- local langs = {}
- local graphicsCache = {}
- local fonts = {}
- local fontsCache = {}
- local pauseMenuFonts = {}
- local pauseMenuFontCache
- local pauseMenuOptionsCache = {}
- local textplus = require("textplus")
- local littleDialogue
- local littleDialogueAnswers = {}
- local littleDialogueSpeakerNames = {}
- pcall(function() littleDialogue = require("littleDialogue") end)
- local pauseMenu
- pcall(function() pauseMenu = require("pauseMenu") end)
- -- local restmenuGUI
- -- pcall(function() restmenuGUI = require("restmenuGUI") end)
- do
- local function unpackTabledStrings(langFile)
- for original, new in pairs(langFile) do
- if type(original) == 'table' then
- for _, original in ipairs(original) do
- langFile[original] = new
- end
- langFile[original] = nil
- end
- end
- end
- local littleDialogueFonts
- local pauseMenuFontsChange
- do
- pauseMenuFontsChange = function(langName)
- if not pauseMenu or not pauseMenu_loadFonts then return end
- pauseMenuFonts[langName] = {}
- local _dir = path .. [[/]] .. langName .. [[/pauseMenu]]
- local _path = Misc.resolveDirectory(_dir)
- if not _path then return end
- pauseMenuFonts[langName] = textplus.loadFont(_dir .. [[/font.ini]])
- end
- end
- do
- local function loadFont(langName, _dir, style)
- fonts[langName][style] = textplus.loadFont(_dir)
- end
- littleDialogueFonts = function(langName)
- if not littleDialogue or not littleDialogue_loadFonts then return end
- fonts[langName] = {}
- local _dir = path .. [[/]] .. langName .. [[/littleDialogue]]
- local _path = Misc.resolveDirectory(_dir)
- -- Misc.dialog(_path)
- if not _path then return end
- local dirs = Misc.listDirectories(_path)
- for _, style in ipairs(dirs) do
- fonts[langName][style] = textplus.loadFont(_dir .. [[/]] .. style .. [[/font.ini]])
- end
- local mainFontPath = _dir .. [[/font.ini]]
- if Misc.resolveFile(mainFontPath) then
- fonts[langName][1] = textplus.loadFont(mainFontPath)
- end
- end
- end
- function transplate.loadLanguage(langName)
- langs[langName] = {}
- local langFile = require(path .. [[/]] .. langName .. [[/lang]])
- unpackTabledStrings(langFile)
- littleDialogueFonts(langName)
- pauseMenuFontsChange(langName)
- langs[langName].strings = langFile
- if preCache then
- SaveData._transplateCache = langs
- end
- end
- end
- function transplate.getLanguage()
- return currentLanguage
- end
- do
- local function littleDialogue_changeFonts(new)
- if not littleDialogue or not littleDialogue_loadFonts then return end
- if currentLanguage and fonts[currentLanguage] then -- updating ld style fonts
- for name, settings in pairs(littleDialogue.styles) do
- local font = fonts[currentLanguage][name]
- if not fontsCache[name] then
- fontsCache[name] = settings.font
- end
- if font then
- settings.font = font
- else
- settings.font = fonts[currentLanguage][1] or settings.font
- end
- end
- elseif not currentLanguage then
- for name, settings in pairs(littleDialogue.styles) do
- settings.font = fontsCache[name] or settings.font
- end
- end
- end
- local function pauseMenu_changeFont(new)
- if not pauseMenu or not pauseMenu_loadFonts then return end
- if currentLanguage and pauseMenuFonts[currentLanguage] then
- if not pauseMenuFontCache then
- pauseMenuFontCache = pauseMenu.pauseFont
- end
- pauseMenu.pauseFont = pauseMenuFonts[currentLanguage]
- -- change names of options
- for id, config in ipairs(pauseMenu.options) do
- if not pauseMenuOptionsCache[config] then
- pauseMenuOptionsCache[config] = config.name
- end
- config.name = transplate.getTranslation(pauseMenuOptionsCache[config])
- end
- elseif not currentLanguage then
- pauseMenu.pauseFont = pauseMenuFontCache or pauseMenu.pauseFont
- -- change names of options back
- for id, config in ipairs(pauseMenu.options) do
- config.name = pauseMenuOptionsCache[config] or config.name
- end
- end
- end
- local function littleDialogue_changeAnswers(new)
- if not littleDialogue or not littleDialogue_loadAnswers then return end
- if currentLanguage then
- for answer, origData in pairs(littleDialogueAnswers) do
- answer.text = transplate.getTranslation(origData.text)
- answer.addText = transplate.getTranslation(origData.addText)
- end
- return
- end
- for answer, origData in pairs(littleDialogueAnswers) do
- answer.text = origData.text
- answer.addText = origData.addText
- end
- end
- local function graphics(new)
- if not loadGraphics or new == nil then
- for graphicsType, v in pairs(graphicsCache) do
- for graphicsId, img in pairs(v) do
- Graphics.sprites[graphicsType][graphicsId].img = img
- end
- end
- return
- end
- local graphicsFolder = Misc.resolveDirectory(transplate.getLanguagePath() .. "/graphics")
- if not graphicsFolder then return end
- local types = Misc.listDirectories(graphicsFolder)
- for _,type in ipairs(types) do
- local fullPath = graphicsFolder .. "/" .. type
- for k,graphicPath in ipairs(Misc.listFiles(fullPath)) do
- local img = Graphics.loadImage(fullPath .. "/" .. graphicPath)
- local name = graphicPath:match("(.+)%..+")
- local graphicType, graphicId = name:match("(%w+)-(.+)")
- if tonumber(graphicId) then graphicId = tonumber(graphicId) end
- graphicsCache[graphicType] = graphicsCache[graphicType] or {}
- graphicsCache[graphicType][graphicId] = graphicsCache[graphicType][graphicId] or Graphics.sprites[graphicType][graphicId].img
- Graphics.sprites[graphicType][graphicId].img = img
- end
- end
- end
- function transplate.setLanguage(new)
- currentLanguage = new
- graphics(new)
- littleDialogue_changeFonts(new)
- littleDialogue_changeAnswers(new)
- pauseMenu_changeFont(new)
- if saveLanguage then
- SaveData._transplateLang = currentLanguage
- end
- end
- end
- function transplate.getLanguagePath()
- if currentLanguage == nil then return "" end
- return (path .. "/" .. currentLanguage)
- end
- function transplate.resolveFile(path)
- local ret = Misc.resolveFile(transplate.getLanguagePath() .. "/" .. path)
- if ret == nil then
- return Misc.resolveFile(path)
- end
- return ret
- end
- function transplate.getTranslation(text, lang)
- if not currentLanguage then return text end
- local lang = langs[lang or currentLanguage]
- if not lang then return text end
- return lang.strings[text] or text
- end
- function transplate.setTranslation(text, new, lang)
- if not currentLanguage then return end
- langs[lang or currentLanguage].strings[text] = new
- end
- function transplate.onInitAPI()
- registerEvent(transplate, 'onStart')
- end
- function transplate.onStart()
- if saveLanguage then
- transplate.setLanguage(SaveData._transplateLang)
- end
- if preCache and SaveData._transplateCache then
- langs = SaveData._transplateCache
- return
- end
- if not preLoad then return end
- local _path = Misc.resolveDirectory(path)
- if not _path then return end
- local dirs = Misc.listDirectories(_path)
- for _, langName in ipairs(dirs) do
- transplate.loadLanguage(langName)
- end
- end
- -- littleDialogue implementation
- if littleDialogue then
- local onMessageBox = littleDialogue.onMessageBox
- littleDialogue.onMessageBox = function(e, msg, p, v)
- return onMessageBox(e, transplate.getTranslation(msg), p, v)
- end
- local registerAnswer = littleDialogue.registerAnswer
- littleDialogue.registerAnswer = function(name, answer)
- littleDialogueAnswers[answer] = {text = answer.text, addText = answer.addText}
- return registerAnswer(name, answer)
- end
- local getPortraitData = littleDialogue.getPortraitData
- littleDialogue.getPortraitData = function(name)
- local data = getPortraitData(name)
- if not littleDialogueSpeakerNames[data] then
- littleDialogueSpeakerNames[data] = data.name
- end
- data.name = transplate.getTranslation(littleDialogueSpeakerNames[data])
- return data
- end
- local customTags = littleDialogue.customTags
- local speakerName = customTags.speakerName
- customTags.speakerName = function(fmt,out,args)
- local args = args
- args[1] = transplate.getTranslation(args[1])
- return speakerName(fmt, out, args)
- end
- end
- return transplate
Advertisement
Add Comment
Please, Sign In to add comment