mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Automatically fetch names and descs from loc.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.ContentPack;
|
||||
@@ -31,11 +31,15 @@ namespace Robust.Shared.Localization
|
||||
/// </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.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.ContentPack;
|
||||
@@ -36,6 +36,11 @@ namespace Robust.Shared.Localization
|
||||
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>
|
||||
@@ -44,6 +49,14 @@ namespace Robust.Shared.Localization
|
||||
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>
|
||||
|
||||
@@ -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;
|
||||
@@ -20,42 +22,107 @@ namespace Robust.Shared.Localization
|
||||
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;
|
||||
}
|
||||
var context = _contexts[_defaultCulture];
|
||||
var message = context.GetMessage(messageId);
|
||||
var args = new Dictionary<string, object>();
|
||||
foreach (var vari in args0)
|
||||
|
||||
if (!TryGetString(messageId, out var msg, args0))
|
||||
{
|
||||
args.Add(vari.Item1, vari.Item2);
|
||||
Logger.WarningS("Loc", $"Unknown messageId ({_defaultCulture.IetfLanguageTag}): {messageId}");
|
||||
msg = 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 (k, v) in args0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return context.Format(message, args, null);
|
||||
if (attribName != null)
|
||||
{
|
||||
if (!message.Attributes.TryGetValue(attribName, out var attrib))
|
||||
{
|
||||
node = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
node = attrib;
|
||||
}
|
||||
else
|
||||
{
|
||||
node = message;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -94,7 +161,7 @@ namespace Robust.Shared.Localization
|
||||
{
|
||||
var context = new MessageContext(
|
||||
culture.Name,
|
||||
new MessageContextOptions { UseIsolating = false }
|
||||
new MessageContextOptions {UseIsolating = false}
|
||||
);
|
||||
|
||||
_contexts.Add(culture, context);
|
||||
@@ -144,7 +211,8 @@ namespace Robust.Shared.Localization
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
@@ -146,12 +146,18 @@ namespace Robust.Shared.Prototypes
|
||||
|
||||
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))
|
||||
@@ -163,12 +169,17 @@ namespace Robust.Shared.Prototypes
|
||||
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
|
||||
|
||||
18
Robust.Shared/Utility/CaseConversion.cs
Normal file
18
Robust.Shared/Utility/CaseConversion.cs
Normal file
@@ -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
Robust.UnitTesting/Shared/Utility/CaseConversionTest.cs
Normal file
23
Robust.UnitTesting/Shared/Utility/CaseConversionTest.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user