roflmuffin / CounterStrikeSharp

CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
https://docs.cssharp.dev
Other
742 stars 111 forks source link

Feature/localization rework #416

Closed Dliix66 closed 5 days ago

Dliix66 commented 4 months ago

Added some more methods to simplify localization out of a command's context

roflmuffin commented 4 months ago

Looks okay to me, might be worth adding a XML doc comment to the methods and updating the example plugin?

Dliix66 commented 4 months ago

I can do that 👍

frederikstonge commented 4 months ago

Since IStringLocalizer can be injected in other classes, would it not be better to create extension methods on IStringLocalizer?

    public static class LocalizerExtensions
    {
        public static string ForPlayer(this IStringLocalizer localizer, CCSPlayerController player, string key)
        {
            if (player == null || player.IsValid == false)
                return "";

            using WithTemporaryCulture temporaryCulture = new WithTemporaryCulture(player.GetLanguage());
            return localizer[key];
        }

        public static string ForPlayer(this IStringLocalizer localizer, CCSPlayerController player, string key, params object[] args)
        {
            if (player == null || player.IsValid == false)
                return "";

            using WithTemporaryCulture temporaryCulture = new WithTemporaryCulture(player.GetLanguage());
            return localizer[key, args];
        }
    }
Dliix66 commented 4 months ago

@roflmuffin I updated the example plugin and this should be available in the docs too.

roflmuffin commented 5 days ago

I've added these as extension methods in build #258, which I think is cleaner/more extensible than amending the base class in this PR.