Files
tbd-station-14/Content.Server/Speech/EntitySystems/SpanishAccentSystem.cs
TakoDragon 929e6a2c00 Improved Spanish accent (#30551)
* Spanish and French comment

* Added the interrobang

* Make spanish accent use sting builder
2024-08-08 13:08:28 +10:00

74 lines
2.3 KiB
C#

using System.Text;
using Content.Server.Speech.Components;
namespace Content.Server.Speech.EntitySystems
{
public sealed class SpanishAccentSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<SpanishAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
// Insert E before every S
message = InsertS(message);
// If a sentence ends with ?, insert a reverse ? at the beginning of the sentence
message = ReplacePunctuation(message);
return message;
}
private string InsertS(string message)
{
// Replace every new Word that starts with s/S
var msg = message.Replace(" s", " es").Replace(" S", " Es");
// Still need to check if the beginning of the message starts
if (msg.StartsWith("s", StringComparison.Ordinal))
{
return msg.Remove(0, 1).Insert(0, "es");
}
else if (msg.StartsWith("S", StringComparison.Ordinal))
{
return msg.Remove(0, 1).Insert(0, "Es");
}
return msg;
}
private string ReplacePunctuation(string message)
{
var sentences = AccentSystem.SentenceRegex.Split(message);
var msg = new StringBuilder();
foreach (var s in sentences)
{
var toInsert = new StringBuilder();
for (var i = s.Length - 1; i >= 0 && "?!‽".Contains(s[i]); i--)
{
toInsert.Append(s[i] switch
{
'?' => '¿',
'!' => '¡',
'‽' => '⸘',
_ => ' '
});
}
if (toInsert.Length == 0)
{
msg.Append(s);
} else
{
msg.Append(s.Insert(s.Length - s.TrimStart().Length, toInsert.ToString()));
}
}
return msg.ToString();
}
private void OnAccent(EntityUid uid, SpanishAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}