Port to Linguini
This commit is contained in:
committed by
Paul Chote
parent
9d8c2bb4c4
commit
834de4efbe
@@ -11,10 +11,13 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Fluent.Net;
|
||||
using Fluent.Net.RuntimeAst;
|
||||
using Linguini.Bundle;
|
||||
using Linguini.Bundle.Builder;
|
||||
using Linguini.Shared.Types.Bundle;
|
||||
using Linguini.Syntax.Parser;
|
||||
using OpenRA.FileSystem;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -34,82 +37,87 @@ namespace OpenRA
|
||||
|
||||
public class Translation
|
||||
{
|
||||
readonly IEnumerable<MessageContext> messageContexts;
|
||||
readonly FluentBundle bundle;
|
||||
|
||||
public Translation(string language, string[] translations, IReadOnlyFileSystem fileSystem)
|
||||
{
|
||||
if (translations == null || translations.Length == 0)
|
||||
return;
|
||||
|
||||
messageContexts = GetMessageContext(language, translations, fileSystem).ToList();
|
||||
bundle = LinguiniBuilder.Builder()
|
||||
.CultureInfo(CultureInfo.InvariantCulture)
|
||||
.SkipResources()
|
||||
.SetUseIsolating(false)
|
||||
.UseConcurrent()
|
||||
.UncheckedBuild();
|
||||
|
||||
ParseTranslations(language, translations, fileSystem);
|
||||
}
|
||||
|
||||
static IEnumerable<MessageContext> GetMessageContext(string language, string[] translations, IReadOnlyFileSystem fileSystem)
|
||||
void ParseTranslations(string language, string[] translations, IReadOnlyFileSystem fileSystem)
|
||||
{
|
||||
var backfall = translations.Where(t => t.EndsWith("en.ftl"));
|
||||
var paths = translations.Where(t => t.EndsWith(language + ".ftl"));
|
||||
foreach (var path in paths.Concat(backfall))
|
||||
// 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 paths = translations.Where(t => t.EndsWith("en.ftl")).ToHashSet();
|
||||
foreach (var t in translations)
|
||||
if (t.EndsWith($"{language}.ftl"))
|
||||
paths.Add(t);
|
||||
|
||||
foreach (var path in paths)
|
||||
{
|
||||
var stream = fileSystem.Open(path);
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
var options = new MessageContextOptions { UseIsolating = false };
|
||||
var messageContext = new MessageContext(language, options);
|
||||
var errors = messageContext.AddMessages(reader);
|
||||
foreach (var error in errors)
|
||||
var parser = new LinguiniParser(reader);
|
||||
var resource = parser.Parse();
|
||||
foreach (var error in resource.Errors)
|
||||
Log.Write("debug", error.ToString());
|
||||
|
||||
yield return messageContext;
|
||||
bundle.AddResourceOverriding(resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetFormattedMessage(string key, IDictionary<string, object> args = null, string attribute = null)
|
||||
public string GetString(string key, IDictionary<string, object> arguments = null)
|
||||
{
|
||||
if (key == null)
|
||||
return "";
|
||||
if (!TryGetString(key, out var message, arguments))
|
||||
message = key;
|
||||
|
||||
foreach (var messageContext in messageContexts)
|
||||
return message;
|
||||
}
|
||||
|
||||
public bool TryGetString(string key, out string value, IDictionary<string, object> arguments = null)
|
||||
{
|
||||
if (!HasMessage(key))
|
||||
{
|
||||
var message = messageContext.GetMessage(key);
|
||||
if (message != null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(attribute))
|
||||
return messageContext.Format(message, args);
|
||||
else
|
||||
return messageContext.Format(message.Attributes[attribute], args);
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
var fluentArguments = new Dictionary<string, IFluentType>();
|
||||
if (arguments != null)
|
||||
foreach (var (k, v) in arguments)
|
||||
fluentArguments.Add(k, v.ToFluentType());
|
||||
|
||||
public bool HasAttribute(string key)
|
||||
{
|
||||
foreach (var messageContext in messageContexts)
|
||||
if (messageContext.HasMessage(key))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public string GetAttribute(string key, string attribute)
|
||||
{
|
||||
if (key == null)
|
||||
return "";
|
||||
|
||||
foreach (var messageContext in messageContexts)
|
||||
try
|
||||
{
|
||||
var message = messageContext.GetMessage(key);
|
||||
if (message != null && message.Attributes != null && message.Attributes.ContainsKey(attribute))
|
||||
{
|
||||
var node = message.Attributes[attribute];
|
||||
var stringLiteral = (StringLiteral)node;
|
||||
return stringLiteral.Value;
|
||||
}
|
||||
}
|
||||
var result = bundle.TryGetAttrMsg(key, fluentArguments, out var errors, out value);
|
||||
foreach (var error in errors)
|
||||
Log.Write("debug", $"Translation of {key}: {error}");
|
||||
|
||||
return "";
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("debug", $"Translation of {key}: {e}");
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasMessage(string key)
|
||||
{
|
||||
return bundle.HasMessage(key);
|
||||
}
|
||||
|
||||
// Adapted from Fluent.Net.SimpleExample.TranslationService by Mark Weaver
|
||||
@@ -125,10 +133,8 @@ namespace OpenRA
|
||||
{
|
||||
name = args[i] as string;
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new ArgumentException($"Expected the argument at index {i} to be a non-empty string",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
value = args[i + 1];
|
||||
if (value == null)
|
||||
|
||||
Reference in New Issue
Block a user