Skip to content

Commit 95811d1

Browse files
committed
Adding Language Support
1 parent 0740dae commit 95811d1

File tree

9 files changed

+62
-6
lines changed

9 files changed

+62
-6
lines changed

Diff for: CustomCommands/CustomCommands.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
</None>
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<None Update="lang\**\*.*" CopyToOutputDirectory="PreserveNewest" />
25+
</ItemGroup>
26+
2327
</Project>

Diff for: CustomCommands/Interfaces/IReplaceTagsFunctions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace CustomCommands.Interfaces;
55
public interface IReplaceTagsFunctions
66
{
77
string[] ReplaceTags(string[] input, CCSPlayerController player);
8+
string ReplaceLanguageTags(string input);
89
string ReplaceMessageTags(string input, CCSPlayerController player);
910
string ReplaceColorTags(string input);
1011
string[] WrappedLine(dynamic message);

Diff for: CustomCommands/Services/EventManager.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
3232
public void RegisterListeners()
3333
{
3434
CustomCommands plugin = (PluginContext.Plugin as CustomCommands)!;
35+
3536
plugin.RegisterListener<Listeners.OnTick>(() =>
3637
{
3738
// Client Print To Center
@@ -53,8 +54,9 @@ public void RegisterListeners()
5354
Utilities.GetPlayers().ForEach(controller =>
5455
{
5556
if (controller == null || !controller.IsValid) return;
56-
57-
string message = ReplaceTagsFunctions.ReplaceMessageTags(PluginGlobals.centerServerOn.Message, controller);
57+
58+
string message = ReplaceTagsFunctions.ReplaceLanguageTags(PluginGlobals.centerServerOn.Message);
59+
message = ReplaceTagsFunctions.ReplaceMessageTags(message, controller);
5860
controller.PrintToCenterHtml(message, 1);
5961
});
6062
}

Diff for: CustomCommands/Services/LoadJson.cs

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public LoadJson(ILogger<CustomCommands> Logger)
2626
var json = File.ReadAllText(jsonPath);
2727
return JsonSerializer.Deserialize<List<Commands>>(json);
2828
}
29+
else if (File.Exists(Path.Combine(path, "Commands.example.json")))
30+
{
31+
Logger.LogWarning("No Config file found. Please rename Commands.example.json to Commands.json");
32+
return null;
33+
}
2934
else
3035
{
3136
Logger.LogWarning("No Config file found. Please create one");

Diff for: CustomCommands/Services/MessageManager.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void PrintToCenterClient(CCSPlayerController player, Commands cmd)
5656
{
5757
CustomCommands plugin = (PluginContext.Plugin as CustomCommands)!;
5858

59-
string message = ReplaceTagsFunctions.ReplaceMessageTags(cmd.CenterMessage.Message, player);
59+
60+
string message = ReplaceTagsFunctions.ReplaceLanguageTags(cmd.CenterMessage.Message);
61+
message = ReplaceTagsFunctions.ReplaceMessageTags(message, player);
6062

6163
var CenterClientElement = new CenterClientElement
6264
{

Diff for: CustomCommands/Services/ReplaceTagsFunction.cs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
using System.Text.Json;
2+
using System.Text.RegularExpressions;
23
using CounterStrikeSharp.API;
34
using CounterStrikeSharp.API.Core;
45
using CounterStrikeSharp.API.Modules.Cvars;
56
using CounterStrikeSharp.API.Modules.Entities;
67
using CounterStrikeSharp.API.Modules.Utils;
78
using CustomCommands.Interfaces;
9+
using CounterStrikeSharp.API.Core.Translations;
810
using Microsoft.Extensions.Logging;
11+
using CounterStrikeSharp.API.Core.Plugin;
912

1013
namespace CustomCommands.Services;
1114
public class ReplaceTagsFunctions : IReplaceTagsFunctions
1215
{
1316
private readonly IPluginGlobals PluginGlobals;
17+
private readonly PluginContext PluginContext;
1418
private readonly ILogger<CustomCommands> Logger;
1519

16-
public ReplaceTagsFunctions(IPluginGlobals PluginGlobals, ILogger<CustomCommands> Logger)
20+
public ReplaceTagsFunctions(IPluginGlobals PluginGlobals, IPluginContext PluginContext, ILogger<CustomCommands> Logger)
1721
{
1822
this.PluginGlobals = PluginGlobals;
23+
this.PluginContext = (PluginContext as PluginContext)!;
1924
this.Logger = Logger;
2025
}
2126

@@ -25,13 +30,38 @@ public string[] ReplaceTags(string[] input, CCSPlayerController player)
2530

2631
for (int i = 0; i < input.Length; i++)
2732
{
28-
output[i] = ReplaceMessageTags(input[i], player);
33+
output[i] = ReplaceLanguageTags(input[i]);
34+
output[i] = ReplaceMessageTags(output[i], player);
2935
output[i] = ReplaceColorTags(output[i]);
3036
}
3137

3238
return output;
3339
}
3440

41+
public string ReplaceLanguageTags(string input)
42+
{
43+
CustomCommands plugin = (PluginContext.Plugin as CustomCommands)!;
44+
45+
// Define the regex pattern to find "{LANG=...}"
46+
string pattern = @"\{LANG=(.*?)\}";
47+
48+
// Use Regex to find matches
49+
Match match = Regex.Match(input, pattern);
50+
51+
// Check if a match is found
52+
if (match.Success)
53+
{
54+
// Return the group captured in the regex, which is the string after "="
55+
string lang = match.Groups[1].Value;
56+
57+
return input.Replace(match.Value, plugin.Localizer[lang] ?? "<LANG in CustomCommands/lang/<language.json> not found>");
58+
}
59+
else
60+
{
61+
// Return the original string if no match is found
62+
return input;
63+
}
64+
}
3565
public string ReplaceMessageTags(string input, CCSPlayerController player)
3666
{
3767
SteamID steamId = new SteamID(player.SteamID);

Diff for: CustomCommands/lang/de.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"test": "Ein sehr cooler test Satz!"
3+
}

Diff for: CustomCommands/lang/en.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
2+
"test": "A very cool test sentance!"
33
}

Diff for: Examples/LanguageSupport.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"Title": "Sentance",
4+
"Description": "Send a sentance",
5+
"Command": "sentance",
6+
"Message": "{LANG=test} test",
7+
"PrintTo": 0
8+
}
9+
]

0 commit comments

Comments
 (0)