Skip to content

Commit

Permalink
Runtime translation decisions. Not final.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keflon committed Feb 26, 2024
1 parent e9ca518 commit c97f2ad
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageLicenseFile>License.md</PackageLicenseFile>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>8.0.0.2-pre6</Version>
<Version>8.0.0.2-pre8</Version>
<PackageIcon>F0 gravatar.png</PackageIcon>
</PropertyGroup>

Expand Down
13 changes: 0 additions & 13 deletions FunctionZero.Maui.Controls/Services/LanguageProvider.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FunctionZero.Maui.MarkupExtensions;
using FunctionZero.ExpressionParserZero.BackingStore;
using FunctionZero.Maui.MarkupExtensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -7,7 +8,7 @@
using System.Text;
using System.Threading.Tasks;

namespace FunctionZero.Maui.Services
namespace FunctionZero.Maui.Services.Localisation
{
/// <summary>
/// Goal. To be decoupled enough to allow downloading and selection of new or updated language packs on the fly.
Expand Down Expand Up @@ -68,10 +69,18 @@ public void SetLanguage(string id)

public string[] CurrentLookup => _resourceHost[_resourceKey] as string[];

public string GetText(TEnum textId)
public string GetText(TEnum textId, IBackingStore host)
{
var retval = _languages[CurrentLanguageId].GetLookup()[(int)(object)textId];
return retval;
var lookup = _languages[CurrentLanguageId].GetLookup()[(int)(object)textId];

foreach (var item in lookup)
{
var result = item.Item1.Evaluate(host).Pop();
if (result.Type == ExpressionParserZero.Operands.OperandType.Bool)
if ((bool)result.GetValue() == true)
return item.Item2;
}
return $"textId {textId} not found.";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using FunctionZero.ExpressionParserZero.BackingStore;
using FunctionZero.ExpressionParserZero.Evaluator;

namespace FunctionZero.Maui.Services.Localisation
{
public class ExpressionString
{
private ExpressionTree _expression;
private readonly string _resultString;

public ExpressionString(ExpressionTree expression, string resultString)
{
_expression = expression;
_resultString = resultString;
}

public bool GetString(object backingStore, ref string result)
{
result = _resultString;

try
{
if (backingStore is IBackingStore pocoBackingStore)
return (bool)_expression.Evaluate(pocoBackingStore).Pop().GetValue();
else
return (bool)_expression.Evaluate(new PocoBackingStore(backingStore)).Pop().GetValue();
}
catch(Exception ex)
{
result = ex.Message;
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FunctionZero.Maui.Services
namespace FunctionZero.Maui.Services.Localisation
{
public class LanguageChangedEventArgs : EventArgs
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FunctionZero.ExpressionParserZero.Binding;
using FunctionZero.ExpressionParserZero.Evaluator;

namespace FunctionZero.Maui.Services.Localisation
{
public class LanguageProvider
{
private readonly ExpressionTree _trueExpression;

public LanguageProvider(Func<List<List<(ExpressionTree, string)>>> getLookup, string languageName)
{
GetLookup = getLookup;
LanguageName = languageName;
}
//item.Add((parser.Parse("true"), "There are loads of bananas!"));
public LanguageProvider(Func<IEnumerable<string>> getLookup, string languageName)
{
var parser = ExpressionParserFactory.GetExpressionParser();
_trueExpression = parser.Parse("true");

GetLookup = GetLookupFromStringList(getLookup);
LanguageName = languageName;
}

private Func<List<List<(ExpressionTree, string)>>> GetLookupFromStringList(Func<IEnumerable<string>> getLookup)
{
List<List<(ExpressionTree, string)>> lookup = new List<List<(ExpressionTree, string)>>();
foreach (var theString in getLookup())
{
var item = new List<(ExpressionTree, string)>();
item.Add((_trueExpression, theString));
lookup.Add(item);
}
return () => lookup;
}


//public Func<string[]> GetLookup { get; }
public Func<List<List<(ExpressionTree, string)>>> GetLookup { get; }
public string LanguageName { get; }
}
}
104 changes: 93 additions & 11 deletions SampleApp/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using FunctionZero.ExpressionParserZero.Binding;
using FunctionZero.ExpressionParserZero.Evaluator;
using FunctionZero.ExpressionParserZero.Operands;
using FunctionZero.ExpressionParserZero.Parser;
using FunctionZero.Maui.Controls;
using FunctionZero.Maui.MvvmZero;
using FunctionZero.Maui.Services;
using FunctionZero.Maui.Services.Localisation;
using SampleApp.Mvvm.Pages;
using SampleApp.Mvvm.Pages.Expander;
using SampleApp.Mvvm.Pages.List;
Expand Down Expand Up @@ -49,7 +51,7 @@ public static MauiApp CreateMauiApp()
.MapVmToView<MultiViewModalPageVm, MultiViewModalPage>()
.MapVmToView<TranslationHomePageVm, TranslationHomePage>()
;
}
)
Expand Down Expand Up @@ -121,21 +123,101 @@ private static LangService GetConfiguredLanguageService(IServiceProvider provide
}

// Example
private static string[] GetEnglish() => new string[] { "Hello", "World", "Welcome to the Moasure Playground!" };
private static string[] GetGerman() => new string[] { "Hallo", "Welt", "Willkommen auf dem Moasure Spielplatz!" };
//private static string[] GetEnglish() => new string[] { "Hello", "World", "Welcome to the Moasure Playground!" };
private static string[] GetGerman() => new string[] { "Bananas", "Hallo", "Welt", "Willkommen auf dem Moasure Spielplatz!" };

//private static List<(ExpressionTree, string)> GetEnglish2()
//{
// var retval = new List<(ExpressionTree, string)>();
private static List<List<(ExpressionTree, string)>> GetEnglish()
{
var parser = ExpressionParserFactory.GetExpressionParser();
var trueExpression = parser.Parse("true");

var retval = new List<List<(ExpressionTree, string)>>();

{
var item = new List<(ExpressionTree, string)>();

item.Add((parser.Parse("NumBananas == 0"), "There are no bananas!"));
item.Add((parser.Parse("NumBananas == 1"), "There is one banana!"));
item.Add((parser.Parse("NumBananas == 2"), "There are two bananas!"));
item.Add((parser.Parse("NumBananas < 10"), "There are {NumBananas} bananas!")); // TODO: {NumBananas}
item.Add((parser.Parse("true"), "There are loads of bananas!"));

//HashSet<string> dependsOn = new HashSet<string>();

//foreach(var tuple in item)
// foreach(var token in tuple.Item1.RpnTokens)
// if(token is Operand op)
// if(op.Type == OperandType.Variable)
// dependsOn.Add((string)op.GetValue());

retval.Add(item);
}
{
var item = new List<(ExpressionTree, string)>();

item.Add((trueExpression, "Hello!"));

retval.Add(item);
}
{
var item = new List<(ExpressionTree, string)>();

item.Add((trueExpression, "World"));

retval.Add(item);
}
{
var item = new List<(ExpressionTree, string)>();

item.Add((trueExpression, "This is a demonstration of FunctionZero Translation Service"));

retval.Add(item);
}


return retval;
}

//private static List<List<(ExpressionTree, string)>> GetGerman()
//{
// var parser = ExpressionParserFactory.GetExpressionParser();
// var trueExpression = parser.Parse("true");

// var retval = new List<List<(ExpressionTree, string)>>();

// {
// var item = new List<(ExpressionTree, string)>();

// item.Add((parser.Parse("NumBananas == 0"), "Es gibt keine Bananen!"));
// item.Add((parser.Parse("NumBananas == 1"), "Es gibt eine Banane!"));
// item.Add((parser.Parse("NumBananas == 2"), "Es gibt zwei Bananen!"));
// item.Add((parser.Parse("NumBananas < 10"), "Es gibt {NumBananas} Bananen!")); // TODO: {NumBananas}
// item.Add((parser.Parse("true"), "Es gibt jede Menge Bananen!"));

// retval.Add(item);
// }
// {
// var item = new List<(ExpressionTree, string)>();

// item.Add((trueExpression, "Hallo!"));

// retval.Add(item);
// }
// {
// var item = new List<(ExpressionTree, string)>();

// item.Add((trueExpression, "Welt"));

// retval.Add(item);
// }
// {
// var item = new List<(ExpressionTree, string)>();

// item.Add((trueExpression, "Dies ist eine Demonstration des FunctionZero Translation Service"));

// retval.Add((parser.Parse("NumBananas == 0"), "There are no bananas!"));
// retval.Add(item);
// }

// retval.Add((parser.Parse("NumBananas == 1"), "There is one banana!"));
// retval.Add((parser.Parse("NumBananas == 2"), "There are two bananas!"));
// retval.Add((parser.Parse("NumBananas < 10"), "There are {NumBananas} bananas!")); // TODO: {NumBananas}
// retval.Add((parser.Parse("true"), "There are loads of bananas!"));

// return retval;
//}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FunctionZero.CommandZero;
using FunctionZero.ExpressionParserZero.BackingStore;
using FunctionZero.Maui.MvvmZero;
using FunctionZero.Maui.Services;
using SampleApp.Mvvm.ViewModels;
Expand All @@ -23,6 +24,8 @@ public TranslationHomePageVm(LangService langService)
{
_langService = langService;

var thisAsPocoBackingStore = new PocoBackingStore(this);

SetLanguageCommand = new CommandBuilder()
.AddGuard(this)
.SetExecute(SetLanguageCommandExecute)
Expand All @@ -31,9 +34,18 @@ public TranslationHomePageVm(LangService langService)
DoTheThingCommand = new CommandBuilder()
.AddGuard(this)
.SetExecute(DoTheThingCommandExecute)
.SetName(() => langService.GetText(LangStrings.E_World))
.SetName(() => langService.GetText(LangStrings.E_Bananas, thisAsPocoBackingStore))
.AddObservedProperty(langService, nameof(LangService.CurrentLanguageId))
.Build();

NumBananas = 1;
}

private int _numBananas;
public int NumBananas
{
get => _numBananas;
set => SetProperty(ref _numBananas, value);
}

private void DoTheThingCommandExecute()
Expand Down
2 changes: 1 addition & 1 deletion SampleApp/Translations/LangService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FunctionZero.Maui.MarkupExtensions;
using FunctionZero.Maui.Services;
using FunctionZero.Maui.Services.Localisation;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
3 changes: 2 additions & 1 deletion SampleApp/Translations/LangStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ namespace SampleApp.Translations
/// </summary>
public enum LangStrings
{
E_Bananas,
E_Hello,
E_World,
E_Welcome
E_Welcome,
}
}

0 comments on commit c97f2ad

Please sign in to comment.