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)
: 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)
{
if (paths == null || paths.Length == 0)
return;
bundle = LinguiniBuilder.Builder()
.CultureInfo(new CultureInfo(language))
.SkipResources()
.SetUseIsolating(false)
.UseConcurrent()
.UncheckedBuild();
Load(language, paths, fileSystem, onError);
}
: this(language, paths, fileSystem, null, onError) { }
public FluentBundle(string language, string text, Action<ParseError> onError)
{
var parser = new LinguiniParser(text);
var resource = parser.Parse();
foreach (var error in resource.Errors)
onError(error);
: this(language, null, null, text, onError) { }
public FluentBundle(string language, string[] paths, IReadOnlyFileSystem fileSystem, string text, Action<ParseError> onError)
{
bundle = LinguiniBuilder.Builder()
.CultureInfo(new CultureInfo(language))
.SkipResources()
@@ -85,31 +74,39 @@ namespace OpenRA
.UseConcurrent()
.UncheckedBuild();
bundle.AddResourceOverriding(resource);
}
void Load(string language, string[] paths, IReadOnlyFileSystem fileSystem, Action<ParseError> onError)
{
// 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.
var resolvedPaths = paths.Where(t => t.EndsWith("en.ftl", StringComparison.Ordinal)).ToList();
foreach (var t in paths)
if (t.EndsWith($"{language}.ftl", StringComparison.Ordinal))
resolvedPaths.Add(t);
foreach (var path in resolvedPaths.Distinct())
if (paths != null && paths.Length > 0)
{
var stream = fileSystem.Open(path);
using (var reader = new StreamReader(stream))
{
var parser = new LinguiniParser(reader);
var resource = parser.Parse();
foreach (var error in resource.Errors)
onError(error);
// 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.
var resolvedPaths = paths.Where(t => t.EndsWith("en.ftl", StringComparison.Ordinal)).ToList();
foreach (var t in paths)
if (t.EndsWith($"{language}.ftl", StringComparison.Ordinal))
resolvedPaths.Add(t);
bundle.AddResourceOverriding(resource);
foreach (var path in resolvedPaths.Distinct())
{
var stream = fileSystem.Open(path);
using (var reader = new StreamReader(stream))
{
var parser = new LinguiniParser(reader);
var resource = parser.Parse();
foreach (var error in resource.Errors)
onError(error);
bundle.AddResourceOverriding(resource);
}
}
}
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)

View File

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

View File

@@ -172,7 +172,7 @@ namespace OpenRA
new("Players", nameof(PlayerDefinitions)),
new("Actors", nameof(ActorDefinitions)),
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("ModelSequences", nameof(ModelSequenceDefinitions), 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
public MiniYaml RuleDefinitions;
public MiniYaml TranslationDefinitions;
public MiniYaml FluentMessageDefinitions;
public MiniYaml SequenceDefinitions;
public MiniYaml ModelSequenceDefinitions;
public MiniYaml WeaponDefinitions;

View File

@@ -90,6 +90,7 @@ namespace OpenRA
public MiniYaml NotificationDefinitions;
public MiniYaml SequenceDefinitions;
public MiniYaml ModelSequenceDefinitions;
public MiniYaml FluentMessageDefinitions;
public FluentBundle FluentBundle { get; private set; }
public ActorInfo WorldActorInfo { get; private set; }
@@ -335,7 +336,7 @@ namespace OpenRA
innerData.SetCustomRules(modData, this, new Dictionary<string, MiniYaml>()
{
{ "Rules", map.RuleDefinitions },
{ "Translations", map.TranslationDefinitions },
{ "Translations", map.FluentMessageDefinitions },
{ "Weapons", map.WeaponDefinitions },
{ "Voices", map.VoiceDefinitions },
{ "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)
{
if (map.TranslationDefinitions == null)
if (map.FluentMessageDefinitions == null)
return;
var usedKeys = GetUsedFluentKeysInMap(map, emitWarning);
@@ -42,7 +42,7 @@ namespace OpenRA.Mods.Common.Lint
foreach (var context in usedKeys.EmptyKeyContexts)
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;
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)
{
if (map.TranslationDefinitions == null)
if (map.FluentMessageDefinitions == null)
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)