options: make language loading and changing part of the options

This commit is contained in:
Kazhnuz 2019-04-13 18:54:07 +02:00
parent 59029068f1
commit fb29ae65b3
2 changed files with 32 additions and 14 deletions

View file

@ -31,28 +31,21 @@ local TRANSLATION_PATH = "datas/languages/"
function LanguageManager:new(controller) function LanguageManager:new(controller)
self.controller = controller self.controller = controller
self.datas = self:getTranslationData() self.datas = self:getTranslationData()
self:setLang(self.controller.options.data.language) self:setLang(self.controller.options.data.language.current)
end end
function LanguageManager:setLang(lang) function LanguageManager:setLang(lang)
self.controller.options.data.language = lang self.controller.options.data.language.current = lang
self.lang = self.controller.options.data.language self.lang = self.controller.options.data.language.current
end end
-- INFO FUNCTIONS -- INFO FUNCTIONS
-- Get informations from the translation manager -- Get informations from the translation manager
function LanguageManager:getTranslationData() function LanguageManager:getTranslationData()
local _path = TRANSLATION_PATH .. "init.lua" return self.controller.options.data.language
local fileinfo = love.filesystem.getInfo(_path)
local datas = nil
if fileinfo ~= nil then
datas = require(TRANSLATION_PATH)
end
return datas
end end
function LanguageManager:getStringList(library, file) function LanguageManager:getStringList(library, file)

View file

@ -27,6 +27,8 @@ local OptionsManager = Object:extend()
local cwd = (...):gsub('%.options$', '') .. "." local cwd = (...):gsub('%.options$', '') .. "."
local binser = require(cwd .. "libs.binser") local binser = require(cwd .. "libs.binser")
local TRANSLATION_PATH = "datas/languages/"
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialize and configure the game options -- Initialize and configure the game options
@ -49,7 +51,7 @@ function OptionsManager:reset()
self.data.input = self:getInputDefaultData() self.data.input = self:getInputDefaultData()
-- TODO: have a way to auto-load a language according to the OS ? -- TODO: have a way to auto-load a language according to the OS ?
self.data.language = "en" self.data.language = self:getTranslationDefaultData()
self.data.audio = {} self.data.audio = {}
self.data.audio.music = 100 self.data.audio.music = 100
@ -102,6 +104,29 @@ function OptionsManager:getInputData()
return self.data.input return self.data.input
end end
-- Lang data
function OptionsManager:getTranslationDefaultData()
local _path = TRANSLATION_PATH .. "init.lua"
local fileinfo = love.filesystem.getInfo(_path)
local datas = nil
if fileinfo ~= nil then
lang = require(TRANSLATION_PATH)
lang.current = self.default
lang.path = TRANSLATION_PATH
end
return lang
end
function OptionsManager:setLanguage(lang)
if (self.controller.lang:isLangAvailable(lang)) then
self.data.language.current = lang
self.controller.lang:getTranslationData()
end
end
-- DATA HANDLING FUNCTIONS -- DATA HANDLING FUNCTIONS
-- Save and get data from the savefile -- Save and get data from the savefile