From 7e53cb8041bc7417a97218d9805d4f2cd025ae02 Mon Sep 17 00:00:00 2001 From: DEATHB4DEFEAT Date: Sun, 18 Aug 2024 01:08:01 -0700 Subject: [PATCH 1/4] LocalizedPrototype --- .../Prototypes/LocalizedPrototype.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Content.Shared/Prototypes/LocalizedPrototype.cs diff --git a/Content.Shared/Prototypes/LocalizedPrototype.cs b/Content.Shared/Prototypes/LocalizedPrototype.cs new file mode 100644 index 00000000000..8dc523178f6 --- /dev/null +++ b/Content.Shared/Prototypes/LocalizedPrototype.cs @@ -0,0 +1,37 @@ +using System.Linq; +using Robust.Shared.Prototypes; + +namespace Content.Shared.Prototypes; + +public abstract class LocalizedPrototype : IPrototype +{ + [IdDataField] + public string ID { get; } = default!; + + public const string LocFormat = "{type}-{ID}-{field}"; + + public string Name => ToLocalizationString("name"); + public string LocalizedName => Loc.GetString(Name); + + public string ToLocalizationString(string field) + { + // Get the ID of the proto Type + var type = + ((PrototypeAttribute?) Attribute.GetCustomAttribute(GetType(), typeof(PrototypeAttribute)))?.Type + ?? GetType().Name.Remove(GetType().Name.Length - 9); + // Lowercase the first letter + type = char.ToLowerInvariant(type[0]) + type[1..]; + // Replace every uppercase letter with a dash and the lowercase letter + type = type.Aggregate("", (current, c) => current + (char.IsUpper(c) ? "-" + char.ToLowerInvariant(c) : c.ToString())); + + // Replace the placeholders with the actual values + var t = LocFormat + .Replace("{type}", type) + .Replace("{ID}", ID) + .Replace("{field}", field); + + Logger.Fatal(t); + Logger.Fatal(Loc.GetString(t)); + return t; + } +} From baf0a1fd53f38d689fc75806cde4cddbc93dfabe Mon Sep 17 00:00:00 2001 From: DEATHB4DEFEAT Date: Sun, 18 Aug 2024 01:42:05 -0700 Subject: [PATCH 2/4] rename name and locale name --- Content.Shared/Prototypes/LocalizedPrototype.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Content.Shared/Prototypes/LocalizedPrototype.cs b/Content.Shared/Prototypes/LocalizedPrototype.cs index 8dc523178f6..fcd739fd242 100644 --- a/Content.Shared/Prototypes/LocalizedPrototype.cs +++ b/Content.Shared/Prototypes/LocalizedPrototype.cs @@ -10,8 +10,10 @@ public abstract class LocalizedPrototype : IPrototype public const string LocFormat = "{type}-{ID}-{field}"; - public string Name => ToLocalizationString("name"); - public string LocalizedName => Loc.GetString(Name); + /// The localization string for the name of this prototype + public string NameLoc => ToLocalizationString("name"); + /// The localized string for the name of prototype + public string Name => Loc.GetString(NameLoc); public string ToLocalizationString(string field) { From 26a7d4c876fa85f925ae6b2652396601395fe97a Mon Sep 17 00:00:00 2001 From: DEATHB4DEFEAT Date: Sun, 18 Aug 2024 01:28:08 -0700 Subject: [PATCH 3/4] remove debug log --- Content.Shared/Prototypes/LocalizedPrototype.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Content.Shared/Prototypes/LocalizedPrototype.cs b/Content.Shared/Prototypes/LocalizedPrototype.cs index fcd739fd242..7d2d19b3f8b 100644 --- a/Content.Shared/Prototypes/LocalizedPrototype.cs +++ b/Content.Shared/Prototypes/LocalizedPrototype.cs @@ -32,8 +32,6 @@ public string ToLocalizationString(string field) .Replace("{ID}", ID) .Replace("{field}", field); - Logger.Fatal(t); - Logger.Fatal(Loc.GetString(t)); return t; } } From b7b290b380db92557bd3b346d464bf6c8f6c2e98 Mon Sep 17 00:00:00 2001 From: DEATHB4DEFEAT Date: Sun, 18 Aug 2024 02:36:54 -0700 Subject: [PATCH 4/4] review changes --- Content.Shared/Prototypes/LocalizedPrototype.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Content.Shared/Prototypes/LocalizedPrototype.cs b/Content.Shared/Prototypes/LocalizedPrototype.cs index 7d2d19b3f8b..acdd5fc180f 100644 --- a/Content.Shared/Prototypes/LocalizedPrototype.cs +++ b/Content.Shared/Prototypes/LocalizedPrototype.cs @@ -8,13 +8,17 @@ public abstract class LocalizedPrototype : IPrototype [IdDataField] public string ID { get; } = default!; - public const string LocFormat = "{type}-{ID}-{field}"; + public const string LocFormat = "{0}-{1}-{2}"; /// The localization string for the name of this prototype public string NameLoc => ToLocalizationString("name"); /// The localized string for the name of prototype public string Name => Loc.GetString(NameLoc); + /// + /// Returns an Loc string using the as the 'property'. + /// Given `desc` it will return `this-prototype-PrototypeId-desc`. + /// public string ToLocalizationString(string field) { // Get the ID of the proto Type @@ -26,12 +30,6 @@ public string ToLocalizationString(string field) // Replace every uppercase letter with a dash and the lowercase letter type = type.Aggregate("", (current, c) => current + (char.IsUpper(c) ? "-" + char.ToLowerInvariant(c) : c.ToString())); - // Replace the placeholders with the actual values - var t = LocFormat - .Replace("{type}", type) - .Replace("{ID}", ID) - .Replace("{field}", field); - - return t; + return string.Format(LocFormat, type, ID, field); } }