Skip to content

Commit

Permalink
Automatically fetch names and descs from loc.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Feb 22, 2021
1 parent 78c132f commit 6bd5814
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Robust.Shared/Localization/ILocalizationManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using JetBrains.Annotations;
using Robust.Shared.ContentPack;
Expand Down Expand Up @@ -31,11 +31,15 @@ public interface ILocalizationManager
/// </returns>
string GetString(string messageId);

bool TryGetString(string messageId, [NotNullWhen(true)] out string? value);

/// <summary>
/// Version of <see cref="GetString(string)"/> that supports arguments.
/// </summary>
string GetString(string messageId, params (string, object)[] args);

bool TryGetString(string messageId, [NotNullWhen(true)] out string? value, params (string, object)[] args);

/// <summary>
/// Default culture used by other methods when no culture is explicitly specified.
/// Changing this also changes the current thread's culture.
Expand Down
15 changes: 14 additions & 1 deletion Robust.Shared/Localization/Loc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using JetBrains.Annotations;
using Robust.Shared.ContentPack;
Expand Down Expand Up @@ -36,6 +36,11 @@ public static string GetString(string messageId)
return LocalizationManager.GetString(messageId);
}

public static bool TryGetString(string messageId, [NotNullWhen(true)] out string? message)
{
return LocalizationManager.TryGetString(messageId, out message);
}

/// <summary>
/// Version of <see cref="GetString(string)"/> that supports arguments.
/// </summary>
Expand All @@ -44,6 +49,14 @@ public static string GetString(string messageId, params (string,object)[] args)
return LocalizationManager.GetString(messageId, args);
}

public static bool TryGetString(
string messageId,
[NotNullWhen(true)] out string? value,
params (string, object)[] args)
{
return LocalizationManager.TryGetString(messageId, out value, args);
}

/// <summary>
/// Load data for a culture.
/// </summary>
Expand Down
102 changes: 85 additions & 17 deletions Robust.Shared/Localization/LocalizationManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using Fluent.Net;
using Fluent.Net.RuntimeAst;
using JetBrains.Annotations;
using Robust.Shared.ContentPack;
using Robust.Shared.Log;
Expand All @@ -20,42 +22,107 @@ internal sealed class LocalizationManager : ILocalizationManagerInternal
public string GetString(string messageId)
{
if (_defaultCulture == null)
{
return messageId;
}
var context = _contexts[_defaultCulture];
var message = context.GetMessage(messageId);

if (message == null)
if (!TryGetString(messageId, out var msg))
{
Logger.WarningS("Loc", $"Unknown messageId ({_defaultCulture.IetfLanguageTag}): {messageId}");
return messageId;
msg = messageId;
}

return context.Format(message, null, null);
return msg;
}

public bool TryGetString(string messageId, [NotNullWhen(true)] out string? value)
{
if (!TryGetNode(messageId, out var context, out var node))
{
value = null;
return false;
}

value = context.Format(node, null, null);
return true;
}

