From 3c9f1f36c55f5ba7acca214432e44c1c26c93922 Mon Sep 17 00:00:00 2001 From: Roflmuffin Date: Mon, 11 Dec 2023 15:03:18 +1000 Subject: [PATCH] feat: add color code translations --- .../WithTranslationsPlugin.cs | 6 +++++ examples/WithTranslations/lang/en.json | 4 +++- .../Resources/lang/en.json | 6 +++-- .../TranslationTests.cs | 8 +++++++ .../Core/Translations/JsonStringLocalizer.cs | 2 +- .../Core/Translations/StringExtensions.cs | 22 +++++++++++++++++++ 6 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 managed/CounterStrikeSharp.API/Core/Translations/StringExtensions.cs diff --git a/examples/WithTranslations/WithTranslationsPlugin.cs b/examples/WithTranslations/WithTranslationsPlugin.cs index 4435a1ab0..a384c7a44 100644 --- a/examples/WithTranslations/WithTranslationsPlugin.cs +++ b/examples/WithTranslations/WithTranslationsPlugin.cs @@ -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")] diff --git a/examples/WithTranslations/lang/en.json b/examples/WithTranslations/lang/en.json index c682e8297..3ab3fb253 100644 --- a/examples/WithTranslations/lang/en.json +++ b/examples/WithTranslations/lang/en.json @@ -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}" } \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API.Tests/Resources/lang/en.json b/managed/CounterStrikeSharp.API.Tests/Resources/lang/en.json index c682e8297..c9d1666c6 100644 --- a/managed/CounterStrikeSharp.API.Tests/Resources/lang/en.json +++ b/managed/CounterStrikeSharp.API.Tests/Resources/lang/en.json @@ -1,4 +1,6 @@ { "test.translation": "This is the english translation", - "test.format": "This number has 2 decimal places {0:n2}" -} \ No newline at end of file + "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}" +} \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API.Tests/TranslationTests.cs b/managed/CounterStrikeSharp.API.Tests/TranslationTests.cs index 75389eb2c..85ed9fee5 100644 --- a/managed/CounterStrikeSharp.API.Tests/TranslationTests.cs +++ b/managed/CounterStrikeSharp.API.Tests/TranslationTests.cs @@ -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]); + } } \ No newline at end of file diff --git a/managed/CounterStrikeSharp.API/Core/Translations/JsonStringLocalizer.cs b/managed/CounterStrikeSharp.API/Core/Translations/JsonStringLocalizer.cs index 55d3daa36..bd1b2aae8 100644 --- a/managed/CounterStrikeSharp.API/Core/Translations/JsonStringLocalizer.cs +++ b/managed/CounterStrikeSharp.API/Core/Translations/JsonStringLocalizer.cs @@ -70,7 +70,7 @@ public LocalizedString this[string name] result = _resourceManager.GetString(name, CultureInfo.DefaultThreadCurrentUICulture!); } - return result; + return result?.ReplaceColorTags(); } protected virtual IEnumerable GetAllStrings(bool includeParentCultures, CultureInfo culture) diff --git a/managed/CounterStrikeSharp.API/Core/Translations/StringExtensions.cs b/managed/CounterStrikeSharp.API/Core/Translations/StringExtensions.cs new file mode 100644 index 000000000..5a5bb66c1 --- /dev/null +++ b/managed/CounterStrikeSharp.API/Core/Translations/StringExtensions.cs @@ -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}"; + } +} \ No newline at end of file