From b801c59a62e23930d080fca0c55cc2d4c96c9dc7 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 14 Apr 2019 11:48:05 +0200 Subject: [PATCH] lang: update translation system --- gamecore/lang.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/gamecore/lang.lua b/gamecore/lang.lua index 95cfd85..519ad21 100644 --- a/gamecore/lang.lua +++ b/gamecore/lang.lua @@ -76,4 +76,46 @@ function LanguageManager:getCurrentLangName() return self:getLangName(self.data.current) end +-- TRANSLATION FUNCTIONS +-- get the translation of a string + +function LanguageManager:getTranslationStringList(lang, library) + local _path = self.data.path .. lang .. "/" .. library + local fileinfo = love.filesystem.getInfo(_path .. ".lua") + local list = nil + + if fileinfo ~= nil then + list = require(_path) + else + print("WARNING: file " .. _path .. " do not exists") + end + + return list +end + +function LanguageManager:translateFromLang(lang, library, stringToTranslate) + local _stringlist = self:getTranslationStringList(lang, library) + + if _stringlist == nil then + return nil + else + return _stringlist[stringToTranslate] + end +end + +function LanguageManager:translate(library, string) + local translation = self:translateFromLang(self.data.current, library, string) + + if (translation == nil) then + translation = self:translateFromLang(self.data.default, library, string) + end + + if (translation == nil) then + translation = string + print("WARNING: no translation path found for " .. string .. " in " .. library) + end + + return translation +end + return LanguageManager