Reorganize FluentBundle ctors to allow inline text.

This commit is contained in:
Paul Chote
2024-10-19 13:46:07 +01:00
committed by Gustas
parent e4539e9cb5
commit 43219e16da
6 changed files with 46 additions and 48 deletions

View File

@@ -56,28 +56,17 @@ namespace OpenRA
public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem) public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem)
: this(language, paths, fileSystem, error => Log.Write("debug", error.Message)) { } : this(language, paths, fileSystem, error => Log.Write("debug", error.Message)) { }
public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem, string text)
: this(language, paths, fileSystem, text, error => Log.Write("debug", error.Message)) { }
public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem, Action<ParseError> onError) public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem, Action<ParseError> onError)
{ : this(language, paths, fileSystem, null, onError) { }
if (paths == null || paths.Length == 0)
return;
bundle = LinguiniBuilder.Builder()
.CultureInfo(new CultureInfo(language))
.SkipResources()
.SetUseIsolating(false)
.UseConcurrent()
.UncheckedBuild();
Load(language, paths, fileSystem, onError);
}
public FluentBundle(string language, string text, Action<ParseError> onError) public FluentBundle(string language, string text, Action<ParseError> onError)
{ : this(language, null, null, text, onError) { }
var parser = new LinguiniParser(text);
var resource = parser.Parse();
foreach (var error in resource.Errors)
onError(error);
public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem, string text, Action<ParseError> onError)
{
bundle = LinguiniBuilder.Builder() bundle = LinguiniBuilder.Builder()
.CultureInfo(new CultureInfo(language)) .CultureInfo(new CultureInfo(language))
.SkipResources() .SkipResources()
@@ -85,10 +74,7 @@ namespace OpenRA
.UseConcurrent() .UseConcurrent()
.UncheckedBuild(); .UncheckedBuild();
bundle.AddResourceOverriding(resource); if (paths != null && paths.Length > 0)
}
void Load(string language, string[] paths, IReadOnlyFileSystem fileSystem, Action<ParseError> onError)
{ {
// Always load english strings to provide a fallback for missing translations. // Always load english strings to provide a fallback for missing translations.
// It is important to load the english files first so the chosen language's files can override them. // It is important to load the english files first so the chosen language's files can override them.
@@ -112,6 +98,17 @@ namespace OpenRA
} }
} }
if (!string.IsNullOrEmpty(text))
{
var parser = new LinguiniParser(text);
var resource = parser.Parse();
foreach (var error in resource.Errors)
onError(error);
bundle.AddResourceOverriding(resource);
}
}
public string GetMessage(string key, object[] args = null) public string GetMessage(string key, object[] args = null)
{ {
if (!TryGetMessage(key, out var message, args)) if (!TryGetMessage(key, out var message, args))

View File

@@ -25,8 +25,8 @@ namespace OpenRA
lock (SyncObject) lock (SyncObject)
{ {
modFluentBundle = new FluentBundle(Game.Settings.Player.Language, modData.Manifest.Translations, fileSystem); modFluentBundle = new FluentBundle(Game.Settings.Player.Language, modData.Manifest.Translations, fileSystem);
mapFluentBundle = fileSystem is Map map && map.TranslationDefinitions != null mapFluentBundle = fileSystem is Map map && map.FluentMessageDefinitions != null
? new FluentBundle(Game.Settings.Player.Language, FieldLoader.GetValue<string[]>("value", map.TranslationDefinitions.Value), fileSystem) ? new FluentBundle(Game.Settings.Player.Language, FieldLoader.GetValue<string[]>("value", map.FluentMessageDefinitions.Value), fileSystem)
: null; : null;
} }
} }

View File

