v1.0.3
This commit is contained in:
parent
68776c3382
commit
576cee8cfb
87 changed files with 4957 additions and 1558 deletions
57
jRandomSkills - SRC Files/src/utils/Localization.cs
Normal file
57
jRandomSkills - SRC Files/src/utils/Localization.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using CounterStrikeSharp.API.Modules.Utils;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace jRandomSkills.src.utils
|
||||
{
|
||||
public static class Localization
|
||||
{
|
||||
private static readonly string path = Path.Combine(jRandomSkills.Instance.ModuleDirectory, "lang");
|
||||
private static readonly Dictionary<string, Dictionary<string, string>> _translations = new Dictionary<string, Dictionary<string, string>>();
|
||||
private static string langCode = "pl";
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
_translations.Clear();
|
||||
SetLangCode();
|
||||
LoadAllLanguages();
|
||||
}
|
||||
|
||||
private static void SetLangCode()
|
||||
{
|
||||
langCode = Config.config.Settings.LangCode;
|
||||
}
|
||||
|
||||
private static void LoadAllLanguages()
|
||||
{
|
||||
if (!Directory.Exists(path))
|
||||
return;
|
||||
|
||||
foreach (var file in Directory.GetFiles(path, "*.json"))
|
||||
LoadLanguage(file);
|
||||
}
|
||||
|
||||
private static void LoadLanguage(string langPath)
|
||||
{
|
||||
if (!File.Exists(langPath))
|
||||
return;
|
||||
|
||||
var code = Path.GetFileNameWithoutExtension(langPath);
|
||||
var jsonText = File.ReadAllText(langPath);
|
||||
var translations = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonText);
|
||||
|
||||
if (translations != null)
|
||||
_translations[code] = translations;
|
||||
}
|
||||
|
||||
public static string GetTranslation(string key, params object[] args)
|
||||
{
|
||||
if (_translations.TryGetValue(langCode, out var langDict) && langDict.TryGetValue(key, out var translation))
|
||||
if (args.Length != 0 && args[0] == "welcome")
|
||||
return translation;
|
||||
else
|
||||
return string.Format(translation, args).Replace("CHATCOLORS.RED", ChatColors.Red.ToString());
|
||||
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue