Skip to content

Commit

Permalink
feat: add color code translations
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Dec 11, 2023
1 parent 628e435 commit 3c9f1f3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions examples/WithTranslations/WithTranslationsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public override void Load(bool hotReload)
// IStringLocalizer can accept standard string format arguments.
// "This number has 2 decimal places {0:n2}" -> "This number has 2 decimal places 123.55"
Logger.LogInformation(Localizer["test.format", 123.551]);

// This message has colour codes
Server.PrintToChatAll(Localizer["test.colors"]);

// This message has colour codes and formatted values
Server.PrintToChatAll(Localizer["test.colors.withformat", 123.551]);
}

[ConsoleCommand("css_replylanguage", "Test Translations")]
Expand Down
4 changes: 3 additions & 1 deletion examples/WithTranslations/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"test.translation": "This is the english translation",
"test.format": "This number has 2 decimal places {0:n2}"
"test.format": "This number has 2 decimal places {0:n2}",
"test.colors": "{orange}This{default} text has {green}green{default} text",
"test.colors.withformat": "{orange}{0:n2}{default}"
}
6 changes: 4 additions & 2 deletions managed/CounterStrikeSharp.API.Tests/Resources/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"test.translation": "This is the english translation",
"test.format": "This number has 2 decimal places {0:n2}"
}
"test.format": "This number has 2 decimal places {0:n2}",
"test.colors": "{orange}This{default} text has {green}green{default} text",
"test.colors.withformat": "{orange}{0:n2}{default}"
}
8 changes: 8 additions & 0 deletions managed/CounterStrikeSharp.API.Tests/TranslationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public void ShouldHandleFormatStrings()
{
Assert.Equal("This number has 2 decimal places 0.25", _localizer["test.format", 0.251]);
}

[Fact]
public void HandlesColorFormatting()
{
// Sets invisible pre-color if there is a color code in the string.
Assert.Equal("\x01\x0B\x01\x10This\x01 text has \x04green\x01 text", _localizer["test.colors"]);
Assert.Equal($"\x01\x0B\x01{'\x10'}1.25\x01", _localizer["test.colors.withformat", 1.25]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public LocalizedString this[string name]
result = _resourceManager.GetString(name, CultureInfo.DefaultThreadCurrentUICulture!);
}

return result;
return result?.ReplaceColorTags();
}

protected virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures, CultureInfo culture)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Reflection;
using CounterStrikeSharp.API.Modules.Utils;

namespace CounterStrikeSharp.API.Core.Translations;

public static class StringExtensions
{
public static string ReplaceColorTags(this string message)
{
var modifiedValue = message;
foreach (var field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (modifiedValue.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)?.ToString() ?? string.Empty, StringComparison.OrdinalIgnoreCase);
}
}

return modifiedValue.Equals(message) ? message : $"\x01\x0B\x01{modifiedValue}";
}
}

0 comments on commit 3c9f1f3

Please sign in to comment.