@@ -172,7 +172,7 @@ namespace OpenRA
new("Players", nameof(PlayerDefinitions)), new("Players", nameof(PlayerDefinitions)),
new("Actors", nameof(ActorDefinitions)), new("Actors", nameof(ActorDefinitions)),
new("Rules", nameof(RuleDefinitions), required: false), new("Rules", nameof(RuleDefinitions), required: false),
new("Translations", nameof(TranslationDefinitions), required: false), new("Translations", nameof(FluentMessageDefinitions), required: false),
new("Sequences", nameof(SequenceDefinitions), required: false), new("Sequences", nameof(SequenceDefinitions), required: false),
new("ModelSequences", nameof(ModelSequenceDefinitions), required: false), new("ModelSequences", nameof(ModelSequenceDefinitions), required: false),
new("Weapons", nameof(WeaponDefinitions), required: false), new("Weapons", nameof(WeaponDefinitions), required: false),
@@ -203,7 +203,7 @@ namespace OpenRA
// Custom map yaml. Public for access by the map importers and lint checks // Custom map yaml. Public for access by the map importers and lint checks
public MiniYaml RuleDefinitions; public MiniYaml RuleDefinitions;
public MiniYaml TranslationDefinitions; public MiniYaml FluentMessageDefinitions;
public MiniYaml SequenceDefinitions; public MiniYaml SequenceDefinitions;
public MiniYaml ModelSequenceDefinitions; public MiniYaml ModelSequenceDefinitions;
public MiniYaml WeaponDefinitions; public MiniYaml WeaponDefinitions;

View File

@@ -90,6 +90,7 @@ namespace OpenRA
public MiniYaml NotificationDefinitions; public MiniYaml NotificationDefinitions;
public MiniYaml SequenceDefinitions; public MiniYaml SequenceDefinitions;
public MiniYaml ModelSequenceDefinitions; public MiniYaml ModelSequenceDefinitions;
public MiniYaml FluentMessageDefinitions;
public FluentBundle FluentBundle { get; private set; } public FluentBundle FluentBundle { get; private set; }
public ActorInfo WorldActorInfo { get; private set; } public ActorInfo WorldActorInfo { get; private set; }
@@ -335,7 +336,7 @@ namespace OpenRA
innerData.SetCustomRules(modData, this, new Dictionary<string, MiniYaml>() innerData.SetCustomRules(modData, this, new Dictionary<string, MiniYaml>()
{ {
{ "Rules", map.RuleDefinitions }, { "Rules", map.RuleDefinitions },
{ "Translations", map.TranslationDefinitions }, { "Translations", map.FluentMessageDefinitions },
{ "Weapons", map.WeaponDefinitions }, { "Weapons", map.WeaponDefinitions },
{ "Voices", map.VoiceDefinitions }, { "Voices", map.VoiceDefinitions },
{ "Music", map.MusicDefinitions }, { "Music", map.MusicDefinitions },

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.Common.Lint
void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map) void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map)
{ {
if (map.TranslationDefinitions == null) if (map.FluentMessageDefinitions == null)
return; return;
var usedKeys = GetUsedFluentKeysInMap(map, emitWarning); var usedKeys = GetUsedFluentKeysInMap(map, emitWarning);
@@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Lint
foreach (var context in usedKeys.EmptyKeyContexts) foreach (var context in usedKeys.EmptyKeyContexts)
emitWarning($"Empty key in map ftl files required by {context}"); emitWarning($"Empty key in map ftl files required by {context}");
var mapTranslations = FieldLoader.GetValue<string[]>("value", map.TranslationDefinitions.Value); var mapTranslations = FieldLoader.GetValue<string[]>("value", map.FluentMessageDefinitions.Value);
var allModTranslations = modData.Manifest.Translations; var allModTranslations = modData.Manifest.Translations;
foreach (var language in GetModLanguages(allModTranslations)) foreach (var language in GetModLanguages(allModTranslations))

View File

@@ -22,10 +22,10 @@ namespace OpenRA.Mods.Common.Lint
{ {
void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map) void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map)
{ {
if (map.TranslationDefinitions == null) if (map.FluentMessageDefinitions == null)
return; return;
Run(emitError, emitWarning, map, FieldLoader.GetValue<string[]>("value", map.TranslationDefinitions.Value)); Run(emitError, emitWarning, map, FieldLoader.GetValue<string[]>("value", map.FluentMessageDefinitions.Value));
} }
void ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData) void ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData)