Add language plugin selection and loading feedback in I18N assistant

This commit is contained in:
Thorsten Sommer 2025-04-25 15:29:17 +02:00
parent a84cfa15ba
commit bf47329166
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 184 additions and 2 deletions

View File

@ -1,5 +1,81 @@
@attribute [Route(Routes.ASSISTANT_AI_STUDIO_I18N)]
@using AIStudio.Settings
@inherits AssistantBaseCore<AIStudio.Dialogs.Settings.SettingsDialogI18N>
<EnumSelection T="CommonLanguages" NameFunc="@(language => language.NameSelecting())" @bind-Value="@this.selectedTargetLanguage" ValidateSelection="@this.ValidatingTargetLanguage" Icon="@Icons.Material.Filled.Translate" Label="Target language" AllowOther="@true" OtherValue="CommonLanguages.OTHER" @bind-OtherInput="@this.customTargetLanguage" ValidateOther="@this.ValidateCustomLanguage" LabelOther="Custom target language" />
<ProviderSelection @bind-ProviderSettings="@this.providerSettings" ValidateProvider="@this.ValidatingProvider"/>
<ConfigurationSelect OptionDescription="Language plugin used for comparision" SelectedValue="@(() => this.selectedLanguagePluginId)" Data="@ConfigurationSelectDataFactory.GetLanguagesData()" SelectionUpdate="@(async void (id) => await this.OnLanguagePluginChanged(id))" OptionHelp="Select the language plugin used for comparision."/>
@if (this.isLoading)
{
<MudText Typo="Typo.body1" Class="mb-6">
The data is being loaded, please wait...
</MudText>
} else if (!this.isLoading && !string.IsNullOrWhiteSpace(this.loadingIssue))
{
<MudText Typo="Typo.body1" Class="mb-6">
While loading the I18N data, an issue occurred: @this.loadingIssue
</MudText>
}
else if (!this.isLoading && string.IsNullOrWhiteSpace(this.loadingIssue))
{
<MudText Typo="Typo.h6">
Added Content (@this.addedKeys.Count entries)
</MudText>
<MudTable Items="@this.addedKeys" Hover="@true" Filter="@this.FilterFunc" Class="border-dashed border rounded-lg mb-6">
<ToolBarContent>
<MudTextField @bind-Value="@this.searchString" Immediate="true" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"/>
</ToolBarContent>
<ColGroup>
<col/>
<col/>
</ColGroup>
<HeaderContent>
<MudTh>Key</MudTh>
<MudTh>Text</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd>
<pre style="font-size: 0.8em;">
@context.Key
</pre>
</MudTd>
<MudTd>
@context.Value
</MudTd>
</RowTemplate>
<PagerContent>
<MudTablePager />
</PagerContent>
</MudTable>
<MudText Typo="Typo.h6">
Removed Content (@this.removedKeys.Count entries)
</MudText>
<MudTable Items="@this.removedKeys" Hover="@true" Filter="@this.FilterFunc" Class="border-dashed border rounded-lg mb-6">
<ToolBarContent>
<MudTextField @bind-Value="@this.searchString" Immediate="true" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"/>
</ToolBarContent>
<ColGroup>
<col/>
<col/>
</ColGroup>
<HeaderContent>
<MudTh>Key</MudTh>
<MudTh>Text</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd>
<pre style="font-size: 0.8em;">
@context.Key
</pre>
</MudTd>
<MudTd>
@context.Value
</MudTd>
</RowTemplate>
<PagerContent>
<MudTablePager />
</PagerContent>
</MudTable>
<EnumSelection T="CommonLanguages" NameFunc="@(language => language.NameSelecting())" @bind-Value="@this.selectedTargetLanguage" ValidateSelection="@this.ValidatingTargetLanguage" Icon="@Icons.Material.Filled.Translate" Label="Target language" AllowOther="@true" OtherValue="CommonLanguages.OTHER" @bind-OtherInput="@this.customTargetLanguage" ValidateOther="@this.ValidateCustomLanguage" LabelOther="Custom target language" />
<ProviderSelection @bind-ProviderSettings="@this.providerSettings" ValidateProvider="@this.ValidatingProvider"/>
}

View File

