Rename Lua UserInterface.Translate to .GetFluentMessage.

This commit is contained in:
Paul Chote
2024-10-19 14:52:36 +01:00
committed by Gustas
parent 8b11b499ed
commit 8452f71481
94 changed files with 308 additions and 308 deletions

View File

@@ -158,22 +158,22 @@ namespace OpenRA.Mods.Common.Lint
if (luaScriptInfo != null)
{
// Matches expressions such as:
// UserInterface.Translate("fluent-key")
// UserInterface.Translate("fluent-key\"with-escape")
// UserInterface.Translate("fluent-key", { ["attribute"] = foo })
// UserInterface.Translate("fluent-key", { ["attribute\"-with-escape"] = foo })
// UserInterface.Translate("fluent-key", { ["attribute1"] = foo, ["attribute2"] = bar })
// UserInterface.Translate("fluent-key", tableVariable)
// UserInterface.GetFluentMessage("fluent-key")
// UserInterface.GetFluentMessage("fluent-key\"with-escape")
// UserInterface.GetFluentMessage("fluent-key", { ["attribute"] = foo })
// UserInterface.GetFluentMessage("fluent-key", { ["attribute\"-with-escape"] = foo })
// UserInterface.GetFluentMessage("fluent-key", { ["attribute1"] = foo, ["attribute2"] = bar })
// UserInterface.GetFluentMessage("fluent-key", tableVariable)
// Extracts groups for the 'key' and each 'attr'.
// If the table isn't inline like in the last example, extracts it as 'variable'.
const string UserInterfaceTranslatePattern =
@"UserInterface\s*\.\s*Translate\s*\(" + // UserInterface.Translate(
const string UserInterfaceFluentMessagePattern =
@"UserInterface\s*\.\s*GetFluentMessage\s*\(" + // UserInterface.GetFluentMessage(
@"\s*""(?<key>(?:[^""\\]|\\.)+?)""\s*" + // "fluent-key"
@"(,\s*({\s*\[\s*""(?<attr>(?:[^""\\]|\\.)*?)""\s*\]\s*=\s*.*?" + // { ["attribute1"] = foo
@"(\s*,\s*\[\s*""(?<attr>(?:[^""\\]|\\.)*?)""\s*\]\s*=\s*.*?)*\s*}\s*)" + // , ["attribute2"] = bar }
"|\\s*,\\s*(?<variable>.*?))?" + // tableVariable
@"\)"; // )
var translateRegex = new Regex(UserInterfaceTranslatePattern);
var fluentMessageRegex = new Regex(UserInterfaceFluentMessagePattern);
// The script in mods/common/scripts/utils.lua defines some helpers which accept a fluent key
// Matches expressions such as:
@@ -195,7 +195,7 @@ namespace OpenRA.Mods.Common.Lint
using (scriptStream)
{
var scriptText = scriptStream.ReadAllText();
IEnumerable<Match> matches = translateRegex.Matches(scriptText);
IEnumerable<Match> matches = fluentMessageRegex.Matches(scriptText);
if (luaScriptInfo.Scripts.Contains("utils.lua"))
matches = matches.Concat(objectiveRegex.Matches(scriptText));
@@ -216,9 +216,9 @@ namespace OpenRA.Mods.Common.Lint
if (variable != "")
{
var userInterface = typeof(UserInterfaceGlobal).GetCustomAttribute<ScriptGlobalAttribute>().Name;
const string Translate = nameof(UserInterfaceGlobal.Translate);
const string FluentMessage = nameof(UserInterfaceGlobal.GetFluentMessage);
emitWarning(
$"{context} calls {userInterface}.{Translate} with key `{key}` and translate args passed as `{variable}`." +
$"{context} calls {userInterface}.{FluentMessage} with key `{key}` and args passed as `{variable}`." +
"Inline the args at the callsite for lint analysis.");
}
}

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.Scripting.Global
[Desc("Formats a language string for a given string key defined in the language files (*.ftl). " +
"Args can be passed to be substituted into the resulting message.")]
public string Translate(string key, [ScriptEmmyTypeOverride("{ string: any }")] LuaTable args = null)
public string GetFluentMessage(string key, [ScriptEmmyTypeOverride("{ string: any }")] LuaTable args = null)
{
if (args != null)
{