Make RandomMetadata properly support localization (#36343)
* Make _outputSegments readonly * Remove mystery character * Use Fluent instead of string concatenation * Adjust format * Convert existing content * Don't need these anymore * Docs
This commit is contained in:
@@ -12,7 +12,7 @@ public sealed class RandomMetadataSystem : EntitySystem
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly MetaDataSystem _metaData = default!;
|
||||
|
||||
private List<string> _outputSegments = new();
|
||||
private readonly List<(string, object)> _outputSegments = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -28,13 +28,13 @@ public sealed class RandomMetadataSystem : EntitySystem
|
||||
|
||||
if (component.NameSegments != null)
|
||||
{
|
||||
_metaData.SetEntityName(uid, GetRandomFromSegments(component.NameSegments, component.NameSeparator), meta);
|
||||
_metaData.SetEntityName(uid, GetRandomFromSegments(component.NameSegments, component.NameFormat), meta);
|
||||
}
|
||||
|
||||
if (component.DescriptionSegments != null)
|
||||
{
|
||||
_metaData.SetEntityDescription(uid,
|
||||
GetRandomFromSegments(component.DescriptionSegments, component.DescriptionSeparator), meta);
|
||||
GetRandomFromSegments(component.DescriptionSegments, component.DescriptionFormat), meta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,17 +42,18 @@ public sealed class RandomMetadataSystem : EntitySystem
|
||||
/// Generates a random string from segments and a separator.
|
||||
/// </summary>
|
||||
/// <param name="segments">The segments that it will be generated from</param>
|
||||
/// <param name="separator">The separator that will be inbetween each segment</param>
|
||||
/// <param name="format">The format string used to combine the segments.</param>
|
||||
/// <returns>The newly generated string</returns>
|
||||
[PublicAPI]
|
||||
public string GetRandomFromSegments(List<ProtoId<LocalizedDatasetPrototype>> segments, string? separator)
|
||||
public string GetRandomFromSegments(List<ProtoId<LocalizedDatasetPrototype>> segments, LocId format)
|
||||
{
|
||||
_outputSegments.Clear();
|
||||
foreach (var segment in segments)
|
||||
for (var i = 0; i < segments.Count; ++i)
|
||||
{
|
||||
var localizedProto = _prototype.Index(segment);
|
||||
_outputSegments.Add(_random.Pick(localizedProto));
|
||||
var localizedProto = _prototype.Index(segments[i]);
|
||||
_outputSegments.Add(($"part{i}", _random.Pick(localizedProto)));
|
||||
}
|
||||
return string.Join(separator, _outputSegments);
|
||||
|
||||
return Loc.GetString(format, _outputSegments.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user