@ -1,4 +1,7 @@
using AIStudio.Dialogs.Settings;
using AIStudio.Tools.PluginSystem;
using Microsoft.Extensions.FileProviders;
namespace AIStudio.Assistants.I18N;
@ -25,11 +28,14 @@ public partial class AssistantI18N : AssistantBaseCore<SettingsDialogI18N>
protected override string SubmitText => "Localize AI Studio";
protected override Func<Task> SubmitAction => this.LocalizeText;
protected override bool SubmitDisabled => !this.localizationPossible;
protected override void ResetForm()
{
if (!this.MightPreselectValues())
{
this.selectedLanguagePluginId = InternalPlugin.LANGUAGE_EN_US.MetaData().Id;
this.selectedTargetLanguage = CommonLanguages.AS_IS;
this.customTargetLanguage = string.Empty;
}
@ -39,6 +45,7 @@ public partial class AssistantI18N : AssistantBaseCore<SettingsDialogI18N>
{
if (this.SettingsManager.ConfigurationData.I18N.PreselectOptions)
{
this.selectedLanguagePluginId = this.SettingsManager.ConfigurationData.I18N.PreselectedLanguagePluginId;
this.selectedTargetLanguage = this.SettingsManager.ConfigurationData.I18N.PreselectedTargetLanguage;
this.customTargetLanguage = this.SettingsManager.ConfigurationData.I18N.PreselectOtherLanguage;
return true;
@ -49,6 +56,105 @@ public partial class AssistantI18N : AssistantBaseCore<SettingsDialogI18N>
private CommonLanguages selectedTargetLanguage;
private string customTargetLanguage = string.Empty;
private bool isLoading = true;
private string loadingIssue = string.Empty;
private bool localizationPossible;
private string searchString = string.Empty;
private Guid selectedLanguagePluginId;
private Dictionary<string, string> addedKeys = [];
private Dictionary<string, string> removedKeys = [];
#region Overrides of AssistantBase<SettingsDialogI18N>
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await this.LoadData();
}
#endregion
private async Task OnLanguagePluginChanged(Guid pluginId)
{
this.selectedLanguagePluginId = pluginId;
await this.LoadData();
this.StateHasChanged();
}
private async Task LoadData()
{
this.isLoading = true;
this.StateHasChanged();
//
// Read the file `Assistants\I18N\allTexts.lua`:
//
#if DEBUG
var filePath = Path.Join(Environment.CurrentDirectory, "Assistants", "I18N");
var resourceFileProvider = new PhysicalFileProvider(filePath);
#else
var resourceFileProvider = new ManifestEmbeddedFileProvider(Assembly.GetAssembly(type: typeof(Program))!, "Assistants.I18N");
#endif
var file = resourceFileProvider.GetFileInfo("allTexts.lua");
await using var fileStream = file.CreateReadStream();
using var reader = new StreamReader(fileStream);
var newI18NDataLuaCode = await reader.ReadToEndAsync();
//
// Next, we try to load the text as a language plugin -- without
// actually starting the plugin:
//
var newI18NPlugin = await PluginFactory.Load(null, newI18NDataLuaCode);
switch (newI18NPlugin)
{
case NoPlugin noPlugin when noPlugin.Issues.Any():
this.loadingIssue = noPlugin.Issues.First();
break;
case NoPlugin:
this.loadingIssue = "Was not able to load the I18N plugin. Please check the plugin code.";
break;
case { IsValid: false } plugin when plugin.Issues.Any():
this.loadingIssue = plugin.Issues.First();
break;
case PluginLanguage pluginLanguage:
this.loadingIssue = string.Empty;
var newI18NContent = pluginLanguage.Content;
if(PluginFactory.RunningPlugins.FirstOrDefault(n => n is PluginLanguage && n.Id == this.selectedLanguagePluginId) is not PluginLanguage comparisonPlugin)
{
this.loadingIssue = $"Was not able to load the language plugin for comparison ({this.selectedLanguagePluginId}). Please select a valid, loaded & running language plugin.";
break;
}
var currentI18NContent = comparisonPlugin.Content;
this.addedKeys = newI18NContent.ExceptBy(currentI18NContent.Keys, n => n.Key).ToDictionary();
this.removedKeys = currentI18NContent.ExceptBy(newI18NContent.Keys, n => n.Key).ToDictionary();
this.localizationPossible = true;
break;
}
this.isLoading = false;
this.StateHasChanged();
}
private bool FilterFunc(KeyValuePair<string, string> element)
{
if (string.IsNullOrWhiteSpace(this.searchString))
return true;
if (element.Key.Contains(this.searchString, StringComparison.OrdinalIgnoreCase))
return true;
if (element.Value.Contains(this.searchString, StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
private string? ValidatingTargetLanguage(CommonLanguages language)
{