public string GetString(string messageId, params (string, object)[] args0)
{
if (_defaultCulture == null)
{
return messageId;

if (!TryGetString(messageId, out var msg, args0))
{
Logger.WarningS("Loc", $"Unknown messageId ({_defaultCulture.IetfLanguageTag}): {messageId}");
msg = messageId;
}
var context = _contexts[_defaultCulture];
var message = context.GetMessage(messageId);

return msg;
}

public bool TryGetString(string messageId, [NotNullWhen(true)] out string? value, params (string, object)[] args0)
{
if (!TryGetNode(messageId, out var context, out var node))
{
value = null;
return false;
}

var args = new Dictionary<string, object>();
foreach (var vari in args0)
foreach (var (k, v) in args0)
{
args.Add(vari.Item1, vari.Item2);
args.Add(k, v);
}

value = context.Format(node, args, null);
return false;
}

private bool TryGetNode(
string messageId,
[NotNullWhen(true)] out MessageContext? ctx,
[NotNullWhen(true)] out Node? node)
{
if (_defaultCulture == null)
{
ctx = null;
node = null;
return false;
}

ctx = _contexts[_defaultCulture];
string? attribName = null;

if (messageId.Contains('.'))
{
var split = messageId.Split('.');
messageId = split[0];
attribName = split[1];
}

var message = ctx.GetMessage(messageId);

if (message == null)
{
Logger.WarningS("Loc", $"Unknown messageId ({_defaultCulture.IetfLanguageTag}): {messageId}");
return messageId;
node = null;
return false;
}

if (attribName != null)
{
if (!message.Attributes.TryGetValue(attribName, out var attrib))
{
node = null;
return false;
}

node = attrib;
}
else
{
node = message;
}

return context.Format(message, args, null);
return true;
}

/// <summary>
Expand Down Expand Up @@ -94,7 +161,7 @@ public void LoadCulture(IResourceManager resourceManager, CultureInfo culture)
{
var context = new MessageContext(
culture.Name,
new MessageContextOptions { UseIsolating = false }
new MessageContextOptions {UseIsolating = false}
);

_contexts.Add(culture, context);
Expand Down Expand Up @@ -144,7 +211,8 @@ private static void _loadData(IResourceManager resourceManager, CultureInfo cult
}
}

private static void _loadFromFile(IResourceManager resourceManager, ResourcePath filePath, MessageContext context)
private static void _loadFromFile(IResourceManager resourceManager, ResourcePath filePath,
MessageContext context)
{
using (var fileStream = resourceManager.ContentFileRead(filePath))
using (var reader = new StreamReader(fileStream, EncodingHelpers.UTF8))
Expand Down
17 changes: 14 additions & 3 deletions Robust.Shared/Prototypes/EntityPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,18 @@ public EntityPrototype()

public void LoadFrom(YamlMappingNode mapping)
{
var loc = IoCManager.Resolve<ILocalizationManager>();
ID = mapping.GetNode("id").AsString();

if (mapping.TryGetNode("name", out var node))
{
_nameModified = true;
Name = Loc.GetString(node.AsString());
Name = loc.GetString(node.AsString());
}
else if (loc.TryGetString($"ent-{CaseConversion.PascalToKebab(ID)}", out var name))
{
_nameModified = true;
Name = loc.GetString(name);
}

if (mapping.TryGetNode("parent", out node))
Expand All @@ -163,12 +169,17 @@ public void LoadFrom(YamlMappingNode mapping)
if (mapping.TryGetNode("description", out node))
{
_descriptionModified = true;
Description = Loc.GetString(node.AsString());
Description = loc.GetString(node.AsString());
}
else if (loc.TryGetString($"ent-{CaseConversion.PascalToKebab(ID)}.desc", out var name))
{
_descriptionModified = true;
Description = loc.GetString(name);
}

if (mapping.TryGetNode("suffix", out node))
{
EditorSuffix = Loc.GetString(node.AsString());
EditorSuffix = loc.GetString(node.AsString());
}

// COMPONENTS
Expand Down
18 changes: 18 additions & 0 deletions Robust.Shared/Utility/CaseConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.RegularExpressions;

namespace Robust.Shared.Utility
{
public static class CaseConversion
{
private static readonly Regex PascalToKebabRegex =
new Regex("(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", RegexOptions.Compiled);

public static string PascalToKebab(string str)
{
if (string.IsNullOrWhiteSpace(str))
return str;

return PascalToKebabRegex.Replace(str, "-$1").Trim().ToLower();
}
}
}
23 changes: 23 additions & 0 deletions Robust.UnitTesting/Shared/Utility/CaseConversionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using Robust.Shared.Utility;

namespace Robust.UnitTesting.Shared.Utility
{
[Parallelizable(ParallelScope.All)]
[TestFixture]
[TestOf(typeof(CaseConversion))]
public sealed class CaseConversionTest
{
[Test]
[TestCase("FooBar", ExpectedResult = "foo-bar")]
[TestCase("Foo", ExpectedResult = "foo")]
[TestCase("FooBarBaz", ExpectedResult = "foo-bar-baz")]
[TestCase("AssistantPDA", ExpectedResult = "assistant-pda")] // incorrect abbreviations
[TestCase("AssistantPda", ExpectedResult = "assistant-pda")] // correct abbreviations
[TestCase("FileIO", ExpectedResult = "file-io")]
public string PascalToKebab(string input)
{
return CaseConversion.PascalToKebab(input);
}
}
}

0 comments on commit 6bd5814

Please sign in to comment.