CounterStrikeSharp icon indicating copy to clipboard operation
CounterStrikeSharp copied to clipboard

Feature/localization rework

Open Dliix66 opened this issue 1 year ago • 4 comments

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

Dliix66 avatar Apr 13 '24 23:04 Dliix66

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

roflmuffin avatar Apr 14 '24 00:04 roflmuffin

I can do that 👍

Dliix66 avatar Apr 15 '24 14:04 Dliix66

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];
        }
    }

frederikstonge avatar Apr 16 '24 12:04 frederikstonge

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

Dliix66 avatar Apr 17 '24 15:04 Dliix66

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.

roflmuffin avatar Aug 19 '24 02:08 roflmuffin