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)
self.controller = controller
self.datas = self:getTranslationData()
self:setLang(self.controller.options.data.language)
self.datas = self:getTranslationData()
self:setLang(self.controller.options.data.language.current)
end
function LanguageManager:setLang(lang)
self.controller.options.data.language = lang
self.lang = self.controller.options.data.language
self.controller.options.data.language.current = lang
self.lang = self.controller.options.data.language.current
end
-- INFO FUNCTIONS
-- Get informations from the translation manager
function LanguageManager:getTranslationData()
local _path = TRANSLATION_PATH .. "init.lua"
local fileinfo = love.filesystem.getInfo(_path)
local datas = nil
if fileinfo ~= nil then
datas = require(TRANSLATION_PATH)
end
return datas
return self.controller.options.data.language
end
function LanguageManager:getStringList(library, file)

View file

@ -27,6 +27,8 @@ local OptionsManager = Object:extend()
local cwd = (...):gsub('%.options$', '') .. "."
local binser = require(cwd .. "libs.binser")
local TRANSLATION_PATH = "datas/languages/"
-- INIT FUNCTIONS
-- Initialize and configure the game options
@ -49,7 +51,7 @@ function OptionsManager:reset()
self.data.input = self:getInputDefaultData()
-- 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.music = 100
@ -102,6 +104,29 @@ function OptionsManager:getInputData()
return self.data.input
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
-- Save and get data from the savefile