Skip to content

Commit

Permalink
Merging locale.lua into messaging.lua.
Browse files Browse the repository at this point in the history
This is an attempt to fix a messaging.lua bug, being unable to load getTranslations function.
  • Loading branch information
manix84 committed Feb 10, 2021
1 parent a6ee1aa commit de8bcc0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 89 deletions.
11 changes: 5 additions & 6 deletions lua/autorun/shared.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
AddCSLuaFile()

include("discord/utils/logging.lua")
include("discord/utils/messaging.lua")
include("discord/utils/discord_connection.lua")
include("discord/utils/http.lua")

resource.AddFile("materials/icon256/mute.png")
if (CLIENT) then
shouldDrawMute = false
Expand All @@ -20,12 +25,6 @@ if (CLIENT) then
return
end

include("discord/utils/locale.lua")
include("discord/utils/messaging.lua")
include("discord/utils/logging.lua")
include("discord/utils/discord_connection.lua")
include("discord/utils/http.lua")

util.AddNetworkString("drawMute")
util.AddNetworkString("connectDiscordID")
util.AddNetworkString("discordPlayerTable")
Expand Down
82 changes: 0 additions & 82 deletions lua/discord/utils/locale.lua

This file was deleted.

81 changes: 80 additions & 1 deletion lua/discord/utils/messaging.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,83 @@
include("./locale.lua")
CreateConVar("discord_language", "english", 1, "Set the language you want user prompts to be in.")

local availableLanguages = {}
function getAvailableLanguages()
if (table.Count(availableLanguages) <= 0) then
local foundLocaleFiles, _ = file.Find("discord/locale/*.lua", "lsv")
for i, localeFileName in ipairs(foundLocaleFiles) do
table.insert(availableLanguages, string.Replace(localeFileName, ".lua", ""))
end
end
return availableLanguages
end
getAvailableLanguages()
print_debug("Available languages: ", util.TableToJSON(availableLanguages))

local selectedLanguage = "english"
function getSelectedLanguage()
local selectedLanguage = GetConVar("discord_language"):GetString()
if (table.HasValue(getAvailableLanguages(), selectedLanguage)) then
selectedLanguage = selectedLanguage
end
return selectedLanguage
end
getSelectedLanguage()
print_debug("Selected language: ", selectedLanguage)

local translationsCache = {}
local function getTranslationsCache()
if (table.Count(translationsCache) <= 0) then
print_debug("Caching language files:")
local localesTable = getAvailableLanguages()
for i, localeFileName in ipairs(localesTable) do
translationsCache[localeFileName] = include("discord/locale/" .. localeFileName .. ".lua")
print_debug(" -", "discord/locale/" .. localeFileName .. ".lua")
end
end
return translationsCache
end
getTranslationsCache()

local translations = {}
function getTranslations()
local selectedLanguage = getSelectedLanguage()
local translationsCache = getTranslationsCache()
translations = translationsCache[selectedLanguage]
return translations
end
getTranslations()
print_debug("Translations:", util.TableToJSON(translations, true))

util.AddNetworkString("discordAvailableLanguages")
util.AddNetworkString("request_discordAvailableLanguages")
net.Receive("request_discordAvailableLanguages", function( len, calling_ply )
if (!calling_ply:IsSuperAdmin()) then
return
end
local availableLanguages = getAvailableLanguages()
local availableLanguagesJSON = util.TableToJSON(availableLanguages)
local availableLanguagesCompressed = util.Compress(availableLanguagesJSON)

net.Start("discordAvailableLanguages")
net.WriteUInt(#availableLanguagesCompressed, 32)
net.WriteData(availableLanguagesCompressed, #availableLanguagesCompressed)
net.Send(calling_ply)
end)

util.AddNetworkString("discordSelectedLanguage")
util.AddNetworkString("request_discordSelectedLanguage")
net.Receive("request_discordSelectedLanguage", function( len, calling_ply )
if (!calling_ply:IsSuperAdmin()) then
return
end
local selectedLanguage = getSelectedLanguage()
local selectedLanguageCompressed = util.Compress(selectedLanguage)

net.Start("discordSelectedLanguage")
net.WriteUInt(#selectedLanguageCompressed, 32)
net.WriteData(selectedLanguageCompressed, #selectedLanguageCompressed)
net.Send(calling_ply)
end)

function playerMessage(translation_key, target_ply, ...)
local translations = getTranslations()
Expand Down

0 comments on commit de8bcc0

Please sign in to comment.